Apr 7, 2014 at 2:20am UTC
I am writing a program that cannot allow a negative number, but it is a structure array. Not sure what to do.
Here is the code from the array to getting the input.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
struct PlayerInfo
{
string name; //Players name
string number; //Players number
int pointsScored; //Points scored by player
};
int main()
{
//Constants and variables
const int NUM_PLAYERS = 3; // Number of players
PlayerInfo players[NUM_PLAYERS]; // Array of players
int count; // Loop counter
int totalScore = 0; // Total score holder
int highest; // Holds highest score
string highPlayer; // Holds highest player name
// Get player data
for (count = 0; count < NUM_PLAYERS; count++)
{
cout << "Enter " << (count+1) << "st players name: " ;
getline(cin, players[count].name);
cout << "Enter " << (count+1) << "st players number: " ;
cin >> players[count].number;
cout << "Enter " << (count+1) << "st players score: " ;
cin >> players[count].pointsScored;
cin.ignore();
cout << endl;
}
I tried 'if (players[count].number <= 0)'
but got an error message.
Last edited on Apr 25, 2014 at 9:14pm UTC
Apr 7, 2014 at 3:20am UTC
What is the error message? And if you want a positive integer, why don't you use "unsigned int"?
Apr 7, 2014 at 3:31am UTC
you're comparing a string to an int
Apr 7, 2014 at 5:59pm UTC
Thanks @Cody0023 I now feel like an idiot.
I changed it and it worked.
Thank you