0

I am currently tidying up my code for Mario problem for pset1 and I got stuck on the part where the user should be prompted for the integer again if the input is empty. I've been looking on various forums for 4 hours already and I can't seem to find an answer. It's either too complicated for me to understand and I doubt that such complex syntax is required to finish pset1 successfully or it doesn't work for me.

So far I tried to use two constructs for rejecting unwanted input and both of them worked equally well for me (they seem to reject everything, except for the empty input).

Here are they:

1) I used this one first, because I forgot about GetInt() from cs50 library. I found it here: https://stackoverflow.com/a/14099507

#include <stdio.h>

int clean_stdin()
{
    while (getchar() != '\n');
    return 1;
}

int height;

int line = 0;
int spc = 1;
int hash = 0;
int spcounter = 1;
int hashcounter = 1;
char z;

int main(void)
{
    // prompts user for integer between 0 and 23
    do                               
    {                               
        printf("Height:");        
    }    
    while (((scanf("%d%c", &height, &z)!=2 || z !='\n') 
    && clean_stdin()) || height < 0 || height > 23);

In this example I thought that the function clean_stdin() should take care of an empty input, but it doesn't.

2) Then, I found that many people asked similar question and they were advised to use the cs50 code GetInt() which, evidently, makes the problem of rejecting incorrect input much easier and the code tidier, yet it still doesn't handle the empty input. If I hit enter when the program runs, instead of prompting me for input again, a new line is created. Here is the code:

#include <stdio.h>
#include <cs50.h>

int clean_stdin()
{
    while (getchar() != '\n');
    return 1;
}

int height;

int line = 0;
int spc = 1;
int hash = 0;
int spcounter = 1;
int hashcounter = 1;
char z;

int main(void)
{
    // prompts user for integer between 0 and 23
    do                               
    {                               
        printf("Height:");
        height = GetInt();        
    }    
    while (height < 0 || height > 23);

What should I add to any of these two codes, so they reject empty input too? I can post my full code if you would like to.

Thanks in advance!

2
  • Are you sure that it doesn't work? I have essentially the same code and it works fine. #include <stdio.h> #include <cs50.h> int main(void) { // prompts user for integer between 0 and 23 int height = -1; do { printf("Height:"); height = GetInt(); } while (height < 0 || height > 23); printf("Height =%I\n", height); } The biggest difference is that I don't use any global vars where you have a number of them. (Global vars are generally not a good practice.)
    – Cliff B
    Commented Apr 4, 2015 at 7:58
  • @CliffB False alarm. I wasn't paying attention and was compiling the first code when I thought that I was compiling the second one. The second one works just fine.
    – Mayakovsky
    Commented Apr 4, 2015 at 21:32

1 Answer 1

1

The good thing about GetInt is that it does validation on input for you so you don't need to care about all the complexity in that (which you shouldn't really in the very first weeks). However, many times we get a bit curious and need more challenges.

In your first example, the function doesn't take care of empty input because scanf gets called first and most conversion characters in scanf discard any initial number of white spaces (including '\n') so it keeps waiting for you to input ordinary input (anything but white spaces).

GetInt gets over that with a bit more clever yet a bit more complicated approach. It basically uses GetString which returns an empty string in case the user just enters a newline character. Then it tries to read that int value from that string in which case the matching fails (because the string is empty) and it prompts the user to retry in that case.

You may take a look at the source code for both GetInt and GetString here! Feel free to skip that though if you get confused. All of that is issued in more details later in the course.

I think your second program works as expected. Here's a screenshot: enter image description here

1
  • Thanks for your explanation! I was doing this problem late at night and was pretty tired, so I didn't notice that when I thought I was checking the second code, I was actually compiling the first one again(the first is named mario.c, while the second one is mario1.c). So, naturally, I thought that the second code is not working just like the first one. What a stupid mistake!
    – Mayakovsky
    Commented Apr 4, 2015 at 21:30

You must log in to answer this question.

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