18

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.

Santosh A
  • 5,173
  • 27
  • 37
JavvaTheHutt
  • 195
  • 1
  • 2
  • 8

4 Answers4

26

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()

Community
  • 1
  • 1
Santosh A
  • 5,173
  • 27
  • 37
  • I had tried code like this earlier. I just didn't realize that new code was not being compiled :( Thank you for the help! It works! – JavvaTheHutt Sep 16 '15 at 14:04
  • 2
    `string[strlen(string) - 1] = '\0';` incorrectly truncates when there is no `'\n'`. It is undefined behavior should the first character be an rare embedded `'\0'`. Better to use http://stackoverflow.com/a/28462221/2410359 – chux - Reinstate Monica Sep 16 '15 at 20:39
  • 2
    regarding this line: `string[strlen(string) - 1] = '\0';` this will not work correctly in all cases for two reasons. 1) as mentioned by chux, if no newline then the line will be corrupted. 2) if running under windows/dos, (where a newline is actually two characters) this will not removed the newline, just corrupt it. Suggest using something like: `char * offset = strstr( string, "\n" ); if (NULL != offset) *offset = '\0';` – user3629249 Sep 19 '15 at 07:30
  • @user3629249: Agreed. Thanks – Santosh A Sep 19 '15 at 09:42
8

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);
    }
}
ashiquzzaman33
  • 5,781
  • 5
  • 31
  • 42
5

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;
    }
}
dimlucas
  • 5,040
  • 7
  • 37
  • 54
1

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.