0

I'm Pretty New With C++, All I Know I've Learnt From Internet(Yay), and Last time I "Programmed" Was a half a year ago, but now I've decided to give it a try again. And Basically I've decided to refresh my memory with a simple calculator, and i've run into a problem with switch function, which I've never used before, I always like If more, but people say it's more convienient, and I get error saying:"case label does not reduce to an integer constant".

    #include <iostream>
#include <string>
#include <sstream>
using namespace std;
string Pirmas; //Pirmas = First In English
string Antras; // Second
string Trecias; // Third
int a,b,c;
int main() {

cout << "Paprastas Skaiciuotuvas V1!"<< endl;   // Simple Calculator
cout << "*********************************"<<endl;
cout <<"Iveskite Pirmaji Skaitmeni!:"<< endl;   //  Enter First Numeral
getline(cin,Pirmas);
stringstream(Pirmas) >> a;
cout <<"Iveskite Antraji Skaitmeni!:"<<endl; // Enter Second Numeral
getline(cin,Antras);
stringstream(Antras)>> b;
cout<<"Iveskite Matematini Zenkla!(+-*/):"<<endl; // Enter Arithemic Sign
getline(cin,Trecias);
stringstream(Trecias)>>c;
switch(c){
case "+":
cout<<"Atsakymas: "<<a+b<<endl; // Answer
break;
case "-":
cout<<"Atsakymas: "<<a-b<<endl;
break;
case "*":
cout<<"Atsakymas: "<<a*b<<endl;
break;
case "/":
cout<<"Atsakymas: "<<a/b<<endl;
break;
};
return 0;
}

Thank You. P.S Coding Is Amazing

  • possible duplicate of [switch case: error: case label does not reduce to an integer constant](http://stackoverflow.com/questions/14069737/switch-case-error-case-label-does-not-reduce-to-an-integer-constant) – rghome Jul 04 '15 at 22:23
  • Google the error message - you will get the answer. – rghome Jul 04 '15 at 22:23

2 Answers2

0

The case expressions must be integral expressions. While a single character value like '/' will be interpreted as an integer expression, the "string" (array of characters) "/" will not.

So you can do:

switch(c)
{
case '-':
    ...
}
jholtrop
  • 406
  • 4
  • 6
0

If +, -, *, / etc. are character here, then you have to enclose them single quote instead quote. Because in C and C++ characters are enclosed with single quote. Double quote is considered as string.

Imran
  • 4,582
  • 2
  • 18
  • 37