我对编程很新,而且这个程序运行,但是当我能够输入击球记录时,控制台出现Windows错误" .exe已停止工作......& #34 ;.这从未发生过,作为一名新程序员,我认为这很可怕。
#include <iostream>
using namespace std;
//Prototype to keep console from closing.
class KeepRunning {
public:
~KeepRunning() {
system("pause");}};
//Define batting values
#define H 1
#define h 1
#define O 1
#define o 1
#define W 0
#define w 0
#define S 0
#define s 0
#define P 0
#define p 0
int main ()
{
KeepRunning kr;
int player; //Assign player number
double sum; //Assign variable for sum of H, h and O, o
double sumHits; //Assign variable for sum of only H and h
double average; //Assign variable for average of H and O
char size[100]; //Allows compiler to view user input as array
int b; //Assign variable for integer size
int letters = 0; //Assing value of 0 to allow compiler to count
cout << "\t\t\tBatting Average Calculator\t\t";
cout << "\n\nEnter the player's number: ";
cin >> player;
cout << "Enter the player's batting record: ";
cin >> size;
bool invalid = false;
while(!invalid)
{
invalid = true;
if ((size[b] == 'H') || (size[b] == 'h')
|| (size[b] == 'O') || (size[b] == 'o')
|| (size[b] == 'W') || (size[b] == 'w')
|| (size[b] == 'S') || (size[b] == 's')
|| (size[b] == 'P') || (size[b] == 'p'))
{
continue;
}
else {
cout << "\nAcceptable batting record codes are: 'H','O','W','S','P'.
Please try again.\n";
invalid = false;
}
}
//Summate H, h, O, o
sum = H + h + O + o;
//Summate
sumHits = H + h;
//Calculate batting average
average = sumHits/sum;
cout << "\nPlayer " << player << "'s batting record: " << size << endl;
cout << "Player " << player << "'s batting average: " << average << endl;
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
return 0;
}
好的,所以我已经做了一些改变,感谢yall。但是,我遇到了新问题。首先,当我运行程序并写一个有效的输入,即&#34; HOWHHHOOWHSPP&#34;时,没有任何反应。控制台只是保持打开状态,显示提示和输入。第二,当我写一个无效的输入时,即&#34; HOWQQQTTSHH&#34;或者不包括特定字母集的任何内容,控制台立即关闭而不是显示我的错误消息。如何让控制台不仅保持打开状态,而且重定向程序以重新开始输入任何无效的输入?
这是新代码:
#include <iostream>
using namespace std;
//Prototype to keep console from closing.
class KeepRunning {
public:
~KeepRunning() {
cin.get();}};
//Define batting values
#define H 1
#define h 1
#define O 1
#define o 1
#define W 0
#define w 0
#define S 0
#define s 0
#define P 0
#define p 0
int main ()
{
KeepRunning kr;
int player; //Assign player number
double sum; //Assign variable for sum of H, h and O, o
double sumHits; //Assign variable for sum of only H and h
double average; //Assign variable for average of H and O
char size[100]; //Allows compiler to view user input as array
int b=0; //Assign variable for integer size
int letters = 0; //Assing value of 0 to allow compiler to count
cout << "\t\t\tBatting Average Calculator\t\t";
cout << "\n\nEnter the player's number: ";
cin >> player;
cout << "Enter the player's batting record: ";
cin >> size;
bool invalid = false;
while (!invalid && size[b] != '\0')
{
if (size[b] != 'H' && size[b] != 'h' &&
size[b] != 'O' && size[b] != 'o' &&
size[b] != 'W' && size[b] != 'w' &&
size[b] != 'S' && size[b] != 's' &&
size[b] != 'P' && size[b] != 'p')
{
invalid = true;
}
else {
invalid = false;
}
}
//Summate H, h, O, o
sum = H + h + O + o;
//Summate
sumHits = H + h;
//Calculate batting average
average = sumHits/sum;
cout << "\nPlayer " << player << "'s batting record: " << size << endl;
cout << "Player " << player << "'s batting average: " << average << endl;
return 0;
}
答案 0 :(得分:1)
在行
cout << "Enter the player's batting record: ";
cin >> size;
我认为您应该将输入发送到b
,而不是size
。在您的代码中b
未初始化,因此它包含垃圾值。当您使用b
作为size
的索引时,索引无效,这会导致程序崩溃。
答案 1 :(得分:0)
看起来问题是你在为数组赋值之前尝试使用变量b作为数组的索引。我想你想要的是一个循环来检查用户输入的所有字符,所以像这样:
int b = 0;
while (!invalid && size[b] != '\0') {
//stuff in your existing loop
}
'\ 0'会自动添加到用户输入的末尾,并向循环指示已读取用户输入的所有字符。
作为旁注,最好重构循环以检查您检查的输入是否无效,如果是,则将invalid设置为true。这里没有理由继续使用。这样的事情会更好:
if (size[b] != 'H' && size[b] != 'h' &&
size[b] != 'O' && size[b] != 'o' &&
....etc.) {
invalid = true;
}
一旦遇到无效字符,循环就会结束。