hello,
i'm a beginner in c++ in terms of programming, though i have a bit of knowledge of OOP. i created this simple calculator progam, but it won't work. here's the code:
i use microsoft visual c++ express.
#include<iostream>
using namespace::std;
int multiply(int x, int y)
{
int product=x*y ;
return product;
}
int divide(int x, int y)
{
int quotient= x/y;
return quotient;
}
int add(int x, int y)
{
int sum =x+y;
return sum;
}
int subtract(int x, int y)
{
int difference=x-y;
return difference;
}
void intro()
{
cout<<"Hello! This is a simple calculator designed in C++.\n"
<<"Please enter your numbers one by one, and choose one of the options below.\n"
<<"[+] Add\n"
<<"[-] Subtract\n"
<<"[*] Multiply\n"
<<"[/] Divide\n"
<<"[0] Exit calc\n";
}
int main(void)
{
int a,b;
char choice;
intro();
cin>>choice;
while (choice!=0)
{
switch (choice)
{
case '+':
cout<<"Please enter two numbers to add:";
cin>>a>>b;
cout<<add(a,b)<<endl;
break;
case '-':
cout<<"\nPlease enter two numbers to subtract:";
cin>>a>>b;
cout<<subtract(a,b)<<endl;
break;
case '*':
cout<<"\nPlease enter two numbers to multiply:";
cin>>a>>b;
cout<<multiply(a,b)<<endl;
break;
case '/':
cout<<"\nPlease enter two numbers to divide";
cin>>a>>b;
cout<<divide(a,b)<<endl;
break;
/*default:
cout<<"The character you entered "<<"("<<choice<<") is invalid.\n"
<<"Please enter a valid option!\n";
*/
}
}
return 0;
}
when you enter '0', the program exits. but when you enter the desired operator(+,=* or /), the program gives you the answer but keeps on repeatin g the same operation.for example,if i entered +,and then 2 numbers, it would give the answer and again say "Please enter two numbers to add". you could paste the code in your compiler to find out.
please help me with this!!! you could also suggest any improvements. thanks a lot.
Really? I get an infinite loop. The problem is this line while (choice!=0). It should be choice != '0'. Otherwise, you're checking for the null terminator character which can't be inputted directly with a standard keyboard.
but when you enter the desired operator(+,=* or /), the program gives you the answer but keeps on repeatin g the same operation
That's because cin>>choice; is outside the loop. The program goes into the loop, and while in there, it never asks the user for a new choice.
#include<iostream>
usingnamespace::std;
int multiply(int x, int y)
{
int product=x*y ;
return product;
}
int divide(int x, int y)
{
int quotient= x/y;
return quotient;
}
int add(int x, int y)
{
int sum =x+y;
return sum;
}
int subtract(int x, int y)
{
int difference=x-y;
return difference;
}
void intro()
{
cout<<"Hello! This is a simple calculator designed in C++.\n"
<<"Please enter your numbers one by one, and choose one of the options below.\n"
<<"[+] Add\n"
<<"[-] Subtract\n"
<<"[*] Multiply\n"
<<"[/] Divide\n"
<<"[0] Exit calc\n";
}
int main(void)
{
int a,b;
char choice;
intro();
cin>>choice;
while (choice!=0)
{
switch (choice)
{
case'+':
cout<<"Please enter two numbers to add:";
cin>>a>>b;
cout<<add(a,b)<<endl;
break;
case'-':
cout<<"\nPlease enter two numbers to subtract:";
cin>>a>>b;
cout<<subtract(a,b)<<endl;
break;
case'*':
cout<<"\nPlease enter two numbers to multiply:";
cin>>a>>b;
cout<<multiply(a,b)<<endl;
break;
case'/':
cout<<"\nPlease enter two numbers to divide";
cin>>a>>b;
cout<<divide(a,b)<<endl;
break;
/*default:
cout<<"The character you entered "<<"("<<choice<<") is invalid.\n"
<<"Please enter a valid option!\n";
*/
}
}
return 0;
}
At the row 50 you enter loop which will never to be ended since you don't ask from the user would they continue program or not.
Look at your program closely and think what happens AFTER first round of while loop is executed ;)
(btw... USE TABS!)
hey guys, sorry for the late reply. i was on vacation. thanks for your replies!
now i've changed the 0 in line 50 to '0', but the progrm still gets stuck into the same infinite loop.
At the row 50 you enter loop which will never to be ended since you don't ask from the user would they continue program or not.
Look at your program closely and think what happens AFTER first round of while loop is executed ;)
thanks a lot! i now understand the mistake fully. but how do i repair it? should i use 2 variables? or is there any way i can reset the value of 'choice'?
#include<iostream>
using namespace std;
int add(int x, int y);
int subtract(int x, int y);
int multiply(int x, int y);
int divide(int x, int y);
void intro();
int main(void)
{
int primary, secondary;
char choice;
primary=0;
intro();
cin>>choice;
A:switch(choice)
{
case ‘+’:
cin>>secondary;
add(primary, secondary);
primary += secondary;
cin>>choice;
goto A;
break;