JavvaTheHutt
JavvaTheHutt

Reputation: 195

Accepting empty strings in C

My program can either accept a number of any length or empty input. However, if the input is empty (space or newline), the program continues to wait for an input. I also tried fgets but if space/newline is pressed, it still waits for more input that is not a space/newline before closing.

Simpified code:

#include <stdio.h>
main()
{
    int num;
    scanf("%i",&num);
    printf("%i",num);
}

Input:

363792 

Output:

363792

Desired:

Input:

Output:

I'm new to C and am having a very hard time accomplishing this.

What tried using fgets:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main()
{
    int n;
    char s[20];

    fgets(s,20,stdin);
    n = atoi(s);

    printf("%i",n);
}

Edit: Turns out I was not compiling the code right. So every time I tried to make changes, it just looked at the original code using scanf.

Upvotes: 18

Views: 12419

Answers (4)

ashiquzzaman33
ashiquzzaman33

Reputation: 5741

According to definition of scanf()

The function will read and ignore any whitespace characters encountered before the next non-whitespace character.

So you can not get your desired result using scanf(). As gets() is not safe and it misbehaves badly. you can use fgets() to get input and convert it to integer if you need using atoi().

Here sample code you can try:

#include <stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
    int num;
    char ch[100];
    fgets(ch, sizeof ch, stdin);
    if (strlen(ch)>1&& ch[strlen(ch)-1] == '\n')
    {
        ch[strlen(ch)-1] = '\0';
        num = atoi(ch);
        printf("%i\n", num);
    }
}

Upvotes: 8

Santosh A
Santosh A

Reputation: 5351

I also tried fgets but if space/newline is pressed, it still waits for more input that is not a space/newline before closing.

Firstly fgets will work in this case. If you had shown us what exactly you tried to do using fgets() then answer to this question would have much narrow or very specific.

I tried to do the same using fgets() and below is the code snippet.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{  
    char string[100];
    printf("Enter a number: ");

    fgets (string, 100, stdin);
    /* Here remove  the trailing new line char; not required unless you are trying to print the string */
    string[strlen(string) - 1] = '\0';

    printf("Num is %d\n", atoi(string));

    return 0;
}

If there is no input or just enter or space and enter, then number printed will be zero and fgets() will not wait until you enter a valid number.

Also check this on why you shouldn't use gets. Do not use gets()

Upvotes: 27

user4580220
user4580220

Reputation:

alright, you do not understand what scanf() does, it goes and scans entire input string and looks for special characters like %d, %f, %s and until it does not get what is in string it will wait.

scanf("%d %d", a, b);

will get done only when you enter 2 integer values, if you press space it does not count as integer value so it will ignore it.

Upvotes: 1

dimlucas
dimlucas

Reputation: 5131

You should read the input character by character and stop reading if the read character is not a number:

char ch;
while(1)
{
    ch=getchar();
    if(isdigit(ch))
    {
        putchar(ch);
    }
    else
    {
        break;
    }
}

Upvotes: 5

Related Questions