使用带整数和getline的cin只能按特定顺序工作 [英] Using cin with integer and getline only works in certain order
问题描述
我正在使用一个非常基本的程序来获取名称并使用Visual C ++ 2012将其打印出来 - 程序运行正常如下:
int main()
{
char name [15];
int namecount;
cout<<输入你的名字:<< endl;
cin.getline(name,15);
cout<<你名字中有多少个字母(包括空格)?:<< endl;
cin>> namecount;
cout<<你的名字是:<< endl;
for(int i = 0; i< namecount; i ++)
{
cout<< name [i];
}
返回0;
}
但是当我尝试首先考虑名称的长度(如下所示)时,它将获取namecount的值,然后只打印你的名字是垃圾,不会执行cin.getline语句。欢迎任何想法。
int main()
{
char name [15];
int namecount;
cout<<你名字中有多少个字母(包括空格)?:<< endl;
cin>> namecount;
cout<<输入你的名字:<< endl;
cin.getline(name,15);
cout<<你的名字是:<< endl;
for(int i = 0; i< namecount; i ++)
{
cout<< name [i];
}
返回0;
}
我知道我可以使用字符串等但只是不知道为什么第二个版本赢了'' t。工作。
读取整数值后应该刷新std :: cin缓冲区:
// ...
std :: cin>> namecount;
std :: cin.sync();
std :: cout<< 输入您的姓名:<<的std :: ENDL;
// ...
或忽略行尾
// ...
std :: cin>> namecount;
std :: cin.ignore(std :: numeric_limits< std :: streamsize> :: max(),' \\\
'跨度>);
std :: cout<< 输入您的姓名:<<的std :: ENDL;
// ...
< / std :: streamsize>
我打电话给cin.Ignore(),请参考下面的内容,
#include < iostream >
#include < conio。使用 namespace std;
int main()
{
char name [ 15 跨度>];
int namecount;
cout<< 您姓名中的字母数(包括空格)?:<< ENDL;
cin>> namecount;
cout<< 输入您的姓名:<< endl;
cin.ignore(); // 已添加
cin.getline(name, 15 ,' \ n');
cout<< 您的姓名是:<< endl;
for ( int i = 0 ; i< namecount; i ++)>
{
cout<< name [i];
}
getch();
return 0 ;
}< /conio.h>< / iostream>
最好的问候
Muthuraja
I''m using a very basic program to take in a name and print it out using Visual C++ 2012 - Program works fine like this:
int main() { char name[15]; int namecount; cout<<"Enter your name: "<<endl; cin.getline(name, 15); cout<<"How many letters in your name (including the space)?: "<<endl; cin>>namecount; cout<<"Your name is:"<<endl; for(int i=0; i<namecount;i++) { cout<<name[i]; } return 0; }
but when I try and take in the length of the name first (as below) it will take in a value for namecount and then just print "Your name is" with garbage and will not execute the cin.getline statement. Any ideas welcome.
int main() { char name[15]; int namecount; cout<<"How many letters in your name (including the space)?: "<<endl; cin>>namecount; cout<<"Enter your name: "<<endl; cin.getline(name, 15); cout<<"Your name is:"<<endl; for(int i=0; i<namecount;i++) { cout<<name[i]; } return 0; }
I know I can use a string etc but just don''t get why the second version won''t work.
You should flush std::cin buffer after your integer value was read:
//... std::cin >> namecount; std::cin.sync(); std::cout << "Enter your name: " << std::endl; //...
or ignore end of line
//... std::cin >> namecount; std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::cout << "Enter your name: " << std::endl; //... </std::streamsize>
Hi,
I have called cin.Ignore(), please refer the below,
#include <iostream> #include <conio.h> using namespace std; int main() { char name[15]; int namecount; cout<<"How many letters in your name (including the space)?: "<<endl; cin>>namecount; cout<<"Enter your name: "<<endl; cin.ignore(); //added cin.getline(name, 15,'\n'); cout<<"Your name is:"<<endl; for(int i=0; i<namecount;i++)> { cout<<name[i]; } getch(); return 0; }</conio.h></iostream>
Best Regards
Muthuraja
这篇关于使用带整数和getline的cin只能按特定顺序工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!