Reputation: 13
I wrote a simple program and met an error in switch. What is wrong?
Error: expected primary-expression before ')' token
#include <iostream>
#include <list>
using namespace std;
int main() {
list<string> myList;
string s;
while (true) {
cin >> s;
switch(s) {
case "quit":
break;
default:
myList.push_back(s);
break;
}
}
}
Thanks.
Upvotes: 0
Views: 1004
Reputation: 4023
The real problem is here:
switch(s) {
You cannot use strings
in a switch case.
Alternative:
An if-else ladder. Since you have only one case, use an if
statement for it. For example:
if (s=="quit") {
break;
}
else
myList.push_back(s);
Upvotes: 3