The purpose of my program is to take two numbers and an operator (+, -, *, /) from the user and perform the operation corresponding with the operator the user chose.
like a lame calculator.
but its not working.
The following code will not compile in Visual C++ 2005 Express v8:
Code:
#include <iostream>
using namespace std;
int main()
{
int firstNum, secNum;
char oper;
cout << "Please type in the first number" << endl;
cin >> firstNum;
cout << "Please type in the operation" << endl;
cin >> oper;
cout << "Please type in the second number" << endl;
cin >> secNum;
switch (oper)
{
case '+':
cout << firstNum << " + " << secNum " = " << firstNum + secNum << endl; //this is line 23
break;
case '-':
cout << firstNum << " - " << secNum " = " << firstNum - secNum << endl; //line 26
break;
case '*':
cout << firstNum << " * " << secNum " = " << firstNum * secNum << endl; //line 29
break;
case '/':
if (firstNum == 0 || secNum == 0)
{
cout << "Cannot divide into/divide by zero" << endl;
} else
{
cout << firstNum << " / " << secNum << " = " << firstNum / secNum << endl;
}
break;
default:
cout << "Invalid Operator" << endl;
}
return 0;
}
I get the following errors:
c:\learning\calulate\calulate\source.cpp(23) : error C2143: syntax error : missing ';' before 'string'
c:\learning\calulate\calulate\source.cpp(23) : warning C4554: '<<' : check operator precedence for possible error; use parentheses to clarify precedence
c:\learning\calulate\calulate\source.cpp(23) : error C2296: '<<' : illegal, left operand has type 'const char [4]'
c:\learning\calulate\calulate\source.cpp(26) : error C2143: syntax error : missing ';' before 'string'
c:\learning\calulate\calulate\source.cpp(26) : warning C4554: '<<' : check operator precedence for possible error; use parentheses to clarify precedence
c:\learning\calulate\calulate\source.cpp(26) : error C2296: '<<' : illegal, left operand has type 'const char [4]'
c:\learning\calulate\calulate\source.cpp(29) : error C2143: syntax error : missing ';' before 'string'
c:\learning\calulate\calulate\source.cpp(29) : error C2296: '<<' : illegal, left operand has type 'const char [4]'