Loop Trouble with For Loop
Ok so this is my homework and I am not asking anyone to do it for me. I am asking where I have gone wrong I am writing for loop with a switch so that scores can be inputted in by a judge. The issue that I am running into is that I will put out an the text then the test happens and the code puts out the switch statement 5 times with random number. Here is what I have written.
Thank you for your help. I am just not seeing where I am wrong. Once again I am not asking you do my work for me just to look at what I am missing.
Code:
int main()
{
int diver;
int option;
int Judge;
cout << "Enter Divers Name:";
cin >> diver;
for (option=0; option <5; option++)
//if (option)
cout << "Judge Please select a number between 0-9:" << Judge << endl;
cin >> option;
cout << option << endl;
option++;
do{
switch (option)
{
case 1: cout << "Score is 1"<< endl;
break;
case 2: cout << "Score is 2"<< endl;
break;
case 3: cout << "Score is 3"<< endl;
break;
case 4: cout << "Score is 4"<< endl;
break;
case 5: cout << "Score is 5"<< endl;
break;
case 6: cout << "Score is 6"<< endl;
break;
case 7: cout << "Score is 7"<< endl;
break;
case 8: cout << "Score is 8"<< endl;
break;
case 9: cout << "Score is 9"<< endl;
break;
case 0: cout << "Score is 10"<< endl;
break;
default: cout << "Invalid Entry" << endl;
break;
}
}
while (Judge < 5);
return 0;
}
Re: Loop Trouble with For Loop
Currently your for-loop only iterates over the statement cout << "Judge Please select a number between 0-9:" << Judge << endl; I guess you would like to control everything beyond the for by that iteration - if that's right you need to put all the stuff into a block, i.e. {}.
There may be other flaws as far as I understood your description:
- There is no randomization anywhere - probably you are asked to generate a random number between 0 ... 9 five times? For a solution of this you may have a look at the documentation for rand() and the modulo-operator
- You are incrementing option twice - why?
Re: Loop Trouble with For Loop
Where do you set the variable Judge?
You ask to enter divers name, but the variable diver is defined as type int not a string (or a c-style char array).
Your case 0: will not be executed if the user inputs a number in the required range as you increment option which gives option a range of 1 - 10.
Re: Loop Trouble with For Loop
Also, you might consider using the lines :
if(option <= 10 && option >= 1)
cout<<"The score is "<<judge<<endl;
else
cout<<"Invalid Entry.";
instead of the entire switch statement.
Re: Loop Trouble with For Loop
Judge is the Divers name so the type of Judge should be string or c-style;
So instead of:
You should write;
or;
Code:
char Judge[10];//we reserve 10 characters
Other thing, the line
doesn't make sense. WhY??
And finally....
I think you forgot deleting the line:
Code:
for (option=0; option <5; option++)
Because that loop is executing 5 times the next line:
Code:
cout << "Judge Please select a number between 0-9:" << Judge << endl;
and I am sure that that isn't teh result you want.
I hope I could help you !!!:wave: