我正在尝试读取名为elect12.csv elect12.csv sample in Excel的文件并获取最后一列(状态)中的值,并将其与用户输入的变量(stateCode)进行比较。但是,每次运行程序时,我都无法提示用户重新输入值。我怎样才能使这个工作?感谢。
//阅读热门投票文件 ifstream pVotesFile(“elect12.csv”);
for (int i = 0; i < statecount; i++)
{
if(pVotesFile.is_open())
{
cout << "Enter state code: ";
cin >> stateCode;
while (pVotesFile >> obama >> romney >> other >> total >> state)
{
if (stateCode == state)
{
statePoints = ((double)(obama - romney) / total) * 100;
stateTotal += statePoints;
}
else
{
cout << "Enter state code: ";
cin >> stateCode;
stateTotal = 0;
}
}
}
如果图片在将来的某个时间发生了变化,那么这个文件的前几行是这样的:
“795696 1255925 22717 2074338 AL”
“122640 164676 13179 300495 AK”
“1025232 1233654 40368 2299254 AZ”
“394409 647744 27315 1069468 AR”
“7854285 4839958 344304 13038547 CA”
答案 0 :(得分:1)
"795696
文件中有额外的quoatation标记。因此,cin >> integer_value;
将失败,因为它遇到文本。您应该像这样更改数据
795696 1255925 22717 2074338 AL 122640 164676 13179 300495 AK 1025232 1233654 40368 2299254 AZ 394409 647744 27315 1069468 AR 7854285 4839958 344304 13038547 CA
使用cout
确保您确实在阅读某些内容:
if (pVotesFile.is_open())
{
cout << "Enter state code: ";
cin >> stateCode;
while (pVotesFile >> obama >> romney >> other >> total >> state)
{
cout << obama << ", " << romney << ", " << other << ", " << total << ", "
<< state << "\n";
if (stateCode == state)
{
cout << "state found\n";
}
}
}
或者,保持数据不变,然后跳过第一个"
引号。