Question: I need this done in C++ Validation help please. Need to validate user input. The following code compiles and runs but I need to validate

I need this done in C++

Validation help please. Need to validate user input.

The following code compiles and runs but I need to validate all user input without using #include (as it sits currently thats how one of my validations work). I need to validate to make sure the program only accepts integer for "Player number" and "player points". I dont want to use a validation method that comes from libraries, templates, #, etc.

#include #include #include // this only used for validating input using namespace std;

// Declaration of the Player structure struct Player

{

char name[45]; // Player's name int number; // Player's number(validated) int points; // Points scored by the player

};

const int numPlayers = 12; // The number of players

// Function prototypes void getPlayerInfo(Player &); void showInfo(Player); int getTotalPoints(Player[], int); void showHighest(Player[], int);

int main()

{

Player team[numPlayers]; int index; for (index = 0; index < numPlayers; index++) { cout << " PLAYER #" << (index + 1) << " "; cout << "--------- "; getPlayerInfo(team[index]); cin.get(); }

cout.width(20); cout.setf(ios::left); cout << " NAME"; cout.width(10); cout << "NUMBER"; cout.width(10); cout << "POINTS SCORED "; for (index = 0; index < 12; index++) showInfo(team[index]); cout << "TOTAL POINTS: " << getTotalPoints(team, numPlayers) << endl; showHighest(team, numPlayers);

}

//fuynction to get Player info from user void getPlayerInfo(Player &p)

{ cout << "Player name: "; cin.getline(p.name, 45); cout << "Player's number: "; // check if input is valid or not while (!(cin >> p.number)) {

//#include cin.clear(); cin.ignore(numeric_limits::max(), ' '); cout << "Entry can only be integer : "; }

do { cout << "Points scored: "; cin >> p.points; } while (p.points < 0);

}

//Displays User input of player void showInfo(Player p)

{ cout << setw(20) << p.name; cout << setw(10) << p.number; cout << setw(10) << p.points << endl;

}

//Finds highest points scored int getTotalPoints(Player p[], int size)

{ int total = 0; for (int index = 0; index < size; index++) total += p[index].points; return total; }

//Shows highest player number scored void showHighest(Player p[], int size) { int highest = 0, highPoints = p[0].points; for (int index = 1; index < size; index++) { if (p[index].points > highPoints) { highest = index; highPoints = p[index].points; } }

cout << "The player who scored the most points is: "; cout << p[highest].name << endl;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!