Click here to Skip to main content
15,913,685 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
Hi all we need help, my newphew needs to get the program to ask the scores of 5 games, once the user enters the 5 score, it suppose to give the average score of the 5 games.... BUT we are missing something somewhere... we can only get it to ask for 1 score. below is what we have... ANY HELP WOULD BE GREAT... I have been trying to help him but I have no idea what to do....

C#
class Program
    {
        static void Main(string[] args)
        {

            // DECLARATIONS

            // variables
            int score = 0;              // integer to hold the current score inputted
            int game = 1;

            /* I know I have to do some kind of looping here for 5 games
             * but I'm not sure how to do that!
             */
            string numbers = null;
            for (int i = 1; i < 5; i++)
            {
                numbers += i;
                numbers += " ";
                if (i < 4)
                    continue;
                
            }

            // INPUT
            try
            {
                // Prompt for the score for the current bowler/game
                Console.Write("Please enter score for Game {0}: ", game);
                score = Convert.ToInt32(Console.ReadLine());

                /* Somehow I need to check to make sure the user entered a valid
                 * number for the score. It's impossible to score a negative
                 * amount or higher than 300... I need HELP!!!
                 */

                if (score >= 301)
                    Console.Write("Please enter a number under 300 for Game {0}: ", game);

                if (score <= -1)
                    Console.Write("Please enter a number apove 0 for Game {0}: ", game);

            }
            catch (Exception) // the user's input could not be converted to int
            {
                // invalid, try again
                Console.WriteLine("Error converting your input to a whole number. Please try again.");

            }


            // PROCESSING

            /* Once I get the looping right, I figure I'll be doing some calculations
             */



            // OUTPUT
            // Show the bowler score... it should be the average score though....
            Console.WriteLine("Average score for Bowler: {0}\n\n, score);

            // end of program
            Console.Write("\n\nPress any key...");

            Console.ReadKey();
        }
    }
}
Posted

Yes... nephew...

Anyways to loop around for five games you need to put your input code inside the for loop, I've also got no idea what your doing with the string numbers and the statement
C#
if (i < 4)
  continue;


Isn't actually doing anything.

So to loop 5 times you should have a for loop like this:

for(int i=0; i<5; i++)
{
  //Get input from user here
}
//Calculate average score and whatnot after the loop

where i starts at zero (or start at 1 if you want but then you need to check for i<6 or i<=5)

Also, instead of using a try-catch block you can use the function bool Int32.TryParse(string str, out int val) which will return true if it succeeds (and your result will be in val) and false if it fails.

So to check for valid input you'd need another kind of loop, the do while loop which will always run at least once and then keep going around until the statement at the end returns false.

bool validInput = false;
do
{
  int score = 0;
  validInput = Int32.TryParse(Console.ReadLine(), out score);
  
  //If the parse was successful (ie the user actually entered a number)
  // then you can check the actual value
  if(validInput)
  {
    if (score > 300)
    {
      Console.Writeline(...);
      validInput = false; //we need to try again
    }

    etc.
  }
}while(!validInput) //We will keep on looping until we have valid input


MSDN TryParse[^]
Loops in C#[^]

Your or your nephew may also want to pick up a beginners book for C# too, as it will cover all of the basics as well as best practices and provide examples; it would also server as a handy reference.
 
Share this answer
 
Comments
Henry Minute 15-Oct-10 4:09am    
You are a very suspicious person :)
he is in a beginners class at college, but have problems ...

we got it to ask for 5 different games, but can not get it to display the average score.


<pre lang="cs">/*  Lab02
 *  YOUR NAME HERE
 *  Lab 2 for PROG 1205
 *  Sep 2010
 *
 *  Description:
 *
 */



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lab02
{
    class Program
    {
        static void Main(string[] args)
        {

            // DECLARATIONS

            // variables
            int score = 0;              // integer to hold the current score inputted
            int game = 1;
            int avgScore = 0;

            /* I know I have to do some kind of looping here for 5 games
             * but I'm not sure how to do that!
             */
            for (game = 1; game < 6; game++)
            {
                // INPUT
                try
                {
                    // Prompt for the score for the current bowler/game
                    Console.Write("Please enter score for Game {0}: ", game);
                    score = Convert.ToInt32(Console.ReadLine());
                    game--;

                    /* Somehow I need to check to make sure the user entered a valid
                     * number for the score. It's impossible to score a negative
                     * amount or higher than 300... I need HELP!!!
                     */

                    if (score >= 301 || score <= -1)
                        Console.WriteLine("Score must be between 0 to 300. Please try agian.");
                    else
                        game++;
                }
                catch (Exception) // the user's input could not be converted to int
                {
                    // invalid, try again
                    Console.WriteLine("Error converting your input to a whole number. Please try again.");
                    game--;
                }

            }
            // PROCESSING

            /* Once I get the looping right, I figure I'll be doing some calculations
             */



            // OUTPUT
            // Show the bowler score... it should be the average score though....
            Console.WriteLine("Averge score for Bowler: {0}\n\n", avgScore);

            // end of program
            Console.Write("\n\nPress any key...");

            Console.ReadKey();
        }
    }
}

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900