Troubles Creating a Program in C++ with Console Input Using Loops

I have an assignment that requires creating a diving competition scoring program. First, it requires creating a loop that prompts the user to input into the console the names and home cities of the each diver accompanied by five scores of the judges from 0 to 10. Also, a message should alert the user when they incorrectly input a value that is not (0 - 10) and then request the correct judge's score again. The user is also prompted to enter a degree of difficulty score with a valid range of 1 - 1.67. Likewise, if this degree of difficulty score is incorrectly inputted the user will be warned and directed to input a score in the valid range. Once the five scores and the degree of difficulty score are correctly inputted, the lowest and highest of the 5 scores are removed, the 3 remaining scores added together, then divided by 3 to get the average, and then multiplied by the degree of difficulty to obtain an overall score. Then the diver's name is to be displayed with their home city and their overall score. The program then asks the user if there is another diver to input with a response of (Y/N)? If the user replies yes by typing in an uppercase "Y" or lowercase "y", the diver information, scoring, and output sequence repeats. At least two divers are required to be inputted (2 sequences). Then when the user is asked if there is another diver to input, the user can respond no by entering an uppercase "N" or lowercase "n". Then the "Event Summary" will be displayed with the total number of divers displayed with the average score of all the divers. The average score is basically the diver's overall scores added together and divided by the amount of divers. The program ends with "Press any key to continue..." on the bottom line of the console.
This assignment is very complicated for a beginner like me. Any help would be appreciated! I don't expect anyone to do my entire assignment. However, I would appreciate some tips on how to build the general architecture of the program with an outer loop, another nested loop, and some possible "if" statements. I think these structures will be necessary for the program to gather all the information to be stored correctly into the variables, to make sure the user does the correct input including being warned when they make a mistake, and to make all the necessary computations.
Thank you for any and all assistance!
hi lvartist70,

You could start by doing a basic design. The assignment question is already spells out the methodology, so put the various steps into a file as comments, then go back and write the code that carries out what is written in the comment. Some thing like this:

1
2
3
4
5
6
7
8
//loop until input is finished
   //Get input Name & city
        //check for valid input
   //loop 5 times 
      //Get input scores
               //check for valid input after each one

//etc 


Some other helpful things to be aware of:

*. Your variable types & capacities - use float not int for scores
*. Think about data structures to store your data. Eg Array of structs
*. Check for divide by zero
*. Be careful comparing floats, need to use epsilon value. Google this.
*. Use meaningful variable names, avoid single character names
*. Always initialise the variables, preferably at the same time as declaration, and comment what the
variable means, max min values plus anything else that might be helpful to someone else.
*. Use functions, declare them before main, put definitions after main
*. Don't be afraid to comment everything
*. Google is your best friend, check things out before you use them, eg library functions, arrays loops etc

HTH

Hi TheIdeasMan,

Your suggestions concerning sound practices are words to live by. Although I haven't initialize with floats, I will look into using them and not int for the scores. I haven't worked with arrays, but they look very helpful and the sensible choice.

So far my basic design is this:

#include <iostream>

using namespace std;

int main()
{

// Write report heading
cout << "\tReport to the media\n";
cout << "Event: Diving Competition\n\n";
char choice = 'Y';
while (choice == 'Y' && choice == 'y');
{
// Declare Variables

char diverName1 = 'a';
char diverName2 = 'b';
char diverName3 = 'c';
char diverName4 = 'd';
char diverName5 = 'e';
char diverCity1 = 'f';
char diverCity2 = 'g';
char diverCity3 = 'h';
char diverCity4 = 'i';
char diverCity5 = 'j';

// Input diver's name and city

cout << "Enter the diver's name: ";
cin >> diverName1;
cout << "Enter the diver's city: ";
cin >> diverCity1;

// Initialize highest score, lowest score and total score

int scoreJudge1 = 0;
int scoreJudge2 = 0;
int scoreJudge3 = 0;
int scoreJudge4 = 0;
int scoreJudge5 = 0;
int degreeDifficulty = 0;

// Using a do-while loop input the 5 judge's scores

do
{
cout << "Enter the score given by judge #1: ";
cin >> scoreJudge1;
if ((scoreJudge1 < 0) || (scoreJudge1 > 10))
{cout << "Invalid score - Please reenter (Valid Range: 0 - 10)" << endl;
cout << "Enter the score given by judge #2: ";
cin >> scoreJudge2;
if ((scoreJudge2 < 0) || (scoreJudge2 > 10))
{cout << "Invalid score - Please reenter (Valid Range: 0 - 10)" << endl;
cout << "Enter the score given by judge #3: ";
cin >> scoreJudge3;
if ((scoreJudge3 < 0) || (scoreJudge3 > 10))
{cout << "Invalid score - Please reenter (Valid Range: 0 - 10)" << endl;
cout << "Enter the score given by judge #1: ";
cin >> scoreJudge4;
if ((scoreJudge4 < 0) || (scoreJudge4 > 10))
{cout << "Invalid score - Please reenter (Valid Range: 0 - 10)" << endl;
cout << "Enter the score given by judge #1: ";
cin >> scoreJudge5;
if ((scoreJudge5 < 0) || (scoreJudge5 > 10))
{cout << "Invalid score - Please reenter (Valid Range: 0 - 10)" << endl;
} while ((scoreJudge1 < 0) || (scoreJudge1 > 10));

// Loop as long as there are divers to process
// Validate the score to ensure it is between 0 and 10
// Add score to total
// Determine highest and lowest scores

// Input and validate the degree of difficulty
// Calculate the overall diver's score

// Display the diver's information and overall score
// Add diver's overall score to the final score
// Add 1 to the number of divers
// Prompt user: Do you want to process another diver (Y/N)?


cout << "\n\nDo you want to process another diver (Y/N)? ";
cin >> choice;

}

// End-Loop
// Display EVENT SUMMARY
// Calculate the average score for all divers
// Display the number of divers and the average score for all divers

system ("PAUSE");

return 0;
}

Looks like I have a long way to go, but I will start implementing your suggestions!

Thank you
OK, the first thing is to use code tags, they make it much easier to read your code. Use the <> buttton on the right under format.

1
2
3
4
// Write report heading
cout << "\tReport to the media\n";
cout << "Event: Diving Competition\n\n";
char choice = 'Y';


I would put this near the end when you have all the info. Writing the report should be a function.

1
2
char choice = 'Y';
while (choice == 'Y' && choice == 'y');


This has several probelms: First, it doesn't do anything (the code in the braces is never executed), for 2 reasons - can you figure out why? Clues are the semicolon, and the && operator.
Apart from this, if the intention was to be able to re run the program, you need to be more clear about how you are going to do this. Try using a bool variable to accomplish this.

Investigate using an array of structs. Here is an example struct.

1
2
3
4
struct Diver {
string Name = "";   //The divers name
string City = "";   //The divers home town
};

Notice how they are strings, not a char.
Now you could have an array of these, the size of which could be determined by a const unsigned short variable. This way you don't have to have this :

1
2
3
4
5
6
7
8
9
10
char diverName1 = 'a';
char diverName2 = 'b';
char diverName3 = 'c';
char diverName4 = 'd';
char diverName5 = 'e';
char diverCity1 = 'f';
char diverCity2 = 'g';
char diverCity3 = 'h';
char diverCity4 = 'i';
char diverCity5 = 'j';


The scores & difficulty could be an array inside the struct.

1
2
3
4
5
6
7
8
9
10
// Using a do-while loop input the 5 judge's scores

do
{
cout << "Enter the score given by judge #1: ";
cin >> scoreJudge1;
if ((scoreJudge1 < 0) || (scoreJudge1 > 10))
{cout << "Invalid score - Please reenter (Valid Range: 0 - 10)" << endl;
cout << "Enter the score given by judge #2: ";
cin >> scoreJudge2;


Now this needs to be in a loop - what if you had 10 judges, would you write this code 10 times? So you need nested loops, One to go through the array of divers, another to get the scores & difficulty. The main idea is to write code once, so if you find yourself writing the same code, then you probably need a loop.

With this code, if input is invalid, it jumps to the next judge with no opportunity to fix the problem.

Usually, the collection of the input and the validation of it should be inside a function.

This condition for the do loop isn't right.

} while ((scoreJudge1 < 0) || (scoreJudge1 > 10));

I try to avoid using do loops, I much prefer to use a while or for loop where ever possible.

Any way this is a start, see how you go with an array of structs.



Thanks, for all the productive feedback. You have identified many of the problems that I have been in running in circles with. Now, I have to list all of these practical approaches, digest all of the data, and do some research. Your help has provided a great deal of much needed direction.
Topic archived. No new replies allowed.