Random Quiz Questions

Pages: 12
closed account (D80DSL3A)
Glad that 1st program worked at your end. Sorry the 2nd program is so complicated. I don't think I would understand it either if I hadn't written it myself. Great exercise for me though.

I thought I'd try to show you a few things about your last post.
Your lines # 11-14 aren't doing anything. Look again at line 3
b = iNumber==12 ? 1 : 0;
Here b gets assigned a value of 1 or 0 depending on the outcome of the conditional test iNumber == 12 . Using the == makes a logical comparison between iNumber and the number 12.
if iNumber is equal to 12 then the result is true and b is assigned the value 1. If iNumber equals 3 (for example) then iNumber == 12 is false and b is assigned the value 0.
This iNumber==2 ? 2 : 0; makes the test but the result (2 or 0) isn't assigned to anything. I hope that made sense.
Next up is the if() statement. What goes in the parentheses is a conditional test, something which may be either true or false. A positive # evaluates as true. Example:
1
2
if ( 2 )// always true
    goto Question 2;// this will always be executed regardless of the value of iNumber. 

What you want is this:
1
2
if ( iNumber == 2 )// true only when iNumber is actually equal to 2
    goto Question 2;// this is executed only when iNumber equals 2 

You will find that this works much better:
1
2
3
4
5
6
7
8
cout << "Pick a number (2-5)" << endl;
cin >> iNumber;

if( iNumber == 2 )
    goto Question 2;

if( iNumber == 3 )
    goto Question 3;

And so on.
The use of goto is discouraged because it can quickly cause confusion. See if you can branch to each question using a switch statement instead. Have fun!
Ah I see. But I think I will continue to use goto because the use of it is much simpler. And it shouldn't be that confusing if I label it well enough. Thanks again for all of your help
closed account (D80DSL3A)
Again, you're welcome but I see that my "help" hasn't done much good. Little has changed in your code since the start of this thread. fun2code out.
Topic archived. No new replies allowed.
Pages: 12