Question: I need this done in C++ This code already compiles and runs. I need my code modified to vali date user input for negative integers.

I need this done in C++

This code already compiles and runs.

I need my code modified to validate user input for negative integers. I dont want to accept negative integers or non integers(for "points"). I have already validated against non integers and now I need the program to check for negative numbers.

no break statements, use of outside libraries, no labels or goto labels, no infinite loops (while(1), etc)

//My code

#include #include 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

};

// Function prototypes void getPlayerInfo(Player &); void showInfo(Player); int getTotalPoints(Player[], int); void showHighest(Player[], int); bool checkInt(string input); //user input validation

int main()

{ const int numPlayers = 12; // The number of players 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)

{ string temp; cout << "Player name: "; cin.getline(p.name, 45); // check if input is valid or not while (true) { cout << "Player's number: "; cin >> temp; if (checkInt(temp)) { p.number = stoi(temp); break; } else { cout << "Entry can only be integer" << endl; } }

bool correct = false; do { cout << "Points scored: "; cin >> temp; if (checkInt(temp)) { p.points = stoi(temp); correct = true; } } while (!correct || 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;

} bool checkInt(string input) { for (int i = 0; i < input.size(); i++) { if (i == 0 && input[i] == '-') { continue; } if (!(input[i] >= '0' && input[i] <= '9')) { return false; } } return true; }

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!