C first Script Question


 
Thread Tools Search this Thread
Top Forums Programming C first Script Question
# 1  
Old 01-04-2013
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:

Code:
/*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

??
# 2  
Old 01-04-2013
Code:
#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.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 01-04-2013
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 ?
# 4  
Old 01-04-2013
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.

Code:
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.

Last edited by Corona688; 01-04-2013 at 02:18 PM..
These 2 Users Gave Thanks to Corona688 For This Post:
# 5  
Old 01-04-2013
speechless. Many Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script question

I know some tricks like this : echo " E"; sleep 0.1; clear; echo " Er"; sleep 0.1; clear; echo " Err"; sleep 0.1; clear; echo " Erro"; sleep 0.1; clear; echo " Error ";... (2 Replies)
Discussion started by: hakermania
2 Replies

2. Shell Programming and Scripting

script question

How can I include a counter in this if statement so only sends out 5 notifications. # the if statement will check for the lines status, if status is down sends email if then LIST="user@email.com" mail -s "rje_lines_down" $LIST < ${tmpfile} #sends an email to list fi thanks... (2 Replies)
Discussion started by: d4n3lu
2 Replies

3. UNIX for Dummies Questions & Answers

Script Question

I have a script that will calculate some information about the current directory that I run the script in. How can I have it where it gets an argument from the user(such as a directory) and the script calculate the information for the given directory? Any help is greatly appreciated. (8 Replies)
Discussion started by: tr32
8 Replies

4. Shell Programming and Scripting

Script question

I am trying to write a script to check the STIG our unix boxes. I want to put in checks to determine if certain files or directories are there. For example: If /opt/SUNWexplo exists echo "Sun Explorer exists" else echo "Sun Explorer is not installed" I am sure I could write... (2 Replies)
Discussion started by: rkruck
2 Replies

5. Shell Programming and Scripting

Script Question

Here is my problem. I have a txt file with a list of user names in both upper case and lower case. I need to remove the names from the passwd file. I want to be able to run a script that looks at my txt list and then removes those names from the passwd file. It would be nice if it backed up the... (3 Replies)
Discussion started by: wgartin
3 Replies

6. Shell Programming and Scripting

script question

Anyone know why this won't work? #!/usr/bin/ksh for db in `cat /etc/oratab|egrep ':N|:Y' | grep -v \* | cut -f1 -d":"` do echo "************************" echo "database is $db" echo "************************" done I am getting an error on the line... (7 Replies)
Discussion started by: ace2000
7 Replies

7. UNIX for Dummies Questions & Answers

Script question

Can anyone help with these scripts? Im new to this and struggling. Thank you for your help. Pre-requisites Create a file with x amount of lines in it, the content of your choice. (Have already done this) Script 1 Write a script that takes two arguments. The first being a line of text,... (3 Replies)
Discussion started by: beginner1
3 Replies

8. Shell Programming and Scripting

script question

I'm using solaris and i have an old script that an ex employee wrote but i can't seem to get it working. well is just one part of the script if ($?MC_PROD_DIR == 0) setenv MC_PROD_DIR $HOME/PRODUCTION source $MC_PROD_DIR/scripts/localenv ** i understand what this part of the script is... (1 Reply)
Discussion started by: fusionjd
1 Replies

9. UNIX for Dummies Questions & Answers

script question

Correct me if i'm wrong, but by adding "#!/bin/ksh" at the start of a script will force it to run in the korn shell no matter which shell you are currently using?? (2 Replies)
Discussion started by: shad0w75
2 Replies

10. UNIX for Dummies Questions & Answers

Another script question.

Hi, First off I usually script in the bash shell. Ok, in my script I am checking to see if the filename has a .txt extension. So I was trying: if then echo "Must contain a valid .txt extension" fiandif ] then echo "Must contain a valid .txt extension" fiBut no go the first... (5 Replies)
Discussion started by: Astudent
5 Replies
Login or Register to Ask a Question