Skip to main content
GameDev.net gamedev.net
🔒 Locked

Math operation can't be used in Case statement.

Started by Assassin7257 Jan 27, 2011 at 2:41 AM 2 replies 1.7k views
Original Post
Assassin7257
Assassin7257

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
double num1;
double num2;
char operation;
char again;

do
{
cout << "Enter math expression:\n";
cin >> num1 >> operation >> num2;

switch (operation);
{
case '+':
cout << "=" (num1 + num2) << endl;
break;
case '-':
cout << "=" (num1 - num2) << endl;
break;
case '*':
cout << "=" (num1 * num2) << endl;
break;
case '/':
cout << "=" (num1 / num2) << endl;
break;
}
cout << "Do you want to try again <y or n>\n";
cin >> again;
}while (again == 'y' || again == 'Y')

system("pause");
return 0;

}


I'm trying to make a calculator but I can't you use math expression in switch statement.
MarkS_
MarkS_
You had several issues.

1.) You put a semicolon after the switch statement.
2.) You forgot the << between the "=" and the (num1 .. num2).
3.) You forgot the semicolon after the while statement.

Works fine now...


#include <iostream>
#include <cmath>
using namespace std;

int main()
{
double num1;
double num2;
char operation;
char again;

do
{
cout << "Enter math expression:\n";
cin >> num1 >> operation >> num2;

switch (operation)
{
case '+':
cout << "=" << (num1 + num2) << endl;
break;
case '-':
cout << "=" << (num1 - num2) << endl;
break;
case '*':
cout << "=" << (num1 * num2) << endl;
break;
case '/':
cout << "=" << (num1 / num2) << endl;
break;
}
cout << "Do you want to try again <y or n>\n";
cin >> again;
}while (again == 'y' || again == 'Y');

system("pause");
return 0;

}
Hodgman
Hodgman
There's just a few simple typeos in your code.

The semicolon on the 'switch' line should be removed.
In between "[font="Courier New"]"="[/font]" and "[font="Courier New"](num1 x num2)[/font]", there should be another "[font="Courier New"]<<[/font]".
The 'while' line should end with a semicolon.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.