是否有一个原因,如果在我的程序中我要求用户输入,我这样做:
int number;
string str;
int accountNumber;
cout << "Enter number:";
cin >> number;
cout << "Enter name:";
getline(cin, str);
cout << "Enter account number:";
cin >> accountNumber;
Run Code Online (Sandbox Code Playgroud)
为什么在输入第一个数字后,它输出"输入名称",然后立即输入"输入账号",然后我甚至输入我的"str"为getline(cin,str)线?谢谢!
可能重复:
getline没有要求输入?
在我的程序中发生了一些独特的事情.以下是一些命令:
cout << "Enter the full name of student: "; // cin name
getline( cin , fullName );
cout << "\nAge: "; // cin age
int age;
cin >> age ;
cout << "\nFather's Name: "; // cin father name
getline( cin , fatherName );
cout << "\nPermanent Address: "; // cin permanent address
getline( cin , permanentAddress );
Run Code Online (Sandbox Code Playgroud)
当我尝试将此代码段与整个代码一起运行时.输出程序的工作方式如下:

Enter the full name of student:
Age: 20
Father's Name:
Permanent Address: xyz
Run Code Online (Sandbox Code Playgroud)
如果你注意到,该程序没有问我全名,并直接问我年龄.然后它也跳过父亲的名字并询问永久地址. 这可能是什么原因?
我很难发布整个代码,因为它太大了.
嗨,大家好!我有以下代码:
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
#define MAXN 301
string names[MAXN];
vector<string> names_vec;
int main(int argc, char **argv)
{
ifstream fin(argv[1]);
int n;
fin>>n;
string line;
while(getline(fin, line))
names_vec.push_back(line);
for(int i=0; i<names_vec.size(); i++)
cout<<names_vec[i]<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
和names.in文件输入:
5
CLEOpatra
AISHWARYA rai
jOHn f. KeNNeDy
leonardo DA Vinci
tyleR durdeN
Run Code Online (Sandbox Code Playgroud)
当我编译并运行它时,首先打印空行,name_vec [0]为空行.任何人都可以解释为什么以及如何解决它?
我正在制作一个C++ Mind Reader程序,这个程序已接近完成.但是,感觉需要跳过第二个cin.我搜索过,我不确定是什么问题.我已经检查了代码,我打赌我做了一些愚蠢的事,但我仍然对此感到困惑.跳过的cin在第32行,这是我的代码:
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
//declaring variables to be used later
string name;
string country;
int age;
//header goes below
cout << " @@@@@@@@@@@@-MIND READER-@@@@@@@@@@@@\n\n";
//asks if the user would like to continue and in not, terminates
cout << "Would like you to have your mind read? Enter y for yes and n for no." << endl;
cout << "If you do not choose to proceed, this program will terminate." …Run Code Online (Sandbox Code Playgroud)