Posted April 25, 201212 yr comment_31774 Hey! I have made this simple calculator in C++. But it has one bug. When we input a valid choice, It still prints invalid choice after printing the output. If we remove the last if statement and invalid choice part the program works fine. Please tell me whats wrong. #include<iostream.h> #include<conio.h> signed long int x, y, choice; int sum () { cout<<"The sum is "<<x+y; } int mul () { cout<<"The product is "<<x*y; } int sub () { if(x>y) cout<<"The subtraction of smaller number from the greater is "<<x-y; if (x<y) cout<<"The subtraction of smaller number from greater number is "<<y-x; } int div () { if (x>y) cout<<"Division of greater number by smaller. Quotient: "<<x/y<<"\tRemainder: "<<x%y; if (x<y) cout<<"Division of greater number by smaller. Quotient: "<<y/x<<"\tRemainder: "<<y%x; } int main () { cout<<"Enter two numbars. \n"; cout<<"First: "; cin>>x; cout<<"Second: "; cin>>y; cout<<"What operation to do?"<<endl; cout<<"1. Addition"<<endl; cout<<"2. Multiplication"<<endl; cout<<"3. Subtration"<<endl; cout<<"4. Division"<<endl; cout<<"Enter the number of operation: "; cin>>choice; if (choice==1) sum(); if (choice==2) mul(); if (choice==3) sub(); if (choice==4) div(); if (choice!=1 || choice!=2 || choice!=3 || choice!=4) cout<<"Invalid Choice"; getch (); }
April 25, 201212 yr comment_31785 instead of using 4 if loops, use switch case and put the last if statement in default
April 25, 201212 yr Author comment_31787 instead of using 4 if loops, use switch case and put the last if statement in default I'm not that great at C++ dude. Please elaborate or atleast give an example.
April 25, 201212 yr comment_31789 Yes there is a problem in your logic. if (choice!=1 || choice!=2 || choice!=3 || choice!=4) cout<<"Invalid Choice"; As per your logic if the choice is 2 then it will do the multiplication and in the enter this last if condition also because you have given OR condition. choice=2 satisfies choice!=1,choice!=3 and choice!=4. So whenever you enter a valid choice the program enters this last if condition also. The proper way of writing the program will be to either replace the OR's with ANDs. Or you can get rid of the last if condition altogether and write-- if (choice==1) sum(); else if (choice==2) mul(); else if (choice==3) sub(); else if (choice==4) div(); else cout<<"Invalid Choice"; You can replace these if-else statements with switch-case also.
April 25, 201212 yr comment_31790 #include<iostream> #include<conio.h> using namespace std; signed long int x, y, choice; int sum () { cout<<"The sum is "<<x+y; } int mul () { cout<<"The product is "<<x*y; } int sub () { if(x>y) cout<<"The subtraction of smaller number from the greater is "<<x-y; if (x<y) cout<<"The subtraction of smaller number from greater number is "<<y-x; } int div () { if (x>y) cout<<"Division of greater number by smaller. Quotient: "<<x/y<<"\tRemainder: "<<x%y; if (x<y) cout<<"Division of greater number by smaller. Quotient: "<<y/x<<"\tRemainder: "<<y%x; } int main () { cout<<"Enter two numbars. \n"; cout<<"First: "; cin>>x; cout<<"Second: "; cin>>y; cout<<"What operation to do?"<<endl; cout<<"1. Addition"<<endl; cout<<"2. Multiplication"<<endl; cout<<"3. Subtration"<<endl; cout<<"4. Division"<<endl; cout<<"Enter the number of operation: "; cin>>choice; switch (choice) { case 1 : sum(); break; case 2 : mul(); break; case 3 : sub(); break; case 4 : div(); break; default : cout<< " Invalid Choice "; } getch (); } just remove "using namespace std"
April 25, 201212 yr Author comment_31791 Thanks for the reply but I did this already: (Just saw the syntax on the net and figured rest out myself ) #include<iostream.h> #include<conio.h> signed long int x, y, choice; int sum () { cout<<"The sum is "<<x+y; } int mul () { cout<<"The product is "<<x*y; } int sub () { if(x>y) cout<<"The subtraction of smaller number from the greater is "<<x-y; if (x<y) cout<<"The subtraction of smaller number from greater number is "<<y-x; } int div () { if (x>y) cout<<"Division of greater number by smaller. Quotient: "<<x/y<<"\tRemainder: "<<x%y; if (x<y) cout<<"Division of greater number by smaller. Quotient: "<<y/x<<"\tRemainder: "<<y%x; } int main () { cout<<"Enter two numbars. \n"; cout<<"First: "; cin>>x; cout<<"Second: "; cin>>y; cout<<"What operation to do?"<<endl; cout<<"1. Addition"<<endl; cout<<"2. Multiplication"<<endl; cout<<"3. Subtration"<<endl; cout<<"4. Division"<<endl; cout<<"Enter the number of operation: "; cin>>choice; switch (choice) { case 1: sum(); break; case 2: mul (); break; case 3: sub (); break; case 4: div(); break; default: cout<<"Invalid Choice"; break; } getch (); } Thanks for your help, buddy!
April 25, 201212 yr comment_31792 i have an idea. why don't you put a goto after the switch case to take the user back to the point it asks for values (a more practical way) like this #include<iostream> #include<conio.h> using namespace std; signed long int x, y, choice; int sum () { cout<<"The sum is "<<x+y; } int mul () { cout<<"The product is "<<x*y; } int sub () { if(x>y) cout<<"The subtraction of smaller number from the greater is "<<x-y; if (x<y) cout<<"The subtraction of smaller number from greater number is "<<y-x; } int div () { if (x>y) cout<<"Division of greater number by smaller. Quotient: "<<x/y<<"\tRemainder: "<<x%y; if (x<y) cout<<"Division of greater number by smaller. Quotient: "<<y/x<<"\tRemainder: "<<y%x; } int main () { char ch; main: cout<<"Enter two numbars. \n"; cout<<"First: "; cin>>x; cout<<"Second: "; cin>>y; cout<<"What operation to do?"<<endl; cout<<"1. Addition"<<endl; cout<<"2. Multiplication"<<endl; cout<<"3. Subtration"<<endl; cout<<"4. Division"<<endl; cout<<"Enter the number of operation: "; cin>>choice; switch (choice) { case 1 : sum(); break; case 2 : mul(); break; case 3 : sub(); break; case 4 : div(); break; default : cout<< " Invalid Choice "; } cout<< " Do you want to do another calculation (y/n) : "; cin>> ch; if (ch == 'y' || ch == 'Y') { goto main; } getch (); }
April 25, 201212 yr Author comment_31793 i have an idea. why don't you put a goto after the switch case to take the user back to the point it asks for values (a more practical way) like this Thanks for the suggestion. I'm implementing it.
April 25, 201212 yr comment_31794 Thanks for the suggestion. I'm implementing it. your welcome. btw, i had made some code snippets(like a password taker which showed stars instead of the characters) for my database project last month should i post them here??
April 25, 201212 yr Author comment_31797 @Blade: Its not working. when I input y and press enter, it does nothing. And if I press any key then, it exits.
April 25, 201212 yr Author comment_31798 your welcome. btw, i had made some code snippets(like a password taker which showed stars instead of the characters) for my database project last month should i post them here?? Surely post them. They'll help other users.
April 25, 201212 yr Author comment_31800 Update: The goto statement working fine. I forgot to put Y and y in '' in the if statement. Now its working fine. Thanks for the idea once again.
April 25, 201212 yr Author comment_31802 Yes there is a problem in your logic. As per your logic if the choice is 2 then it will do the multiplication and in the enter this last if condition also because you have given OR condition. choice=2 satisfies choice!=1,choice!=3 and choice!=4. So whenever you enter a valid choice the program enters this last if condition also. The proper way of writing the program will be to either replace the OR's with ANDs. Or you can get rid of the last if condition altogether and write-- You can replace these if-else statements with switch-case also. Thank for help bro.
April 25, 201212 yr Author comment_31803 Yes there is a problem in your logic. if (choice!=1 || choice!=2 || choice!=3 || choice!=4) cout<<"Invalid Choice"; As per your logic if the choice is 2 then it will do the multiplication and in the enter this last if condition also because you have given OR condition. choice=2 satisfies choice!=1,choice!=3 and choice!=4. So whenever you enter a valid choice the program enters this last if condition also. The proper way of writing the program will be to either replace the OR's with ANDs. Or you can get rid of the last if condition altogether and write-- You can replace these if-else statements with switch-case also. Query regarding your solution: If we put && instead of || , will the program work fine?
April 25, 201212 yr comment_31805 i have an idea. why don't you put a goto after the switch case to take the user back to the point it asks for values (a more practical way) like this #include<iostream> #include<conio.h> using namespace std; signed long int x, y, choice; int sum () { cout<<"The sum is "<<x+y; } int mul () { cout<<"The product is "<<x*y; } int sub () { if(x>y) cout<<"The subtraction of smaller number from the greater is "<<x-y; if (x<y) cout<<"The subtraction of smaller number from greater number is "<<y-x; } int div () { if (x>y) cout<<"Division of greater number by smaller. Quotient: "<<x/y<<"\tRemainder: "<<x%y; if (x<y) cout<<"Division of greater number by smaller. Quotient: "<<y/x<<"\tRemainder: "<<y%x; } int main () { char ch; main: cout<<"Enter two numbars. \n"; cout<<"First: "; cin>>x; cout<<"Second: "; cin>>y; cout<<"What operation to do?"<<endl; cout<<"1. Addition"<<endl; cout<<"2. Multiplication"<<endl; cout<<"3. Subtration"<<endl; cout<<"4. Division"<<endl; cout<<"Enter the number of operation: "; cin>>choice; switch (choice) { case 1 : sum(); break; case 2 : mul(); break; case 3 : sub(); break; case 4 : div(); break; default : cout<< " Invalid Choice "; } cout<< " Do you want to do another calculation (y/n) : "; cin>> ch; if (ch == 'y' || ch == 'Y') { goto main; } getch (); } USING GOTO STATEMENTS IS PUNISHABLE BY DEATH!! Using goto statements is an offence that can get you fired from any software development company.
April 25, 201212 yr Author comment_31808 USING GOTO STATEMENTS IS PUNISHABLE BY DEATH!! Using goto statements is an offence that can get you fired from any software development company. Why? Any bugs?
April 25, 201212 yr comment_31810 Query regarding your solution: If we put && instead of || , will the program work fine? YES .
April 25, 201212 yr Author comment_31811 YES . Answer this please! http://forum.gizmolord.com/index.php?topic=4317.msg56122#msg56122
April 25, 201212 yr comment_31822 Why? Any bugs? No there aren't any bugs but it gives rise to a lot of problems later on. After a software is made it has to maintained and enhanced. Bugs have to removed, improvements and new features have to be to be added and the software has to be tweaked from time to time as per the clients changing business needs. So after a software is developed it has to be maintained by a separate group of people. Since goto is an unconditional program branching the maintenance guy reading your code might not understand why you have used it in your program. So in any software development company the goto statement is banned. Only conditional branching is allowed.
April 25, 201212 yr Author comment_31827 No there aren't any bugs but it gives rise to a lot of problems later on. After a software is made it has to maintained and enhanced. Bugs have to removed, improvements and new features have to be to be added and the software has to be tweaked from time to time as per the clients changing business needs. So after a software is developed it has to be maintained by a separate group of people. Since goto is an unconditional program branching the maintenance guy reading your code might not understand why you have used it in your program. So in any software development company the goto statement is banned. Only conditional branching is allowed. Thanks for the info! +1 for your help.
April 26, 201212 yr comment_32022 USING GOTO STATEMENTS IS PUNISHABLE BY DEATH!! Using goto statements is an offence that can get you fired from any software development company. why..... is do while more preferable??
April 26, 201212 yr Author comment_32028 why..... is do while more preferable?? He explained that here:http://forum.gizmolord.com/index.php?topic=4317.msg56136#msg56136
April 26, 201212 yr comment_32055 why..... is do while more preferable?? Yes a simple while loop or a do-while loop. Any conditional loop basically so that any other person reading your code may understand what logic you have implemented. Unconditional branch statements like goto are strictly prohibited.
April 26, 201212 yr comment_32057 USING GOTO STATEMENTS IS PUNISHABLE BY DEATH!! Using goto statements is an offence that can get you fired from any software development company. lol why, i sometimes use them to get out of tight spots
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.