Gmenfan83
Gmenfan83

Reputation: 2587

Input validation executes true regardless?

OK, here is a program that prompts a user for basketball player input. Well after the user gets prompted for the name of the player I validate the input to make sure it is in fact a string. Well the validation always executes true regardless and terminates the program. This does not happen with my other input validation, just the string. Any ideas as to why it will always execute true, hence terminating the program? Thank you.

Here is my code:

#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;


//struct of Basketball Player info
struct BasketballPlayerInfo
{
    string name; //player name

    int playerNum, //player number
        pointsScored; //points scored

};

int main()
{
    int index, //loop count
        total = 0; //hold total points
    const int numPlayers = 5; //nuymber of players
    BasketballPlayerInfo players[numPlayers]; //Array of players

    //ask user for Basketball Player Info
    cout << "Enter the name, number, and points scored for each of the 5 players.\n";

    for (index = 0; index < numPlayers; index++)
    {
        //collect player name
        cout << " " << endl;
        cout << "Enter the name of player # " << (index + 1);
        cout << ": ";

        //input validation
        if((!(getline(cin, players[index].name))));  <----validation goes wrong!
        {
            cout << "Player Name must be alphabetical characters only!\n";
            cout << "Program terminating please start over." << endl;
            system("pause");
            exit(0);
        }

        getline(cin, players[index].name);

        //collect players number
        cout << "Enter the number of player # " << (index + 1);
        cout << ": ";

        //input validation
        if(!(cin >> players[index].playerNum))
        {
            cout << "Player Name must be numeric characters only!\n";
            cout << "Program terminating please start over." << endl;
            system("pause");
            exit(0);
        }
        //collect points scored
        cout << "Enter points scored for player # " << (index + 1);
        cout << ": ";

        //input validation
        if(!(cin >> players[index].pointsScored))
        {
            cout << "Player Name must be numeric characters only!\n";
            cout << "Program terminating please start over." << endl;
            system("pause");
            exit(0);
        }

        cin.ignore();
    }

    //display
    cout << "\n";
    cout << "Here is the information for each player: \n";
    cout << fixed << showpoint << setprecision(2) << setw(5);
    cout << "\n";
    cout << "          \tName\tNumber\tPoints\n";
    cout << "------------------------------------------------" << endl;

    for(index = 0; index < numPlayers; index++)
    {
        cout << "Player # " << (index + 1);
        cout << ": \t" << players[index].name << "\t" << players[index].playerNum << "\t" << players[index].pointsScored << endl;
        cout << "------------------------------------------------" << endl;

    }

    //display total points scored by all players
    for(index = 0; index < numPlayers; index++)
    {
        //hold total
        total += players[index].pointsScored;
    }

    cout << "Total Points scored are: " << total << endl;

 system("pause");
return 0;

}

Upvotes: 0

Views: 141

Answers (1)

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272607

You have a ; at the end of that line.

Upvotes: 6

Related Questions