Switch statements- grading system - c

#include <stdio.h>
void main() {
int a, b, c, d, e, f, avg;
printf("Enter marks of six subjects: ");
scanf("%d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f);
avg = (a + b + c + d + e + f) / 6;
printf("Average is %d", avg);
switch (avg) {
case 10:
printf("Your grade is 10.");
break;
case 9:
printf("Your grade is 9.");
break;
case 8:
printf("Your grade is 8.");
break;
case 7:
printf("Your grade is 7.");
break;
case 6:
printf("Your grade is 6.");
break;
case 5:
printf("Your grade is 5.");
default:
printf("You are fail.");
}
}
After I enter the marks of six subjects, the code is not running, it's neither showing any error message as well.

Problems include
Not enabling all compiler warnings
Mis-match
"%d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f: 7 specifiers and 6 following arguments.
Not checking return value of scanf()
// scanf("%d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f);
if (scanf("%d %d %d %d %d %d", &a, &b, &c, &d, &e, &f) != 6) {
; // TBD: Handle error, maybe print some error message and exit
}
No break
case 5:
printf("Your grade is 5.");
break; // add
default:
printf("You are fail.");

Related

c lang, double printed statement in switch case [duplicate]

This question already has answers here:
scanf not working as expected
(4 answers)
C loop prints string twice? (using scanf("%c"))
(2 answers)
why does this c program print twice [closed]
(1 answer)
Closed 2 years ago.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
char op;
float a, b;
printf("\n Operators are ( +,-,*,/,^) \n");
printf("Enter 0 to quit \n");
while(1)
{
printf("Enter your choice: \n");
scanf("%c", &op);
switch(op)
{
case '+':
printf(" Enter your two numbers: \n");
scanf("%f %f", &a, &b);
printf("%f + %f = %f", a, b, a+b);
break;
case '-':
printf(" Enter your two numbers: \n");
scanf("%f %f", &a, &b);
printf("%f - %f = %f", a, b, a-b);
break;
case '*':
printf(" Enter your two numbers: \n");
scanf("%f %f", &a, &b);
printf("%f x %f = %f", a, b, a*b);
break;
case '/':
printf(" Enter your two numbers: \n");
scanf("%f %f", &a, &b);
printf("%f / %f = %f", a, b, a/b);
break;
case '^':
printf(" Enter your two numbers: \n");
scanf("%f %f", &a, &b);
printf("%f ^ %f = %f", a, b, pow(a, b));
break;
case '0':
exit(0);
}
}
return 0;
}
This program is basically a switch case basic calculator.
After the while loop start, the program prints "Enter your choice" twice instead of once, can anyone tell how to fix this. Apart from that it works fine. The while loop is for menu driven program.

Have reviewed several answers and still don't get powers in C [duplicate]

This question already has answers here:
pow() isn't defined [closed]
(4 answers)
Closed 3 years ago.
I have reviewed multiple questions and answers for powers in C, or 4^2. I have wrote it out in different ways and still can't it right. As of now I get the message:
/tmp/cc9yBiPo.o: In function `main':
Functions2.c:(.text+0x2b8): undefined reference to `pow'
collect2: error: ld returned 1 exit status.
Here is my code so far, like I said I have tried different peoples answers but I am more confused more than anything.
#include <stdio.h>
#include <math.h>
int main()
{
int choice;
int a, b, c;
float d;
do{
printf("\t Menu \n");
printf("1. Addition \n");
printf("2. Subtraction \n");
printf("3. Multiplication \n");
printf("4. Division \n");
printf("5. Modulo \n");
printf("6. Eponents \n");
printf("7. Exit \n");
printf("Please choose a menu selection: \n");
scanf("%d", &choice);
switch(choice)
{
case 1:
{
printf("Enter two numbers to add: \n");
scanf("%d%d", &a, &b);
c = a + b;
printf("The sum of the two numbers = %d \n", c);
}break;
case 2:
{
printf("Enter two numbers to subtract: \n");
scanf("%d&d", &a, &b);
c = a - b;
printf("The difference between the two number = %d \n", c);
}break;
case 3:
{
printf("Enter two numbers to multiply: \n");
scanf("%d%d", &a, &b);
c = a * b;
printf("The product of the two numbers = %d \n", c);
}break;
case 4:
{
printf("Enter two numbers to divide: \n");
scanf("%d%d", &a, &b);
d = (float) a/b;
printf("The quotient of the two numbers = %f \n", d);
}break;
case 5:
{
printf("Enter two numbers to get a modulo: \n");
scanf("%d%d", &a, &b);
c = a % b;
printf("The modulus of the two numbers = %d \n", c);
}break;
case 6:
{
printf("Enter a base: \n");
scanf("%d", &a);
printf("Enter an exponent: \n");
scanf("%d", &b);
c = pow(a, b);
printf("The exponent of the two numbers = %d", c);
}break;
case 7:
{
printf("Thank you, you will now exit.");
}break;
default:
printf("error \n");
}
} while (choice != 7);
return 0;
}
You have to link with the math library(libm).
Add -lm flag to your complication command to link with it.
Check this question.
You have to change the data types of base & exponent to get correct result.
The syntax for the pow function in the C Language is:
double pow(double x, double y);
Again, change the data type from int to float in case of 4.
#include <stdio.h>
#include <math.h>
int main()
{
double g,h,i;
int choice;
int a, b, c;
float d,e,f;
do{
printf("\t Menu \n");
printf("1. Addition \n");
printf("2. Subtraction \n");
printf("3. Multiplication \n");
printf("4. Division \n");
printf("5. Modulo \n");
printf("6. Eponents \n");
printf("7. Exit \n");
printf("Please choose a menu selection: \n");
scanf("%d", &choice);
switch(choice)
{
case 1:
{
printf("Enter two numbers to add: \n");
scanf("%d%d", &a, &b);
c = a + b;
printf("The sum of the two numbers = %d \n", c);
}break;
case 2:
{
printf("Enter two numbers to subtract: \n");
scanf("%d&d", &a, &b);
c = a - b;
printf("The difference between the two number = %d \n", c);
}break;
case 3:
{
printf("Enter two numbers to multiply: \n");
scanf("%d%d", &a, &b);
c = a * b;
printf("The product of the two numbers = %d \n", c);
}break;
case 4:
{
printf("Enter two numbers to divide: \n");
scanf("%f%f", &e, &f);
d = e/f;
printf("The quotient of the two numbers = %f \n", d);
}break;
case 5:
{
printf("Enter two numbers to get a modulo: \n");
scanf("%d%d", &a, &b);
c = a % b;
printf("The modulus of the two numbers = %d \n", c);
}break;
case 6:
{
printf("Enter a base: \n");
scanf("%lf", &h);
printf("Enter an exponent: \n");
scanf("%lf", &i);
g = pow(h, i);
printf("The exponent of the two numbers = %lf", g);
}break;
case 7:
{
printf("Thank you, you will now exit.");
}break;
default:
printf("error \n");
}
} while (choice != 7);
return 0;
}
Hope this will help you.

Not getting output for division and modulus

The below C program gives me output only for Addition(+), difference(-), multiplication(*).
But when I try to use division(/) and modulus(%) the program just close itself without giving any error. Help me I'm a newbie to C programming.
//A simple calculator.
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, sum, diff, rem, multi;
float div;
char character;
clrscr();
printf("Choose the character you want to use(+, -, *, /, %): ");
scanf("%c", &character);
switch(character)
{
case '+': //will be used for addition.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
sum = a+b;
printf("The sum of the %d and %d is %d", a, b, sum);
break;
case '-': //will be used for difference.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
diff = a-b;
printf("The difference between %d and %d is %d", a, b, diff);
break;
case '%': //will be used for modulus.
printf("Enter the first and second number: ");
scanf("%f %f", &a, &b);
rem = a%b;
printf("The remainder of %f and %f is %f", a, b, rem);
break;
case '*': //will be used for product of 2 no.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
multi = a*b;
printf("The multiplication of %d and %d is %d", a, b, multi);
break;
case '/': //will be used for the division.
printf("Enter the first and second number: ");
scanf("%f %f", &a, &b);
div = a/b;
printf("The division of %f and %f is %f", a, b, div);
break;
default:
printf("Error! character please retry");
}
getch();
}
You are using %f format specifier for int variables in / and % cases.
scanf("%f %f", &a, &b);
Thus invoking undefined behavior.
Change it to as below.
scanf("%d %d", &a, &b);
%f is used to readin the float variables.
If you want float result for division, you need to cast the one of the argument to float instead reading them as float.
div = (float)a/b;
When using the / and % use
scanf("%d %d", &a, &b);
instead of
scanf("%f %f", &a, &b);
because %f is used for float vars whereas in case of / and % %d is used
You are using wrong formats. After enabling -Wall in gcc and fixing the warnings I get a working program. You also missing the \n in your answer printf()'s
#include <stdio.h>
int main(int argc, char **argv)
{
int a, b, sum, diff, rem, multi;
float div;
char character;
printf("Choose the character you want to use(+, -, *, /, %%): ");
scanf("%c", &character);
switch(character)
{
case '+': //will be used for addition.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
sum = a+b;
printf("The sum of the %d and %d is %d\n", a, b, sum);
break;
case '-': //will be used for difference.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
diff = a-b;
printf("The difference between %d and %d is %d\n", a, b, diff);
break;
case '%': //will be used for modulus.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
rem = a%b;
printf("The remainder of %d and %d is %d\n", a, b, rem);
break;
case '*': //will be used for product of 2 no.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
multi = a*b;
printf("The multiplication of %d and %d is %d\n", a, b, multi);
break;
case '/': //will be used for the division.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
div = a/b;
printf("The division of %d and %d is %f\n", a, b, div);
break;
default:
printf("Error! character please retry");
}
}
Test output:
$ ./dummy
Choose the character you want to use(+, -, *, /, %): %
Enter the first and second number: 5 2
The remainder of 5 and 2 is 1
$

Why the output is coming like this?

When I tried to do division of 6/3 the output comes like this 2 / -1431650288. What's wrong in code?
My program in c is like this:
#include <stdio.h>
int main(){
char Operator;
int num1, num2;
printf("Enter the operator in which you want to perform calculation(+, -, *, /)\n");
scanf(" %c", &Operator);
if (Operator == '/'){
printf("Enter two numbers: ");
scanf(" %d %d", &num1, &num2);
if (num2==0){
printf("\a Denominator must be greater than 0.\n");
}
else{
printf(" %d / %d", num1/num2);
}
}
else{
printf("Enter two integer numbers: ");
scanf(" %d %d", &num1, &num2);
if(Operator =='+'){
printf(" %d + %d = %d", num1, num2, num1+num2);
}
else if(Operator == '-'){
printf(" %d - %d = %d", num1, num2, num1-num2);
}
else if(Operator == '*'){
printf(" %d * %d = %d", num1, num2, num1*num2);
}
else{
printf("\t \a Invalid Operator.\n");
}
}
}
This line:
printf(" %d / %d", num1/num2);
The first '%d' is the result of num1/num2 and that's enough. The second %d and the '/' character should not be here. Change it to:
printf(" %d ", num1/num2);
Additionaly, for your purpose, the switch case structure is more suitable for code readability (and better optimization too I think)

how to repeat a c program from the beginning and clean the screen and 1st input values?

i m new in programing.
i've written a simple program.
i want to repeat the program again and again and it can only exit when user wants to exit.
here is my program
#include<stdio.h>
#include<conio.h>
main()
{
char ch;
int num1, num2, a, m, s, choice;
float d;
printf("\nEnter The First Number: ");
scanf("%d", &num1);
printf("\nEnter The Second Number: ");
scanf("%d", &num2);
a=num1+num2;
m=num1*num2;
s=num1-num2;
d=(float)(num1/num2);
printf("\nEnter Your Choice \nFor Addition Type A \nFor Multipication Type M \nFor Division Type D \nFor Substraction Type S : ");
scanf(" %c", &ch);
switch(ch)
{
case 'A': printf("\nThe Addition Of The Number Is= %d", a);
break;
case 'M': printf("\nThe Multipication Of The Numbers Is= %d", m);
break;
case 'S': printf("\nThe Substraction Of THe Numbers Is= %d", s);
break;
case 'D': printf("\nThe Division Of The Two Numbers Is= %f", d);
break;
default : printf("\nInvalid Entry");
break;
}
printf("\nPress Any Key To Exit");
getch();
return 0;
}
and here is the output
"Enter The First Number: 10
Enter The Second Number: 10
Enter Your Choice
For Addition Type A
For Multipication Type M
For Division Type D
For Substraction Type S : A
The Addition Of The Number Is= 20
Press Any Key To Exit"
I want a line before the line Press Any Key To Exit
"If You Want To Calculate Again Press Y
or
Press Any Key To Exit"
when press Y then the program should start from the beginning.
How can i do this???
wrap the code inside a do{} while() ?
char answer;
do{
printf("\nEnter The First Number: ");
scanf("%d", &num1);
printf("\nEnter The Second Number: ");
scanf("%d", &num2);
a=num1+num2;
m=num1*num2;
s=num1-num2;
d=(float)(num1/num2);
printf("\nEnter Your Choice \nFor Addition Type A \nFor Multipication Type M \nFor Division Type D \nFor Substraction Type S : ");
scanf(" %c", &ch);
switch(ch)
{
case 'A': printf("\nThe Addition Of The Number Is= %d", a);
break;
case 'M': printf("\nThe Multipication Of The Numbers Is= %d", m);
break;
case 'S': printf("\nThe Substraction Of THe Numbers Is= %d", s);
break;
case 'D': printf("\nThe Division Of The Two Numbers Is= %f", d);
break;
default : printf("\nInvalid Entry");
break;
}
printf("\nPress Y to continue. Press any Key To Exit");
scanf(" %c",&answer); // dont forget type &
}
while(answer == 'y' || answer == 'Y');
Declare a variable, let's say answer, which will store the user answer when you ask for "Press Y to continue. Press any Key To Exit". Check to see what value has that variable. If is 'y' or 'Y', the loop will repeat. If the user pressed other key, the loop is over.
You can also use recursion, which is often used in more functional oriented programming languages.
Pseudo-code:
myfunction = do
...
b <- somethingtodo
...
if b
then myfunction
else return ()
Relative to Jens's solution, it would look like:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main (void)
{
char choice;
int num1, num2, cont;
printf("Enter the first number: ");
scanf("%d", &num1);
getchar ();
printf("Enter the second number: ");
scanf("%d", &num2);
getchar ();
printf("A - addition\n");
printf("S - subtraction\n");
printf("M - multiplication\n");
printf("D - division\n");
printf("[ASMD]? ");
choice = (char)toupper(getchar());
getchar ();
printf("\n");
switch(choice)
{
case 'A':
printf("%d + %d = %d", num1, num2, num1 + num2);
break;
case 'S':
printf("%d - %d = %d", num1, num2, num1 - num2);
break;
case 'M':
printf("%d * %d = %d", num1, num2, num1 * num2);
break;
case 'D':
if (num2 == 0)
fprintf(stderr, "The divisor can not be zero");
else
{
printf("%d / %d = %f", num1, num2, (double)num1 / num2);
}
break;
default :
fprintf(stderr, "Invalid entry");
break;
}
printf("\n");
for (;;)
{
printf("Continue [YN]? ");
cont = toupper(getchar());
getchar ();
if (cont == 'Y')
return main(); // the difference.
else if (cont == 'N')
return EXIT_SUCCESS;
}
}
I would move the calculation stuff in it's own function and then use while() in main.
I have tried to fix other problems as well (this solution only uses standard C functions).
#include <stdio.h> // puts, printf, fprintf, scanf, getchar, stderr, EOF
#include <stdlib.h> // exit, EXIT_SUCCESS, EXIT_FAILURE
#include <ctype.h> // toupper
char fail_on_eof (int c)
{
if (c == EOF)
exit (EXIT_FAILURE);
// In case of fail_on_eof (scanf (...)) the return value of this this
// function is not useful
// scanf () returns the number of chars read or EOF
// getchar () returns a char or EOF
return (char) c;
}
void skip_to_next_line (void)
{
char c;
do
{
c = fail_on_eof (getchar ());
} while (c != '\n');
}
char read_upcase_char_line (char* prompt)
{
char c;
printf ("%s ", prompt);
c = fail_on_eof (toupper (getchar ()));
skip_to_next_line ();
return c;
}
int read_num_line (char* prompt)
{
int num;
printf ("%s ", prompt);
fail_on_eof (scanf ("%d", &num));
skip_to_next_line ();
return num;
}
int calculate (void)
{
char choice;
int num1, num2, cont;
num1 = read_num_line ("Enter the first number:");
num2 = read_num_line ("Enter the second number:");
puts("A - addition");
puts("S - subtraction");
puts("M - multiplication");
puts("D - division");
choice = read_upcase_char_line ("[ASMD]?");
puts("");
switch(choice)
{
case 'A':
printf("%d + %d = %d", num1, num2, num1 + num2);
break;
case 'S':
printf("%d - %d = %d", num1, num2, num1 - num2);
break;
case 'M':
printf("%d * %d = %d", num1, num2, num1 * num2);
break;
case 'D':
if (num2 == 0)
// Better use stderr for error messages
fprintf(stderr, "The divisor can not be zero");
else
{
printf("%d / %d = %f", num1, num2, (double)num1 / num2);
}
break;
default :
// Better use stderr for error messages
fprintf(stderr, "Invalid entry");
break;
}
printf("\n");
for (;;)
{
cont = read_upcase_char_line ("Continue [YN]?");
if (cont == 'Y')
return -1;
else if (cont == 'N')
return 0;
}
}
int main(void)
{
while (calculate ());
return EXIT_SUCCESS; // Use this constant to avoid platform specific issues with the return code
}

Resources