尝试与一些视频一起编写,以帮助我学习C ++并开始学习。这第一个程序很简单,我发现它无法正常工作。
我的代码
int main(){
int favorite_number
std::cout << "Enter your favorite number: ";
std::cin >> favorite_number;
std::cout << "Cool!!" <<std::endl;
return 0;
}
这是我不断遇到的错误
error: expected initializer before 'std'
std::cout << "Enter your favorite number: ";
^~~
error: 'favorite_number' was not declared in this scope
std::cin >> favorite_number;
^~~~~~~~~~~~~~~
有人可以向我解释为什么这行不通吗?我觉得我应该提到视频中的老师目前正在使用过时的软件。
答案 0 :(得分:3)
您缺少分号:
int main(){
int favorite_number;
std::cout << "Enter your favorite number: ";
std::cin >> favorite_number;
std::cout << "Cool!!" <<std::endl;
return 0;
}
答案 1 :(得分:0)
以这种方式尝试。
#include <iostream> // required for cout and cin
using namespace std; // instead of using std multiple times
int main(){
int favorite_number; // semicolon was missing
cout << "Enter your favorite number: ";
cin >> favorite_number;
cout << "Cool!!" <<endl;
return 0;
}