Question

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>
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;
}

Okay, so I've made a couple of changes thanks to yall. But, I've got new problems. First, when I run the program and write a valid input, i.e. "HOWHHHOOWHSPP", nothing happens. The console just stays open displaying the prompts and inputs. Second, when I write an invalid input, i.e. "HOWQQQTTSHH" or anything not including the specific set of letters, the console closes immediately rather than displaying my error message. How can I have the console not only stay open, but redirect the program to start over for any invalid input?

Here's the new code:

#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;
}
Was it helpful?

Solution 2

It looks like the problem is that you're attempting to use the variable b as the index to the array before assigning a value to it. I think what you want is a loop to check all of the characters entered by the user, so something like this:

int b = 0;

while (!invalid && size[b] != '\0') {
    //stuff in your existing loop
}

'\0' is automatically added to the end of the user input and indicates to the loop that all of the characters entered by the user have been read.

As a side note, it would be good to restructure your loop to check if the input you're checking is invalid and if it is, set invalid to true. There's no reason to use continue here. Something like this would be better:

if (size[b] != 'H' && size[b] != 'h' &&
    size[b] != 'O' && size[b] != 'o' &&
    ....etc.) {
    invalid = true;
}

The loop will end as soon as an invalid character is encountered.

OTHER TIPS

In the lines

cout << "Enter the player's batting record: ";
cin >> size;

I think you should take input to b, not size. In your code b is uninitialized, so it contains garbage value. When you use b as index for size, the index is invalid, which causes your program to crash.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top