include <cstdlib>
#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
void printintromessage ();
void getUserInput (char& Y);
int main(int argc, char *argv[])
{
const int numofgamesinseries = 3;
const int numofplayersonteam = 4;
int i, score[numofgamesinseries];
int j, players[numofplayersonteam];
char Y = Y;


printintromessage ();

getUserInput (Y);

do
{
if (Y == 'Y' || Y == 'y')
{
cout <<"Enter scores for team 1" << endl;
for (j = 1; j <= numofplayersonteam; j++)
{
for (i = 1; i < numofgamesinseries; i++)
{
cout << "Enter player " << j << "'s score " << i << ": ";
cin >> score[i];
if (score [i] > 0 && score [i] <= 300)
{
cout << "Game 1 : " << score[0] << endl;
cout << "Game 2 : " << score[1] << endl;
cout << "Game 3 : " << score[2] << endl;
cout << "Series : " << score[0]+score[2]+score[3] << endl;
cout << "AVG. : " << ((score[1]+score[2]+score[3])/numofgames… << endl;
cout << "" << endl;
}
else
{
cout << score[i] << " is not a valid score ! Score must be from 0 to 300" << endl;
}
}
}
}

else

{
cout << "All finished ? Thank you !" << endl;
}
}

while ((Y == 'Y' || Y == 'y'));

system("PAUSE");
return EXIT_SUCCESS;
}
void printintromessage ()
{
cout << "This program processes bowling scores." << endl;
cout << "The user is asked to enter 3 bowling scores" << endl;
cout << "for each person on the team. There are 4 people" << endl;
cout << "per team. After all the scores are entered" << endl;
cout << "for a player, the program will list the individual" << endl;
cout << "game scores,the player's total score (series), and" << endl;
cout << "the player's average.After each team's input is complete," << endl;
cout << "the program will list the team's total scores by game," << endl;
cout << "the team's total score (series)and the team's average." << endl;
cout << "The user will then be asked if he/she wants to" << endl;
cout << "enter more data. If not, the program will list the" << endl;
cout << "team with the highest total score (series) and that " << endl;
cout << "total. If the user wants to enter more data," << endl;
cout << "the above process is repeated." << endl;
cout << " " << endl;
}

void getUserInput (char& Y)
{
cout << "Do you want to enter (more) data?" << endl;
cout << "Enter Y to continue, anything else to quit: ";
cin >> Y;
cout << " " << endl;
}

What the code is SUPPOSED to do is take in 3 scores each for all 4 players. The program is then supposed to check if the value input is between 0 and 300 and if it is output game 1, game 2, game 3, series and average. If a number between 0 and 300 ISNT input it should reprompt the user to enter a correct value. This is ideally what i need the code to do

What my code is actually doing is spitting out garbage values, please help!

Recommended Answers

All 4 Replies

Garbage in, garbage out. Uninitialized values are usually garbage, so make sure you don't have any. To check, initialize them all to something like -999999. Then if you are using them anywhere, it'll stand out like a sore thumb.

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
void printintromessage ();
void getUserInput (char& Y);
int main(int argc, char *argv[])
{
    const int numofgamesinseries = 3;
    const int numofplayersonteam = 4;
    int i, score[numofgamesinseries];
    int j, players[numofplayersonteam];
    char Y = Y;
   
   
    printintromessage ();
   
    getUserInput (Y);
   
 do
 {
  if (Y == 'Y' || Y == 'y')
         {
           {  
          cout <<"Enter scores for team 1" << endl;
           } 
         for (j = 0; j < numofplayersonteam; j++)
             {
            for (i = 0; i < numofgamesinseries; i++)
            {
                cout << "Enter player " << j + 1 << "'s score " << i+1 << ": ";
                cin >> score[i];
                
                
                if (score [i] > 0 && score [i] <= 300)
                {
                 cout << "Game 1 : " <<  score[0] << endl;
                 cout << "Game 2 : " <<  score[1] << endl;
                 cout << "Game 3 : " <<  score[2] << endl;
                 cout << "Series : " <<  score[0]+score[1]+score[2] << endl;
                 cout << "AVG. : " << ((score[0]+score[1]+score[2])/numofgamesinseries) << endl;
                 cout << "" << endl;   
                 }    
              else
                  {
        cout << score[i] << " is not a valid score ! Score must be from 0 to 300" << endl;
                  }
            }
            }
            }
            
            
            
        else
         {
          cout << "All finished ? Thank you !" << endl;
         }
         }  
        
         
 while ((Y == 'Y' || Y == 'y'));
 
    system("PAUSE");
    return EXIT_SUCCESS;
}

void printintromessage ()
     {
     cout << "This program processes bowling scores." << endl;
     cout << "The user is asked to enter 3 bowling scores" << endl;
     cout << "for each person on the team. There are 4 people" << endl;
     cout << "per team. After all the scores are entered" << endl;
     cout << "for a player, the program will list the individual" << endl;
     cout << "game scores,the player's total score (series), and" << endl;
     cout << "the player's average.After each team's input is complete," << endl;
     cout << "the program will list the team's total scores by game," << endl;
     cout << "the team's total score (series)and the team's average." << endl;
     cout << "The user will then be asked if he/she wants to" << endl;
     cout << "enter more data. If not, the program will list the" << endl;
     cout << "team with the highest total score (series) and that " << endl;
     cout << "total. If the user wants to enter more data," << endl;
     cout << "the above process is repeated." << endl;
     cout << " " << endl;
     }
    
void getUserInput (char& Y)
      {
      cout << "Do you want to enter (more) data?" << endl;
      cout << "Enter Y to continue, anything else to quit: ";           
      cin >> Y;
      cout << " " << endl;
      }

with this is seems to work other than it doesnt check for it to be between 0 and 300, how would i fix my problem

It DOESN'T work, whether you check for whether it is between 0 and 300 or not. Not checking either 1) made the fact that it doesn't work less apparent or 2) you got lucky and the fact that the variables are used without being uninitialized was not a problem THIS time.

Initialize your arrays to -999999 or some other number that stands out and run the program and look for that value.

You also still have the same 2 issues I mentioned the other day, in your other thread about the 2-d arrays (with this program).

While I do not think this will affect your program (because you get the value with user input later) it is still not correct.

char Y = Y; //this does not initialize it to the character Y. You need 's like char Y = 'Y';
   cout<<endl<<Y<<endl; //add this code & see what it prints

And you have that infinite loop again, because you don't provide a condition for which the loop can be stopped. It just keeps asking for scores, forever.

do{
      //your code here
             
      getUserInput(Y);   //add this to fix endless loop     
  }while ((Y == 'Y' || Y == 'y'));
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.