C++ 程序不会问3个问题,而是使用我对其他两个问题的第一个问题的答案
我试图让用户输入他们3个朋友的名字,但是,它只问一个问题,并在第二个和第三个问题中写下我第一个问题的答案C++ 程序不会问3个问题,而是使用我对其他两个问题的第一个问题的答案,c++,C++,我试图让用户输入他们3个朋友的名字,但是,它只问一个问题,并在第二个和第三个问题中写下我第一个问题的答案 #include <iostream> using namespace std; int main() { char first_name; cout << "Please enter a name: "; cin >> first_name; cout << first_name << endl;
#include <iostream>
using namespace std;
int main()
{
char first_name;
cout << "Please enter a name: ";
cin >> first_name;
cout << first_name << endl;
char second_name;
cout << "Please enter a name: ";
cin >> second_name;
cout << second_name << endl;
char third_name;
cout << "Please enter a name: ";
cin >> third_name;
cout << third_name << endl;
return 0;
}
#包括
使用名称空间std;
int main()
{
字符名;
姓名;
cout您可能应该在代码中使用string
来输入名称。在名称中,您可能传递了多个字符。第一个字符由first\u name
读取,任何其他字符都将由以下字符读取,特别是cin>>second\u name
和cin>>third\u name
将读取输入的第2和第3个字符
char a;
char b;
cin>>a; //will only read one character and stop
cin>>b; //will read the second character of the input...
//be that after pressing enter(a Enter b) or continuous input (ab)
cout<<a<<" "<<b; //will output 1st and 2nd character only
chara;
字符b;
cin>>a;//将只读取一个字符并停止
cin>>b;//将读取输入的第二个字符。。。
//按enter(a输入b)或连续输入(ab)后
你试着在一个字符中容纳许多字符(一个单词),而这些字符只能容纳一个字符
#include <iostream>
#include <string> // We need a string, container to hold a chars. Something like array of chars but have a few difference.
using namespace std; // You should avoid using this but in that short code this doesn't matter
int main()
{
// You don't need separate container for this code
// Then we create one container to holds all of inputs
string input;
cout << "Please enter a name: ";
cin >> input; // Put input from user in our input(string)
cout << input << endl; // Print input
// Next code is the same as above
cout << "Please enter a name: ";
cin >> input;
cout << input << endl;
cout << "Please enter a name: ";
cin >> input;
cout << input << endl;
return 0;
}
#包括
#include//我们需要一个字符串、容器来保存字符。类似于字符数组,但有一些区别。
使用namespace std;//应该避免使用它,但在这么短的代码中,这并不重要
int main()
{
//此代码不需要单独的容器
//然后我们创建一个容器来保存所有输入
字符串输入;
cout>input;//将用户的输入放入我们的输入(字符串)
为什么你只使用一个名字<代码> char <代码>?你是指使用<代码> char */COD>?'c-风格字符串在C++中有点过时了,你可以使用<代码>字符串< /C>库,如果你进入C++,也可以是一个有趣的阅读。哦,我现在看到我的错误。把它改成字符串来修复这个问题。谢谢!