1

Ok, I've beat my head on the wall for over a week on this issue and I'm just over it at this point. Here's the code I have, I'll post the issue at the bottom of the code. As a side note, I am a first time computer guy who would classify himself in the super uncomfortable with this stuff category, so please, any tips, tricks, etc would be amazing.

  int height;

do { printf("Please provide a number between 1 and 23: "); height = GetInt(); } while (height < 0 || height > 23); { printf("%i is not between 1 and 23, please try again: ", height); height = GetInt(); }

UPDATE

I have updated the code to the DO/WHILE loop I am running

Now with the DO/WHILE loop the program does not run correctly at all. If you enter a valid number, you have to enter it again because it tells you it was incorrect, like this:

jharvard@appliance (~/Dropbox/pset1): ./mymario2 Please provide a number between 1 and 23: 3 3 is not between 1 and 23, please try again: 3 # ##

#

jharvard@appliance (~/Dropbox/pset1):

If I repeatedly enter an invalid number, it will re prompt, but it uses the incorrect prompt:

jharvard@appliance (~/Dropbox/pset1): ./mymario2 Please provide a number between 1 and 23: 24 Please provide a number between 1 and 23: 24 Please provide a number between 1 and 23: 24

The other issue with this loop is that if 0 is used repeatedly, the program will again terminate, but it at least uses the correct prompt:

jharvard@appliance (~/Dropbox/pset1): ./mymario Please provide a number between 1 and 23: 0 0 is not between 1 and 23, please try again: 0 jharvard@appliance (~/Dropbox/pset1):

With the IF/ELSE IF loop I was running previously it would work either way, it just wouldnt re prompt the user a second time if a second invalid number were entered. At this point I dont know where else to go, as I previously stated, ive tried all manner of different loops but none will work. I think Im going to revert back to my IF/ELSE IF loop and just submit and be done with it. As a very beginning, first time programmer this is just too damn frustrating.

  • Rick, if you are on reddit, please send me a private message there and I'll be happy to help. Stack Exchange isn't really good for one-on-one help (particularly trying to keep the Honor Code in place). here's the direct link to message me: reddit.com/message/compose/?to=delipity – curiouskiwi Oct 30 '14 at 22:49
1

The issue is that if you enter another invalid number it doesn't prompt you again

You need a do while loop if you want something to keep prompting until a certain condition.

do
{
    // do this code
}
while (this is true)

In your case you want to prompt the user for a number while the number they've given you is not within the correct range (it's < 0 or it's > 23).

So you are almost there!


edit

Inside the do block, you want to prompt the user and get the height. That's it. If the while condition is true (i.e., they didn't enter the right thing), the block will simply repeat. You don't need to add another prompt or do any other testing of the value.

do
{
    // prompt user for height
    // store the value from GetHeight into variable
}
while (the height is not in range)

Can you see how that would work?

It's also a good idea to run the staff version ~cs50/pset1/mario to see how they handle it.

| improve this answer | |
  • So I tried to change it to a do while loop, but now its even worse. Heres the new code, ill put what it does below: { int height; do { printf("Please provide a number between 1 and 23: "); height = GetInt(); } while (height <= 0 || height > 23); { printf("%i is not between 1 and 23, please try again: ", height); height = GetInt(); } This doesnt work at all now. Even if a valid number is given. – Rick Mazurek Oct 30 '14 at 16:57
  • I don't see the word while in there? I updated my answer. Perhaps that will help. – curiouskiwi Oct 30 '14 at 20:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .