Seeking some help on my bowling assignment please

Nested loops and domain range validation for bowling scores

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 12477 Views - Last Post: 22 January 2010 - 08:43 PM Rate Topic: -----

#1 bridsmath  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 21-January 10

Seeking some help on my bowling assignment please

Posted 21 January 2010 - 09:29 AM

Hello everyone! Nice to meet you!

I have been reading the forums for quite some time now and decided that I would register because I am really having a hard time with c++ programming. I just took VB and found it a lot easier for some reason.


A bowling team consists of five players.Each player bowls three games. Write a complete C++ program that uses a nested loop to enter each player's individual scores and then computes and displays the average score for each bowler. Also calculate and display the team score.

Include domain (range) validation in your solution. The lowest score a bowler can have is zero. The highest is three hundred. If the user enters a value outside of that range, the program should display an error message and re-prompt. Data-type validation is not required.



As you can tell by my code posted below I have managed to get the loop process to work and the averages of all 5 players scores, but my problem lies in the team total. The only solution I can come up with in my head involves TONS of inefficiency. There must be something very simple that I'm missing. I'm also not familiar at all with c++ validation because VB makes it so easy, haha. Any help on this would be so very much appreciated.


#include <iostream>
using namespace std;

int main()
{ 
	// Declarations
	int score1;
	int score2;
	int score3;
	int totalplayer;
	int totalteam;
	double average;
	
	// Input  
	for (int game = 1; game <= 5; game++)
{  
			cout << "Please enter your first score: ";
			cin >> score1;
			cout << "Please enter your second score: ";
			cin >> score2;
			cout << "Please enter your third score: ";
			cin >> score3;
			
			average = (score1 + score2 + score3) / 3;
   
			cout << "Your average is: " << average << endl;
				 for (int total = 1; total <= 5; total++)
				 {  
					totalplayer = (score1 + score2 + score3);
					totalteam += totalplayer;
				 }
}

cout << "The total score for the teams 3 games is: " << totalteam << endl;

	system("pause");
   return 0;
}


This post has been edited by bridsmath: 21 January 2010 - 09:30 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Seeking some help on my bowling assignment please

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1852
  • View blog
  • Posts: 6,664
  • Joined: 19-February 09

Re: Seeking some help on my bowling assignment please

Posted 21 January 2010 - 10:19 AM

Hi, although the general direction of your program could be made to work,
the question requires that it uses a nested loop.

Trying to add

	 for (int total = 1; total <= 5; total++)
	 {  

	 }



on to your present code, doesn't really work, as you are finding out.

I would suggest a basic structure like :-

for each player
   for each game
	  enter score.


Edit: Thats just pseudo code!

This post has been edited by #define: 21 January 2010 - 10:24 AM

Was This Post Helpful? 1
  • +
  • -

#3 algotechie  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 29-November 09

Re: Seeking some help on my bowling assignment please

Posted 21 January 2010 - 11:40 AM

Working the problem inside out, let's first get the scores from the user. Since a range is to be checked, let's put the code in a nested loop, like this:

int teamTotal=0;
for (player=1; player<6; ++player)
{
int total=0, score;
for (game=1; game<4; ++game)
{
do
{
cout<<"\nEnter score for player no. "<<player<<" in game no. "<<"game<<": ";
cin>>score;
if ((score>=0)&&(score<301))
break;
cout<<"\nPlease enter a number between 0 and 300, both included.";
}
while (1);
total+=score;
}
cout<<"\nAverage score for player no. "<<player<<" is "<<score/3;
teamTotal+=score;
}
cout<<"Team's score for all three matches is "<<teamScore; // If this is what is meant by team score; if it's something different, please elaborate.


Hope that helps.
Was This Post Helpful? 1
  • +
  • -

#4 bridsmath  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 21-January 10

Re: Seeking some help on my bowling assignment please

Posted 21 January 2010 - 02:36 PM

Hey guys,

Your pseudo code did help me, but I can't figure out how to execute it properly :(

algotechie: I've been toying with the code that you posted but have been running into countless errors that I can't seem to fix.

Any other insight on this assignment would be absolutely lovely!

Thanks again!

This post has been edited by bridsmath: 21 January 2010 - 03:26 PM

Was This Post Helpful? 0
  • +
  • -

#5 Notorion  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

Re: Seeking some help on my bowling assignment please

Posted 21 January 2010 - 04:36 PM

Repost your code plz. Also if you have specific questions a certain section please post that as well.
Was This Post Helpful? 0
  • +
  • -

#6 Metitron  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 79
  • Joined: 13-October 09

Re: Seeking some help on my bowling assignment please

Posted 21 January 2010 - 04:37 PM

This just begs to be put into a parralel array or vector...but as such as your new to c++ we wont go that route.
if you think of nested loops as rows of data the first loop is the number of columns (players) and the second as rows(games).
so in the inner loop ask for the scores and put the total of each plaers scores into an independant variable than after the outer loop simply total the total scores for each player:

int score=0;
int playerTotal=0;
int playerAvg=0;
int teamTotal=0;

for (int i=0; i<5; i++)
{
for (int x=0; x<3; x++)
{
cout<<"please enter a score: ";
cin>>score;
playerTotal=playerTotal+score;
}
playerAvg=playerTotal/3;
teamTotal=teamTotal+playerTotal;
playerTotal=0;
cout<<playerAvg<<endl;
}
cout<<teamTotal<<endl;
then just make it look good to include game #'s and player numbers or names.

Hope this helps
Was This Post Helpful? 0
  • +
  • -

#7 bridsmath  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 21-January 10

Re: Seeking some help on my bowling assignment please

Posted 21 January 2010 - 04:40 PM

Here's an updated version of my code:
I have everything working except for the team's totals and now the averages wont work :(
Can anyone figure out why?
#include <iostream>
using namespace std;


int main()
{
	
	// Declarations
	int total = 0;		 // The total scores for each player
	int teamTotal = 0;	 // The total scores for all players and games
	int score;			 // Stores input for scores		  
	
	for (int player = 1; player <= 5; player++)   // 5 Loops for each player
{
		for (int game = 1; game <= 3; game++)	 // 3 Loops for each game
		{
		cout << "\nEnter score for player no. "<<player<<" in game no. "<<game<<": ";
		cin >> score;
			if (score <= -1 || score >= 301)
			{
			cout << "\nPlease enter a number between 0 and 300, both included.";
			--player;  
			break;
			}
			else
			{
			continue;
			total += score;
			teamTotal += total;
			cout << "\nAverage score for player no. "<<player<<" is " << total / 3 << endl;
			total = 0;
			}
		} 
}

cout << "\nTeam's score for all three matches is " << teamTotal << endl;

 system("pause");
   return 0;
}


This post has been edited by bridsmath: 21 January 2010 - 04:41 PM

Was This Post Helpful? 0
  • +
  • -

#8 Metitron  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 79
  • Joined: 13-October 09

Re: Seeking some help on my bowling assignment please

Posted 21 January 2010 - 04:42 PM

you just need to put the team total out side the inner loop but still inside the outer loop :)
Was This Post Helpful? 1
  • +
  • -

#9 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Seeking some help on my bowling assignment please

Posted 21 January 2010 - 04:44 PM

I would assume that your continue statement is messing you up.

continue doesn't mean keep going on the current iteration, it means skip this iteration and go on to the next one.
Was This Post Helpful? 1
  • +
  • -

#10 bridsmath  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 21-January 10

Re: Seeking some help on my bowling assignment please

Posted 21 January 2010 - 05:00 PM

View Posterik.price, on 21 Jan, 2010 - 03:44 PM, said:

I would assume that your continue statement is messing you up.

continue doesn't mean keep going on the current iteration, it means skip this iteration and go on to the next one.


Ah, I got rid of the continue, but now my loop breaks early and reports the average after 1 input is made. :blink:

This post has been edited by bridsmath: 21 January 2010 - 05:03 PM

Was This Post Helpful? 0
  • +
  • -

#11 Notorion  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

Re: Seeking some help on my bowling assignment please

Posted 21 January 2010 - 05:24 PM

take the break out. You don't need the continue or the break there in the if else statements.
Also, Metitron was correct. You do need the teamTotal inside the first loop, but not in the second.
The second loop is called a nested loop, because it's nested inside another one.

This post has been edited by Notorion: 21 January 2010 - 05:28 PM

Was This Post Helpful? 0
  • +
  • -

#12 bridsmath  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 21-January 10

Re: Seeking some help on my bowling assignment please

Posted 22 January 2010 - 11:42 AM

OK, so I've almost completed this program, but I'm running into one more logical error that I can't seem to wrap my mind around.

Here is my updated code:

#include <iostream>
using namespace std;


int main()
{
	
	// Declarations
	int playerTotal = 0;		 // The total scores for each player
	int teamTotal = 0;		   // The total scores for all players and games
	int score = 0;			   // Stores input for scores		  
	
	for (int player = 1; player <= 5; player++)   // 5 Loops for each player
{
		for (int game = 1; game <= 3; game++)	 // 3 Loops for each game
		{
		cout << "\nEnter score for player no. "<<player<<" in game no. "<<game<<": ";
		cin >> score;
			
			if (score <= -1 || score >= 301)
			{
			cout << "\nPlease enter a number between 0 and 300, both included.";
			game--;
			}
			else
			{
			playerTotal += score;
			}
		}
		cout << "\nAverage score for player no. "<<player<<" is " << playerTotal / 3 << endl;
		playerTotal = 0;
		
}

teamTotal += playerTotal;
cout << "\nTeam's score for all three matches is " << teamTotal << endl;

 system("pause");
   return 0;
}



I have just about everything working so far. The problem I am running into is that I am using the command playerTotal = 0; at the end of my loop to set it back to 0 so that the next players average will calculate properly, but it is interfering with my teamTotal at the end of the program. If there is any way you could lead me in the right direction to fixing this I would appreciate it a lot!

Thanks again guys. This forum has been a great help to me as a beginner.
Was This Post Helpful? 0
  • +
  • -

#13 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Seeking some help on my bowling assignment please

Posted 22 January 2010 - 11:52 AM

Take another variable: int playerTotalTmp = 0, for example. Then, use it here:
 playerTotal += score;
playerTotalTmp += playerTotal;


Modify:
teamTotal += playerTotal;


with
teamTotal += playerTotalTmp;


And that's all. As you could see, playerTotal = 0; you use this here, and playerTotal will be always 0 at the end of the loops.

This post has been edited by sarmanu: 22 January 2010 - 11:53 AM

Was This Post Helpful? 1
  • +
  • -

#14 algotechie  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 29-November 09

Re: Seeking some help on my bowling assignment please

Posted 22 January 2010 - 11:52 AM

I could not further help you yesterday because I went out after posting the first reply and then onward I was on a mobile phone on which it was difficult to type. I'm sorry for that. I was, though, reading the topic.

Alright, you've got the code working so far. Just a small change should get it going. Here's the code after that change (changed the location of setting the "playerTotal" to 0 and also moved the "teamTotal" to increment inside the loop). (Note that except for these changes, I just copied and pasted your own code without any checking.)

for (int player = 1; player <= 5; player++)   // 5 Loops for each player
{
		playerTotal = 0;
		for (int game = 1; game <= 3; game++)	 // 3 Loops for each game
		{
		cout << "\nEnter score for player no. "<<player<<" in game no. "<<game<<": ";
		cin >> score;
		   
			if (score <= -1 || score >= 301)
			{
			cout << "\nPlease enter a number between 0 and 300, both included.";
			game--;
			}
			else
			{
			playerTotal += score;
			}
		}
		cout << "\nAverage score for player no. "<<player<<" is " << playerTotal / 3 << endl;

				teamTotal += playerTotal;
	   
}

cout << "\nTeam's score for all three matches is " << teamTotal << endl;

 system("pause");
   return 0;
}


Hope that helped.

This post has been edited by algotechie: 22 January 2010 - 12:01 PM

Was This Post Helpful? 0
  • +
  • -

#15 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,997
  • Joined: 09-June 09

Re: Seeking some help on my bowling assignment please

Posted 22 January 2010 - 11:55 AM

I didnt compile this but i think you just need to move teamTotal into your loop
#include <iostream>
using namespace std;


int main()
{
	
	// Declarations
	int playerTotal = 0;		 // The total scores for each player
	int teamTotal = 0;		   // The total scores for all players and games
	int score = 0;			   // Stores input for scores		  

	
	for (int player = 1; player <= 5; player++)   // 5 Loops for each player
{
		for (int game = 1; game <= 3; game++)	 // 3 Loops for each game
		{
		cout << "\nEnter score for player no. "<<player<<" in game no. "<<game<<": ";
		cin >> score;
			if (score <= -1 || score >= 301)
			{
			cout << "\nPlease enter a number between 0 and 300, both included.";
			game--;
			}
			else
			{
			playerTotal += score;
			}
		}
		cout << "\nAverage score for player no. "<<player<<" is " << playerTotal / 3 << endl;
		teamTotal += playerTotal; //<-------------
		playerTotal = 0; 
}
cout << "\nTeam's score for all three matches is " << teamTotal << endl;

system("pause");
   return 0;
}


Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2