-
January 26th, 2014, 02:34 PM
#1
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;
}
-
January 26th, 2014, 02:56 PM
#2
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?
-
January 26th, 2014, 03:31 PM
#3
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
February 15th, 2014, 06:04 PM
#4
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.
-
February 16th, 2014, 04:28 PM
#5
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 !!!
Last edited by Jose M; February 16th, 2014 at 04:32 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|