我刚开始学习 C++,这个测试似乎是一个好主意,所以我尝试这样做,似乎没有用,而且(对我来说)这真的没有意义。
#include <iostream>
using namespace std;
int myNum = 5; // Integer (whole number without decimals)
double myFloatNum = 5.32543; // Floating point number (with decimals)
char myLetter = 'H'; // Character
string myText = "test text: test"; // String (text)
bool myBoolean = true; // Boolean (true or false)
int main() {
cout << myNum << endl;
cin >> myNum >> endl;
cout << myFloatNum << endl;
cin >> myFloatNum >> endl;
cout << myLetter << endl;
cin >> myLetter >> endl;
cout << myText << endl;
cin >> myText >> endl;
cout << myBoolean << endl;
cin >> myBoolean >> endl;
return 0;
}
它没有意义的cin
东西进入endl
。cin
是一个从中获取数据的流,但endl
正如@arsdever 所评论的那样,这是结束行的事情。
只需删除它,您的代码就会编译:
#include <iostream>
#include <string> // You forgot to include that header, for using std::string
using namespace std;
int myNum = 5;
double myFloatNum = 5.32543;
char myLetter = 'H';
string myText = "test text: test";
bool myBoolean = true;
int main() {
cout << myNum << endl;
cin >> myNum;
cout << myFloatNum << endl;
cin >> myFloatNum;
cout << myLetter << endl;
cin >> myLetter;
cout << myText << endl;
cin >> myText;
cout << myBoolean << endl;
cin >> myBoolean;
return 0;
}
虽然,您可能希望首先读取用户的输入,然后打印它。现在,您打印由您预定义的变量值(然后打印行尾),然后从用户那里读取该特定变量的输入。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句