Jump to content
Sign in to follow this  
NiharG

Simple Two-number calculator in C++

Recommended Posts

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 ();
}

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

#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"

Share this post


Link to post
Share on other sites

Thanks for the reply but I did this already:

(Just saw the syntax on the net and figured rest out myself :D)

#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!

Share this post


Link to post
Share on other sites

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 ();
}

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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??

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...