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.
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.