I'm pretty new to programming, and this program runs, but when I am able to enter the batting record, the console is presented with a Windows error ".exe has stopped working...". This has never happened before, and as a new programmer, I think it's scary.
#include <iostream>
usingnamespace 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;
}
Well, I was able to figure out the console issue. Now I'm just trying to make sum work. My problem is that sum=letters, but I have int letters=0;. Thus sum is zero. How can I loop through the array "size" so that the program then COUNTS and SUMS the values of the user input array??
#include <iostream>
usingnamespace std;
//Prototype to keep console from closing.
class KeepRunning {
public:
~KeepRunning() {
cin.get();}};
int main ()
{
KeepRunning kr;
constchar H = 1; //Define batting values
constchar h = 1;
constchar O = 1;
constchar o = 1;
constchar W = 0;
constchar w = 0;
constchar S = 0;
constchar s = 0;
constchar P = 0;
constchar p = 0;
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";
do
{
cout << "\n\nEnter the player's number: ";
cin >> player;
cout << "Enter the player's batting record: ";
cin >> size;
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' || cin.fail())
{
cout << "\nAcceptable batting record codes are: 'H','O','W','S','P'.Please try again.\n";
cin.clear();
cin.ignore();
}
} while (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' || cin.fail());
//Summate H, h, O, o
sum = letters;
cout << sum;
//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 << "\n\nPress ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
return 0;
}