Skip to content
View in the app

A better way to browse. Learn more.

IPS Gizmolord Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Simple Two-number calculator in C++

Featured Replies

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

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.

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.

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

  • Author

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!

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

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.

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

  • Author

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.
  • Author

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.

  • Author

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.
  • Author

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?

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.

  • Author

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?

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.

  • Author

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.

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

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.

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.

Guest
Reply to this topic...

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.