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
#include
// 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
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started