【发布时间】:2011-11-26 12:46:51
【问题描述】:
我正在为我的编程课编写这个程序,它有一堆愚蠢的约束,比如我必须使用嵌套的 if else 语句,我必须使用 cin.getLine() 来获取玩家姓名。它应该获取每个球员的名字并计算他们的击球率。
这不是整个程序,而是我遇到错误的部分。当我在命令提示符下运行它时,我可以很好地接收到第一个名称,但之后第二个 cin.getline() 不会读取任何输入。有什么建议吗?
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
char name1[100], name2[100], name3[100];
int numBat, numHit;
double avg1, avg2, avg3;
// Get Average for Player 1
cout << "What's Your Name? ";
cin.getline(name1, 100);
cout << "How many times have you been at bat? ";
cin >> numBat;
if(numBat < 0 || numBat > 25)
{
cout << "ERROR ::: Number of Times at Bat Cannot Be Less Than 0 or Greater Than 25. Run Program Again." << endl;
return 0;
}
else
{
cout << "How many times have you hit the ball? ";
cin >> numHit;
if(numHit < 0)
{
cout << "ERROR ::: Number Hit Cannot Be Less Than 0. Run Program Again." << endl;
return 0;
}
else
{
// Calculate Average for Player 1
avg1 = numHit / numBat;
// Get Average for Player 2
cout << "What's Your Name? ";
cin.getline(name2, 100);
cout << "How many times have you been at bat? ";
cin >> numBat;
cout << "How many times have you hit the ball? ";
cin >> numHit;
}
}
}
【问题讨论】:
-
正要将此标记为作业...但是已经有 5 个标记。大声笑
-
:/ 需要有作业标签吗?我觉得这会阻止人们帮助我解决我的问题......