Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases And Information Systems 1 International Baltic Conference Dbandis 2020 Tallinn Estonia June 19 2020 Proceedings

Authors: Tarmo Robal ,Hele-Mai Haav ,Jaan Penjam ,Raimundas Matulevicius

1st Edition

303057671X, 978-3030576714

More Books

Students also viewed these Databases questions