C 投票计划帮助(初学者)
C++ voting program help (beginner)!
我在CPP中是新的,我试图通过一个小投票计划来训练自己。您参加聚会,然后派遣政党的投票。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string sInput = " ";
string sName1 = " ";
string sName2 = " ";
int iParty1 = 0;
int iParty2 = 0;
cout << "Name of the first party: ";
cin >> sName1;
cout << "Name of the second party: ";
cin >> sName2;
while (sInput != "")
{
cout << "Your vote: ";
cin >> sInput;
cout << endl;
if (sInput == sName1)
{
iParty1++;
}
else
{
if (sInput == sName2)
{
iParty2++;
}
else
{
cout << "Wrong Input" << endl;
}
}
}
cout << sName1 << ": " << iParty1 << endl;
cout << sName2 << ": " << iParty2 << endl;
getchar();
return 0;
}
因此,您会看到时循环。如果我只按ENTER,我希望程序停止。但是当我这样做时,什么都没有发生。为什么?给我一个线索!
更改输入函数,以便它通过std::cin
而不是令牌字符串在整个行中读取。您需要考虑这种读取方法,因为乐器中可能存在空间。另外,当您仅放置空白时,您的错误输入代码会发射。
这是一个固定版本:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string sInput = " ";
string sName1 = " ";
string sName2 = " ";
int iParty1 = 0;
int iParty2 = 0;
cout << "Name of the first party: ";
cin >> sName1;
cout << "Name of the second party: ";
cin >> sName2;
cin.ignore();
while (sInput != "")
{
cout << "Your vote: ";
getline(cin, sInput);
cout << endl;
if (sInput == sName1)
{
iParty1++;
}
else
{
if (sInput == sName2)
{
iParty2++;
}
else
{
cout << "Wrong Input" << endl;
}
}
}
cout << sName1 << ": " << iParty1 << endl;
cout << sName2 << ": " << iParty2 << endl;
getchar();
return;
}
相关文章:
- 需要帮助设置在C++中使用的Potrace
- 在指针的帮助下,文本文件中单词的频率
- C++错误消息*成员参考.**初学者*
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 如何在Qbutton的帮助下更改Q对话框的宽度
- 需要帮助将结构数组传递给函数
- 嵌套for循环C++的问题(初学者)
- C++需要帮助从用户那里获得一个整数,并确保它在另外两个整数之间
- 我是 C++ 的初学者,我想知道如何在 2D 矢量中获取重复值
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- 有人可以帮助我处理正则表达式吗?
- C++调用具有 *this 属性的单个帮助程序函数
- C++:需要帮助了解运算符重载错误
- 需要以下代码的帮助,下面的代码有什么问题
- 初学者需要帮助对数组进行排序和计算
- C 投票计划帮助(初学者)
- C++初学者.帮助将结构传递给函数.
- 初学者帮助:用sum打印Table
- 初学者c++帮助:if语句中的if语句
- c++ XCODE需要初学者帮助使用