NiharG Posted April 25, 2012 Report Posted April 25, 2012 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 (); } Quote
bladefire Posted April 25, 2012 Report Posted April 25, 2012 instead of using 4 if loops, use switch case and put the last if statement in default Quote
NiharG Posted April 25, 2012 Author Report Posted April 25, 2012 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. Quote
Anindya1989 Posted April 25, 2012 Report Posted April 25, 2012 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. Quote
bladefire Posted April 25, 2012 Report Posted April 25, 2012 #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" Quote
NiharG Posted April 25, 2012 Author Report Posted April 25, 2012 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! Quote
bladefire Posted April 25, 2012 Report Posted April 25, 2012 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 (); } Quote
NiharG Posted April 25, 2012 Author Report Posted April 25, 2012 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. Quote
bladefire Posted April 25, 2012 Report Posted April 25, 2012 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?? Quote
NiharG Posted April 25, 2012 Author Report Posted April 25, 2012 @Blade: Its not working. when I input y and press enter, it does nothing. And if I press any key then, it exits. Quote
NiharG Posted April 25, 2012 Author Report Posted April 25, 2012 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. Quote
NiharG Posted April 25, 2012 Author Report Posted April 25, 2012 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. Quote
NiharG Posted April 25, 2012 Author Report Posted April 25, 2012 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. Quote
NiharG Posted April 25, 2012 Author Report Posted April 25, 2012 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? Quote
Anindya1989 Posted April 25, 2012 Report Posted April 25, 2012 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. Quote
NiharG Posted April 25, 2012 Author Report Posted April 25, 2012 Update: Putting && also does the job. Quote
NiharG Posted April 25, 2012 Author Report Posted April 25, 2012 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? Quote
Anindya1989 Posted April 25, 2012 Report Posted April 25, 2012 Query regarding your solution: If we put && instead of || , will the program work fine? YES . Quote
NiharG Posted April 25, 2012 Author Report Posted April 25, 2012 YES . Answer this please! http://forum.gizmolord.com/index.php?topic=4317.msg56122#msg56122 Quote
Anindya1989 Posted April 25, 2012 Report Posted April 25, 2012 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. Quote
NiharG Posted April 25, 2012 Author Report Posted April 25, 2012 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. Quote
bladefire Posted April 26, 2012 Report Posted April 26, 2012 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?? Quote
NiharG Posted April 26, 2012 Author Report Posted April 26, 2012 why..... is do while more preferable?? He explained that here:http://forum.gizmolord.com/index.php?topic=4317.msg56136#msg56136 Quote
Anindya1989 Posted April 26, 2012 Report Posted April 26, 2012 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. Quote
anurag Posted April 26, 2012 Report Posted April 26, 2012 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 Quote
Recommended Posts
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.