我有以下代码对nomC和nomP进行一些测试,但是使用2个连续的getlines导致跳过第一个(getline(cin,nomP);)..我该如何解决这个问题? PS:我试过cin.ignore();和cin.clear();并且它不起作用
#include <iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
int T;
cin >> T;
vector<string> decision(T);
for(int i=0;i<T;i++)
{
string nomP,nomC;
string help="";
vector<string> desc;
bool accepted = true;
getline(cin,nomP);
getline(cin,nomC);
while(help!="----")
{ getline(cin,help); desc.push_back(help);}
if ((nomP.size()<5)|| (nomP.size()>20))
{decision[i] = nomC+" rejected with error code 1.\n";accepted=false;}
if (nomP[0]<65|| nomP[0]>90)
{decision[i] = nomC+" rejected with error code 2.\n";accepted=false;}
if (nomP[nomP.size()]==32)
{decision[i] = nomC+" rejected with error code 3.\n";accepted=false;}
if((nomC.size()<5)|| (nomC.size()>10))
{decision[i] = nomC+" rejected with error code 4.\n";accepted=false;}
for(int j=0;j<nomC.size();j++)
{
if(((nomC[j]<48)&&(nomC[j]>57))||((nomC[j]<97)&&(nomC[j]>122)))
{decision[i] = nomC+" rejected with error code 5.\n";accepted=false;break;}
}
if (desc.size()>10)
{decision[i] = nomC+" rejected with error code 6.\n";accepted=false;}
for(int j=0;j<desc.size();j++)
{
if((desc[j].size()<1)||(desc[j].size()>80))
{decision[i] = nomC+" rejected with error code 7.\n";accepted=false;break;}
}
if (accepted)
decision[i] = nomC+" is OK.\n";
}
for (int i=0;i<decision.size();i++)
{
cout<< decision[i] << endl;
}
return 0;
}
答案 0 :(得分:1)
以这种方式看你的程序
int T;
cin >> T;
控制台输入:5 \ n
您可能已经注意到了这个问题。你认为你得到的是5,但它是一个5 +换行符。
控制台输入:名称\ n
然后你调用getline()
cin buffer不是:Name \ n,
实际上是:\ nName \ n
因此,使用第一个getline,您正在阅读单个“\ n”
和第二个,你终于阅读了“Name \ n”
有办法解决这个问题。一个是做这个技巧
while (isspace(cin.peek())) cin.ignore(); //dodge spaces, line breaks.
getline(cin, nomP);
getline(cin, nomC);
我只使用Windows,但也许换行符可能是\ r \ n在另一个操作系统中,这就是为什么单个cin.ignore()可能还不行。所以诀窍仍然有效。
但是有一种更好的方法:创建一个函数,只有当它读取非空行时才会返回。类似的东西:
string my_getline()
{
string result;
while (!getline(cin, result) || result.empty());
return result;
}
string nomP = my_getline();
string nomC = my_getline();
使用RVO,这与做getline(cin,nomP)一样快,而且更简单。
答案 1 :(得分:0)
问题是,从cin
获得迭代次数后,您无法清除换行符。所以,当你第一次打电话给getline()
时,它很乐意把所有东西都抓到你无意中留在那里的新线,而不是单独划线。
尝试输入以下输入,您就会明白我的意思:
2 Fred
Ted
----
Bob
Joel
----
请改为尝试:
#include <limits>
// ...
int T;
cin >> T;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
这应该清除输入循环大小后线上剩下的任何内容。
我还建议在程序中添加提示,至少在故障排除期间。通过这种方式更容易看到发生了什么。
std::cout << "Input number of iterations: ";
cin >> T;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// ...
std::cout << "Input first name: ";
getline(cin,nomP);
std::cout << "Input second name: ";
getline(cin,nomC);
while(help!="----")
{
std::cout << "Input separator: ";
getline(cin,help);
desc.push_back(help);
}
此外,我想指出您的错误代码设置存在两个潜在问题:遇到的最后一条错误消息将覆盖之前的任何错误消息,并且您的所有错误消息都说明问题出在nomC
,无论他们是否正在检查nomP
或nomC
。
// Issues pointed out with C-style comments.
if ((nomP.size()<5)|| (nomP.size()>20))
{
/* Checks nomP, says error is in nomC. */
decision[i] = nomC + " rejected with error code 1.\n";
accepted=false;
}
/* If conditions are met for both this and the previous error message, this overwrites the
previous error message.
*/
if (nomP[0]<65|| nomP[0]>90)
{
/* Checks nomP, says error is in nomC. */
decision[i] = nomC + " rejected with error code 2.\n";
accepted=false;
}
/* If conditions are met for both this and the previous error message, this overwrites the
previous error message.
*/
if (nomP[nomP.size()]==32)
{
/* Checks nomP, says error is in nomC. */
decision[i] = nomC + " rejected with error code 3.\n";
accepted=false;
}
/* If conditions are met for both this and the previous error message, this overwrites the
previous error message.
*/
if((nomC.size()<5)|| (nomC.size()>10))
{
decision[i] = nomC + " rejected with error code 4.\n";
accepted=false;
}
for(int j=0;j<nomC.size();j++)
{
/* If conditions are met for both this and the previous error message, this overwrites
the previous error message.
*/
if(((nomC[j]<48) && (nomC[j]>57)) || ((nomC[j]<97) && (nomC[j]>122)))
{
decision[i] = nomC + " rejected with error code 5.\n";
accepted=false;
break;
}
}
/* If conditions are met for both this and the previous error message, this overwrites the
previous error message.
*/
if (desc.size()>10)
{
/* Checks number of strings in desc, says error is in nomC. */
decision[i] = nomC + " rejected with error code 6.\n";
accepted=false;
}
for(int j=0;j<desc.size();j++)
{
/* If conditions are met for both this and the previous error message, this overwrites
the previous error message.
*/
if((desc[j].size()<1) || (desc[j].size()>80))
{
/* Checks string in desc, says error is in nomC. */
decision[i] = nomC + " rejected with error code 7.\n";
accepted=false;
break;
}
}
有两种方法可以解决这个问题:
if
语句更改为else if
语句。 (对于if
循环中和之后的for
语句,您可以添加一个条件,检查decision[i]
处是否已有字符串。)decision.push_back()
而不是直接分配到decision[i]
。