• Post Reply Bookmark Topic Watch Topic
  • New Topic

Control Statement  RSS feed

 
Kevin Corre
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Program tests 10 questions then asks if you want to test again. If you select yes then it goes and only does 1 question and asks you again if you want to test. Need it to tests 10 questions each time. Something simple I know, just can't see it...sigh.



Thanks,
Kevin
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's about where you set the initial value of count. If you want to go round the loop again you need to reset count to 1 inside the loop (in fact, you may as well declare count inside the loop as well).
 
Kevin Corre
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which loop does it go in and how do I reset it? Tried to define it again and says count is already defined in main. Sorry for the brain damage just not seeing it.

Thanks,
Kevin
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the inner loop keeps going as long as count is less that 10. So setting it to 1 inside that loop would be bad - it would never exit! What you need to do is reset count just before you enter that loop. In other words, between lines 13 and 14.

You can't declare the same variable twice. but if you declared it on line 13½ you wouldn't need to declare it on line 8. Or, if that was needed (which would be the case if you referred to count anywhere outside the loops), just set it to 1 without redeclaring it.
 
Kevin Corre
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Matt, I knew it was something stupid and I had moved it there but had left the original declaration and thats why it wasn't working. I was just confused about what you said to declare it inside as well.

Thanks again,
Kevin
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a general guideline, your code will be clearer if you don't give variables bigger scope than they need. So, for instance, don't use a member variable when a local variable will do. And in this case, since the count variable is only referred to inside the loops, it makes sense to only declare them within the loops - which is why I suggested it.

In fact, having another look the same applies to name, input and repeat - none of them need to have a scope as big as they've been given.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!