我正在看一些我正在处理的代码,有3-4个错误我已经尝试了大约一周的时间来处理,但我就是做不到!我是编程新手,所以如果你能用愚蠢的方式回答,那就太好了!下面是代码。
#include <iostream>
#include <string>
using namespace std;
int main() {
string password;
int choice;
cout << "Command Line Multi-Tool" << endl;
cout << endl;
cout << "plase enter your password: " << endl;
cin >> password;
if (password == "creeper1") {
cout << endl;
cout << "Main Menu" << endl;
cout << "1. Class Schedule" << endl;
cout << "2. School Info" << endl;
cout << "3. Exit" << endl;
cin >> choice;
}
else {
cout << "Incorrect, Access Denied" << endl;
return(0);
}
}
else (password == "admin1"){
cout << "/*adminLogin=='1'*/" << endl;
cout << endl;
cout << "Menu::Main" << endl;
}
return(0);
}
}这是错误日志。
/Users/student/Documents/TO BE FILED/Tuesday/main.cpp:31:0 /Users/student/Documents/TO BE
FILED/Tuesday/main.cpp:31: error: expected unqualified-id before 'else'
/Users/student/Documents/TO BE FILED/Tuesday/main.cpp:36:0 /Users/student/Documents/TO BE
FILED/Tuesday/main.cpp:36: error: expected unqualified-id before 'return'
/Users/student/Documents/TO BE FILED/Tuesday/main.cpp:36:0 /Users/student/Documents/TO BE
FILED/Tuesday/main.cpp:36: error: expected declaration before '}' token再次,非常感谢!
发布于 2012-12-01 21:18:33
您的代码中有一个if,但有两个else分支。决定你想要哪一个,失去另一个。查看您可能需要的代码
if (password == "creeper1") {
cout << endl;
cout << "Main Menu" << endl;
cout << "1. Class Schedule" << endl;
cout << "2. School Info" << endl;
cout << "3. Exit" << endl;
cin >> choice;
} else if (password == "admin1")
// Your processing code here
} else {
cout << "Incorrect, Access Denied" << endl;
return(0);
}发布于 2012-12-01 21:18:31
不平衡的else,即没有对应的if。
也许你想要这样的东西:
if (password == "creeper1") {
}
else if (password == "admin1") {
}
else {
}发布于 2012-12-01 21:22:36
你不能在else之后使用else if,这个块永远不会返回,正确的语法是executed.Also 0,而不是return(0)。
您还可以包含额外的大括号,但是如果您以正确的方式缩进代码,您将看到代码块何时结束和开始,因此您很少犯像添加额外的大括号这样的错误:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string password;
int choice;
cout << "Command Line Multi-Tool" << endl;
cout << endl;
cout << "plase enter your password: " << endl;
cin >> password;
if (password == "creeper1")
{
cout << endl;
cout << "Main Menu" << endl;
cout << "1. Class Schedule" << endl;
cout << "2. School Info" << endl;
cout << "3. Exit" << endl;
cin >> choice;
}
else if(password == "admin1")
{
cout << "/*adminLogin=='1'*/" << endl;
cout << endl;
cout << "Menu::Main" << endl;
}
else
{
cout << "Incorrect, Access Denied" << endl;
return 0;
}
return 0;
}这是带有所有语法错误的代码,fixed.About语义我不知道它是否可以工作,但是现在你可以编译并执行它,来测试代码。
https://stackoverflow.com/questions/13659805
复制相似问题