| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Can you help me with this calculator?
The calculator works fine. The problem comes when exiting. I get only 1 error, and that is (in function "int z1();" near the bottom), it says, "syntax error before '<<' token." Here's the code:
Code:
#include <iostream>
int addition ( int x, int y)
{
return x + y;
}
int subtraction ( int x, int y)
{
return x - y;
}
int multiplication ( int x, int y)
{
return x * y;
}
int division ( int x, int y)
{
return x / y;
}
int z1 ( int z)
{
return z + 1;
}
using namespace std;
int add();
int sub();
int mul();
int div();
int z1();
int main()
{
int input, z;
z = 0;
do {
cout<<"1. add\n2. subtract\n3. multiply\n4. divide\n5. exit\nEnter your choice: ";
cin>> input;
switch ( input ) {
case 1:
add();
break;
case 2:
sub();
break;
case 3:
mul();
break;
case 4:
div();
break;
case 5:
z1();
break;
default:
cout<<"Error\n";
break;
}
cin.get();
} while ( z != 1 );
}
int sub(){
int x, y;
cout<<"Enter the first number to subtract: ";
cin>> x;
cin.ignore();
cout<<"Enter the second number to subtract: ";
cin>> y;
cin.ignore();
cout<<"The difference is: "<<subtraction (x , y );
cin.get();
}
int add(){
int x, y;
cout<<"Enter the first number to add: ";
cin>> x;
cin.ignore();
cout<<"Enter the second number to add: ";
cin>> y;
cin.ignore();
cout<<"The sum is: "<<addition (x, y);
cin.get();
}
int mul(){
int x, y;
cout<<"Enter the first number to be multiplied: ";
cin>> x;
cin.ignore();
cout<<"Enter the second number to be multiplied: ";
cin>> y;
cin.ignore();
cout<<"The product is: "<<multiplication (x, y);
cin.get();
}
int div(){
int x, y;
cout<<"Enter the first number to be divided: ";
cin>> x;
cin.ignore();
cout<<"Enter the second number to be divided: ";
cin>> y;
cin.ignore();
cout<<"The quotiant is: "<<division (x, y);
cin.get();
}
int z1(){
int z;
<<z1 (z, 1);
cin.get();
}
I might have done something so blatantly obvious I can't see it. So please point it out to me. |
|
#2
|
|||
|
|||
|
The compiler is telling you it doesn't understand the line:
<<z1 (z, 1); Neither do I. What are you trying to do here? cout??? left shift??? |
|
#3
|
|||
|
|||
|
It's reasonable to assume cout...don't you think?
Quote:
|
|
#4
|
|||
|
|||
|
Trying to get it to add 1 to z so that it will exit out of the loop. Did I do it wrong? Okay, so that's a given, how would I do it right?
|
|
#5
|
|||
|
|||
|
Okay, I revised it, but, when you press 'E' it says error invalid input. . . And it still won't loop. I don't know what I'm doing, but can someone help me?
Code:
#include <iostream>
int addition ( int x, int y )
{
return x + y;
}
int subtraction ( int x, int y )
{
return x - y;
}
int multiplication ( int x, int y )
{
return x * y;
}
int division ( int x, int y )
{
return x / y;
}
using namespace std;
int add();
int sub();
int mul();
int div();
int main()
{
int input, z;
z = 0;
do
{
cout<<"1. Add\n2. Subtract\n3. Multiply\n4. Divide\nE. Exit\nEnter your choice: ";
cin>> input;
switch ( input ) {
case 1:
add();
break;
case 2:
sub();
break;
case 3:
mul();
break;
case 4:
div();
break;
case 'E':
z = ( z + 1 );
break;
default:
cout<<"Error: Unknown Input\n";
break;
}
return 0;
} while ( z != 1 );
}
int sub(){
int x, y;
cout<<"Enter the first number to subtract: ";
cin>> x;
cin.ignore();
cout<<"Enter the second number to subtract: ";
cin>> y;
cin.ignore();
cout<<"The difference is: "<<subtraction ( x , y );
cout<<"\n";
return 0;
}
int add(){
int x, y;
cout<<"Enter the first number to add: ";
cin>> x;
cin.ignore();
cout<<"Enter the second number to add: ";
cin>> y;
cin.ignore();
cout<<"The sum is: "<<addition ( x, y );
cout<<"\n";
return 0;
}
int mul(){
int x, y;
cout<<"Enter the first number to be multiplied: ";
cin>> x;
cin.ignore();
cout<<"Enter the second number to be multiplied: ";
cin>> y;
cin.ignore();
cout<<"The product is: "<<multiplication ( x, y );
cout<<"\n";
return 0;
}
int div(){
int x, y;
cout<<"Enter the dividend: ";
cin>> x;
cin.ignore();
cout<<"Enter the divisor: ";
cin>> y;
cin.ignore();
cout<<"The quotiant is: "<<division ( x, y );
cout<<"\n";
return 0;
}
|
|
#6
|
|||
|
|||
|
Is it ' E' or 'E' ? In former case it is giving valid error.
Secondly, your program won't stop looping due to execution of default case again and again.The condition of while loop will always be TRUE as z = 0. |
|
#7
|
|||
|
|||
|
It's not that bad...the loop would end if you, in fact, entered 'E'. The thing is C/C++ is case sensitive, so if you're entering e instead of E, you won't get the while to stop looping (it never enters that case)!!
Another thing: if you want to exit when z reaches 1, why not setting Z = 1 ? after all, you're not using that variable as a counter (there's no point doing z = z + 1). And finaly a question: what is that bloody return 0 doing inside the loop?? Good Luck!! Anibal. |
|
#8
|
|||
|
|||
|
Why not just make z a bool, set it equall to true before you enter the loop, then just set it false to exit the loop? Also, why not give it a descriptive name? Don't make this harder than it needs to be. And like Anibal said, get rid of the return 0 insdide the loop.
bool keep_looping = true; do { . . . case 'whatever your exit condition is': keep_looping = false; break; } while (keep_looping); |
|
#9
|
|||
|
|||
|
Thank you all so much. That damn return 0; I knew it should be lower, and the help with bool and all that. Thanks.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Can you help me with this calculator? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|