C first Script Question

Gents for 2013 I have embarked on learning C and just wrote my first script and need some help. In the learning process, I am trying to understand where I went wrong with my script. The flow of the script is as such:

Enter a number --> check if it is a number --> if it is not a number,print what it is --> finally if it not a number or a character, tell you that you are an "Idjit"

My script:

/*my first c script */

#include <stdio.h>

int main()
{
int num;

printf("Idjit enter a number:");
scanf("%d", &num);
while(num = "%d" && num > 0)
    {
    printf(" Your not an idjit. This is the number you entered %d\n",num);
    return(0);
    }
    if(num != "%d" && num = "%c")
    {
    printf("Idjit this a letter not a number %c\n",num);
    return(1);
    }
    else
    {
    printf("Give up. You are a true Idjit");
    return(1);
}

cli@user~/Documents/scripts/c-prog# gcc -o number number.c 
number.c: In function �main':
number.c:16:12: warning: comparison between pointer and integer [enabled by default]
number.c:16:27: error: lvalue required as left operand of assignment
number.c:25:1: error: expected declaration or statement at end of input

??

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

int main()
{
    char tmp[16]={0x0};  // read in strings it works better;
    int num=0;
    char *p;
    printf("Idjit enter a number:");
    fgets(tmp, sizeof(tmp), stdin);  // read tmp from keyboard
    //scanf("%d", &num);
    p=strchr(tmp, '\n');  // find the trailing newline character
    if(p!=NULL) *p=0x0;   // terminate the string on the \n character  
    num=atoi(tmp);    // we do not use this but here it is anyway
    if( isdigit(*tmp) )   // %d is a string, use the isdigit  
    {
        printf(" Your not an idjit. This is the number you entered %s\n",tmp);
        return(0);
    }
    if(!isdigit(*tmp) )  // it is not a digit
    {
       printf("Idjit this a letter not a number %s\n",tmp);  // you cannot print numbers that do not exist
       return(1);
    }
    else // you cannot get here from the above if() statement.
    {
       printf("Give up. You are a true Idjit");
       return(1);
    }
    
}

comments explain the changes.

1 Like

awesome.

---------- Post updated at 12:51 PM ---------- Previous update was at 12:47 PM ----------

Just out of curiosity, why didnt you want to use scanf ?

scanf() has an unfortunate problem where, if it finds any bad data in the stream, it puts it back. You are asking for %d, so it looks for digits. If it sees any non-digits, it will give up and not even show them to you.

Imagine you're trying to read 9 numbers with 9 scanf calls, and on the very first one you type in A then enter. The first scanf will see 'A', decide it's not a number, and put it back. This means the second will also see 'A'. So will the third, fourth, fifth, sixth, seventh, eighth, and ninth.

sscanf, which operates on strings instead of streams, avoids this problem, since it has nowhere to "put back" the letter into. It just uses the string you give it and doesn't care what happened last time. fgets() and sscanf() make a good team; fgets reads entire lines and turns them into strings, while sscanf() processes strings and turns them into what you want.

while(fgets(buf, 512, stdin) != NULL)
{
        int value;
        if(sscanf(buf, "%d", &value) == 1)
        {
                printf("You typed a number\n");
        }
        else
        {
                printf("You didn't type a number\n");
        }
}

Note that this will accept things like 1234a, since sscanf will stop at the a.

2 Likes

speechless. Many Thanks