switch case

This is a simple program I wrote to tryin to learn about switch case statements. Can you help me find what I did wrong.

When I try to compile, I get this error message:
13 D:\- C++\Farming sim\test.cpp switch quantity not an integer


Thank you for your help.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
using namespace std;

int main()
{
string reply;
while(true)
{
cout <<"\nwhat \n";
cin >> reply;

switch (reply)
{
case "three": cout << "3";
    break;
case "two": cout << "2";
    break;
case "one": cout << "1";
    break;
default: cout << "qwerty";
    break;
}
}
cin.get();
cin.get();
} // main 
Nvm, I found that switch case statements only handle int and char values.
Last edited on
There are alternatives, but for some reason the compiler won't understand my clear code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <cstring>
#include <map>
using namespace std;

int main()
{
    string reply;
    map<string, int> numbers();
    numbers["one"] = 1;
    numbers["two"] = 2;
    numbers["three"] = 3;
    
    while(true)
    {
        cout <<"\nwhat \n";
        cin >> reply;
        
        if (numbers[reply])
        {
            cout << "element " << numbers[reply] << endl;
        }
        else
        {
            cout << "Element hasn't been found. The program will exit.";
            break;
        }
    }
    cin.get();
    return (0);
}


numers["one"] = 1; tells me that "one" is an invalid datatype, damn this.
An alternative to a switch could be an if()else if()...:
1
2
3
4
if (reply=="three")      cout << "3";
else if (reply=="two")   cout << "2";
else if (reply=="one")   cout << "1";
else                     cout << "qwerty";
map<string, int> numbers();
I guess you want to make a map called 'numbers'. What you do instead is declaring a function returning a map which is named 'numbers' and takes no arguments.
@ exception you are completely right.
After removing the parenteses my program worked.

Since I'm implicitly calling a constructor, I expected parameters should be passed. I will reread the chapter about classes sometime.
Mmmm, google "C++ most vexing parse" to understand why you had to remove the parens.
Topic archived. No new replies allowed.