When I run this program, it ignores everything in the switch statement, and displays the default.
I am using Code::Blocks.
Please explain to me why this logic error is encountered and maybe it is an error in Code::Blocks and not from the code — should I try it on another IDE?
#include <stdio.h>
int main()
{
double a,b;
int choose,subtract,divide;
printf("My first Calculater in c\n");
printf("\n");
printf("Enter your two numbers\n");
scanf("%lf",&a);
scanf("%lf",&b);
printf("Please choose an operation\n");
printf("1.add two no.(s)\n");
printf("2.subtract two no.(s)\n");
printf("3.multiply two no.(s)\n");
printf("4.divide two no.(s)\n");
printf("please choose an operation ");
scanf("%lf", &choose);
switch(choose){
case 1:
printf("sum of a+b is : %lf\n",(a+b));
break;
case 2:
printf("1.a-b?\n");
printf("2.b-a?\n");
scanf("%d", &subtract);
switch(subtract)
{
case 1:
printf("\ndiff of a-b is : %lf",(a-b));
break;
case 2:
printf("\ndiff of b-a is : %lf",(b-a));
break;
}
case 3:
printf("\nproduct of a*b is : %lf",(a*b));
break;
case 4:
printf("\n1.divide a/b?,a!=0");
printf("\n2.divide b/a?,b!=0");
scanf("%d", ÷);
switch(divide)
{
case 1:
if(b==0)
printf("\ndivision by zero is undefined");
else
printf("\nquotient of a/b is : %lf",(a/b));
break;
case 2:
if(a==0)
printf("\ndivision by zero is undefined");
else
printf("\nquotient of b/a is : %lf",(b/a));
break;
}
default:
printf("please choose either 1,2,3 or 4 options\n");
}
}
You are reading in a double for choose
scanf("%lf", &choose);
Change to reading a decimal
scanf("%d", &choose);
Related
Using just the switch statement works totally fine. My problem comes when inserting the do-while loop. On the first try, the program works smoothly, but in the second and following loops, after asking the user if he wants to try again by typing 1, the program "jumps" the part where the user writes its input (scanf("%d", &oper)) and executes the rest of the program considering the previously typed 1 as the input and messing up everything.
I´ve tried looking for the answer on similar questions and videos, but I´m quite new and I just can´t grasp it. I have another program with a similar problem. I will really appreciate your help :)
#include <stdio.h>
int main()
{
int num1, num2, oper, select;
printf("Enter the first number:");
scanf("%d", &num1);
printf("Enter the second number:");
scanf("%d", &num2);
printf("Enter the number 1-5 to for the operations\n");
printf("1-Addition\n");
printf("2-Subtraction\n");
printf("3-Division\n");
printf("4-Multiplication\n");
printf("5-Modulo\n");
scanf("%d", &oper);
do{
printf("Press 0 to stop and 1 to continue");
scanf("%d", &select);
switch(oper){
case 1:
printf("The sum is %d\n", num1+num2);
break;
case 2:
printf("The difference is %d\n", num1-num2);
break;
case 3:
printf("The quotient is %d\n", num1/num2);
break;
case 4:
printf("The product is %d\n", num1*num2);
break;
case 5:
printf("The remainder is %d\n", num1%num2);
break;}
}while(select == 1);
} ```
I think you want something like bellow code.
Try this:
#include <stdio.h>
int main()
{
int num1, num2, oper, select;
do{
printf("Enter the first number:");
scanf("%d", &num1);
printf("Enter the second number:");
scanf("%d", &num2);
printf("Enter the number 1-5 to for the operations\n");
printf("1-Addition\n");
printf("2-Subtraction\n");
printf("3-Division\n");
printf("4-Multiplication\n");
printf("5-Modulo\n");
scanf("%d", &oper);
switch(oper){
case 1:
printf("The sum is %d\n", num1+num2);
break;
case 2:
printf("The difference is %d\n", num1-num2);
break;
case 3:
printf("The quotient is %d\n", num1/num2);
break;
case 4:
printf("The product is %d\n", num1*num2);
break;
case 5:
printf("The remainder is %d\n", num1%num2);
break;
}
printf("Press 0 to stop and 1 to continue: ");
scanf("%d", &select);
} while(select == 1);
printf("Finish");
}
change the following code bellow:
while(select != 0);
Try this:
#include <stdio.h>
int main()
{
int num1, num2, oper, select;
printf("Enter the first number:");
scanf("%d", &num1);
printf("Enter the second number:");
scanf("%d", &num2);
printf("Enter the number 1-5 to for the operations\n");
printf("1-Addition\n");
printf("2-Subtraction\n");
printf("3-Division\n");
printf("4-Multiplication\n");
printf("5-Modulo\n");
scanf("%d", &oper);
do{
printf("Press 0 to stop and 1 to continue");
scanf("%d", &select);
switch(oper){
case 1:
printf("The sum is %d\n", num1+num2);
break;
case 2:
printf("The difference is %d\n", num1-num2);
break;
case 3:
printf("The quotient is %d\n", num1/num2);
break;
case 4:
printf("The product is %d\n", num1*num2);
break;
case 5:
printf("The remainder is %d\n", num1%num2);
break;
}
}while(select != 0);
}
I am trying this code with exit case, the exit case doesn't work it still asks me for the two numbers and then it exits. I have provided the output. What is the alternative to the switch case that I can use for menu driven programs?
Sample input/output:
::::::Menu:::::
1. Addition:
2. Subtraction
3. Multiplication
4. Division
5. Mod
6. Exit
Enter your choice:6
Enter the values of a and b:3 4
#include<stdio.h>
#include<stdlib.h>
int main(){
int a,b,c;
int no;
do{
printf("\n::::::Menu:::::\n");
printf(" 1. Addition:\n 2. Subtraction \n 3. Multiplication \n 4. Division \n 5. Mod \n 6. Exit");
printf("\nEnter your choice:");
scanf("%d",&no);
printf("\nEnter the values of a and b:");
scanf("%d%d",&a,&b);
switch(no){
case 1:
c = a + b;
printf("\nAddition is:%d",c);
break;
case 2:
c = a - b;
printf("\nSubtraction is:%d",c);
break;
case 3:
c = a * b;
printf("\nMultiplication is:%d",c);
break;
case 4:
c = a / b;
printf("\nDivision is:%f",c);
break;
case 5:
c = a % b;
printf("\nMod is:%d",c);
break;
case 6:
exit(1);
default:
printf("\nInvalid choice\n");
break;
}
}while(no!=6);
return 0;
}
Your program asks for two numbers because you are checking for the exit code after the second scanf statement. If you wish the program to exit when 6 is entered, then you have to add an if statement in between the first and second scanf. You should additionally removed the exit case from your switch statement. Below is an example:
printf("\nEnter your choice:");
scanf("%d",&no);
if (no == 6)
exit(0);
printf("\nEnter the values of a and b:");
scanf("%d%d",&a,&b);
You're reading the user's menu option, then asking for the next two numbers before you get to the switch statement, so of course it will always ask for those two numbers.
You need to either check specifically for 6 right after reading the menu input, or you need to move the second prompts to only where they're needed, i.e. inside each case.
It keeps asking for the two numbers because the switch statement is positioned right AFTER you ask for the user input. I restructured your code, this should work:
#include<stdio.h>
#include<stdlib.h>
int main(){
int a,b,c;
int no;
while(1){
printf("\n::::::Menu:::::\n");
printf(" 1. Addition:\n 2. Subtraction \n 3. Multiplication \n 4. Division \n 5. Mod \n 6. Exit");
printf("\nEnter your choice:");
scanf("%d",&no);
if (no == 6)
break;
printf("\nEnter the values of a and b:");
scanf("%d%d",&a,&b);
switch(no){
case 1:
c = a + b;
printf("\nAddition is:%d",c);
break;
case 2:
c = a - b;
printf("\nSubtraction is:%d",c);
break;
case 3:
c = a * b;
printf("\nMultiplication is:%d",c);
break;
case 4:
c = a / b;
printf("\nDivision is:%f",c);
break;
case 5:
c = a % b;
printf("\nMod is:%d",c);
break;
default:
printf("\nInvalid choice\n");
break;
}
}
return 0;
}
Note how I changed the do-while to a while-true and explicitly check for no == 6 PRIOR to asking for user input.
The statements in C are meant to be executed sequentially unless you disrupt the normal flow of action using jumps. In your case
printf("\nEnter your choice:");
scanf("%d",&no);
printf("\nEnter the values of a and b:");
scanf("%d%d",&a,&b);
// Only now does the switch start.
You ask for the choice first and then for the two numbers. That is why you always end up in entering the two values. One way is to take the exit out of the switch-case which perhaps is the simplest solution I guess. Something like
printf("\nEnter your choice:");
scanf("%d",&no);
if ( 6 == no )
exit(1);
What is the alternative to the switch case that I can use for menu
driven programs
Well, switch-case is exactly made for that. Why think of an alternative?
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
int main()
{
char ch;
while(1)
{
printf("l.print l c.print c q. exit \n");
printf("enter choice ");
scanf("%c",&ch);
switch(ch)
{
case 'l':
printf("You have typed l \n");
break;
case 'c':
printf("yoh have typed c \n");
break;
case 'q':
exit(0);
}
}
return 0;
}
Why my program is giving garbage value in O/P after providing sufficient inputs?
I have given I/P as 10 & 40 & choose multiplication option as 3.
My code is as follows:
int main()
{
int a,b,c,x;
printf("Enter a & b \n"); //printing
scanf("%d %d, &a,&b");
printf("1. add \n 2. sub \n 3. multiply \n 4. div \n 5. mod \n 6. and \n 7. or\n 8. not \n 9. xor \n");
printf("Enter your choice \n");
scanf("%d, &x");
switch(x)
{
case 1: c=a+b;
break;
case 2: c=a-b;
break;
case 3: c=a*b;
break;
case 4: c=a/b;
break;
case 5: c=a%b;
break;
case 6: c=a && b;
break;
case 7: c=a || b;
break;
case 8: c=~a;
break;
case 9: c=a^b;
break;
default: printf("Make correct choice\n");
}
printf("result is: %d",c);
return 0;
}
You are passing a string to scanf function in line number 5 & 8 and no buckets to hold the scanned/read arguments.
The scanf function's prototype is as follows:
int scanf(const char *format_string, ...);
Where the format_string is string containing format specifiers such as %d, %f...etc
And "..." in prototype means variable number of arguments. The arguments are buckets to hold the values that scanf fills by reading the format_string.
Hence the corrected lines of code should be
line 5. scanf("%d %d", &a,&b);
line 8. scanf("%d", &x);
Another notewothy thing is the variable arguments for scanf are always pointers to objects.
Syntax of scanf function is
scanf(“format string”, argument list);
So for example, change
scanf("%d %d, &a,&b");
scanf("%d, &x");
to
scanf("%d %d", &a,&b);
scanf("%d", &x);
first of all your input nothing to be scan from your console or any other input.
please check scanf() sysntax.
#include<stdio.h>
int main()
{
int a,b,c,x;
printf("Enter a & b \n"); //printing
scanf("%d %d", &a, &b);
printf("1. add \n 2. sub \n 3. multiply \n 4. div \n 5. mod \n 6. and \n 7. or\n 8. not \n 9. xor \n");
printf("Enter your choice \n");
scanf("%d", &x);
switch(x)
{
case 1: c=a+b;
break;
case 2: c=a-b;
break;
case 3: c=a*b;
break;
case 4: c=a/b;
break;
case 5: c=a%b;
break;
case 6: c=a && b;
break;
case 7: c=a || b;
break;
case 8: c=~a;
break;
case 9: c=a^b;
break;
default: printf("Make correct choice\n");
}
printf("result is: %d",c);
return 0;
}
I'm making this program using switch statements that will assign letter grades based on if the user enters numbers 0 - 10. If the user enters a number that is not 0-10, the program outputs an error message and has the user re-enter. However, if the user enters a character the program will loop at the default case. I want it to output the error message from the default case once, and have them re-enter if they enter a character. I'm not sure as to why it loops the default case when a character is entered though.
#include <stdio.h>
int main()
{
int grade;
int r;
while((r = scanf("%i", &grade)) != EOF)
{
switch(grade)
{
case 10:
case 9:
printf("Your grade is an A\n");
break;
case 8:
printf("Your grade is a B\n");
break;
case 7:
printf("Your grade is a C\n");
break;
case 6:
printf("Your grade is a D\n");
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
printf("Your grade is an F\n");
break;
default:
printf("Invalid score, please re-enter\n");
}
}
return 0;
}
Try something like:
#include <stdio.h>
int main()
{
int grade;
int r=0;
while(r != 1)
{
scanf("%i", &grade);
switch(grade)
{
case 10:
case 9:
printf("Your grade is an A\n");
r=1
break;
case 8:
printf("Your grade is a B\n");
r=1
break;
case 7:
printf("Your grade is a C\n");
r=1
break;
case 6:
printf("Your grade is a D\n");
r=1
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
printf("Your grade is an F\n");
r=1
break;
default:
printf("Invalid score, please re-enter\n");
break;
}
}
return 0;
}
This will clear the input buffer on an invalid input and allow a retry.
#include <stdio.h>
int main()
{
int grade;
int r;
while((r = scanf("%i", &grade)) != EOF)
{
if ( r != 1) {//r == 1 is successful input of integer
grade = -1;//reset grade on invalid input
}
switch(grade)
{
case 10:
case 9:
printf("Your grade is an A\n");
break;
case 8:
printf("Your grade is a B\n");
break;
case 7:
printf("Your grade is a C\n");
break;
case 6:
printf("Your grade is a D\n");
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
printf("Your grade is an F\n");
break;
default:
printf("Invalid score, please re-enter\n");
while ( getchar() != '\n');//clear input buffer
}
}
return 0;
}
The reason your code always loops is because there is no way to exit out of your while other than to kill the program. Remember that break only breaks out of the inner-most switch or loop.
The cleanest way to break out of multiple levels is to use a flag. One way to do what you want is like this:
bool valid_grade = false;
while(!valid_grade && (r = scanf("%i", &grade)) != EOF)
{
valid_grade = true;
switch(grade)
{
case 10:
// unchanged from your code
default:
valid_grade = false;
printf("Invalid score, please re-enter\n");
}
}
void menu(){
printf("\n");
printf("1. Convert integers in decimal number system to binary numbers \n");
printf("2. Compute a consecutive square root expression \n");
printf("3. Solve a quadratic equation \n");
printf("4. Print something fun \n");
printf("q. Quit\n \n");
printf(" Enter your choice: ");
}
main () {
char choice;
do {
menu();
scanf("%c", &choice);
switch (choice){
case '1':
...
case '2':
....
case '3':
...
case '4':
....
default:
printf("Wrong choice. Please enter again: ");
break;
}
}
while (choice != 'q');
}
Here is my general idea, but I can't get it to prompt the wrong choice and repeat the menu. When I enter a wrong choice, the output is as follows:
For example, I entered 5:
Enter your choice: 5
Wrong choice, please enter again:
1. Convert integers in decimal number system to binary numbers
2. Compute a consecutive square root expression
3. Solve a quadratic equation
4. Print something fun
q. Quit
Enter your choice: (this is where I get to input)
Take a look at the below changes:
Change your scanf() as
scanf(" %c",&choice);
A space before the %c will make sure all special characters including newline is ignored.Without this everytime there is a newline in the buffer to and scanf reads from it and you will see that your look doesn't work as expected.
After this please make sure once the default case is hit you need to break from the while() loop.
do {
menu();
scanf(" %c", &choice);
switch (choice){
case '1':
break;
case '2':
break;
case '3':
break;
case '4':
break;
default:
{
printf("Wrong choice. Please enter again: ");
break;
}
}
}
while (choice != 'q');
First thing, Put break; in each case so only the case you choose will apply. To solve the printing 2 times issue, just Change the %c in scanf("%c", &choice); to %s >> scanf("%s", &choice);
This is the code:
#include <stdio.h>
void menu(){
printf("\n");
printf("1. Convert integers in decimal number system to binary numbers \n");
printf("2. Compute a consecutive square root expression \n");
printf("3. Solve a quadratic equation \n");
printf("4. Print something fun \n");
printf("q. Quit\n \n");
printf(" Enter your choice: ");
}
main () {
char choice;
do {
menu();
scanf("%s", &choice);
switch (choice){
case '1':
printf("1 \n");
break;
case '2':
printf("2 \n");
break;
case '3':
printf("3 \n");
break;
case '4':
printf("4 \n");
break;
case 'q':
break;
default:
printf("Wrong choice. Please enter again: ");
break;
}
}
while (choice != 'q');
}