Thread: Newbie question. What am I doing wrong?

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    26

    Newbie question. What am I doing wrong?

    Hello. For my assignment, my teacher wants me to get 2 numbers from the user, and then valide them. He wants us to use getchar( ). I can get most of it to work using scanf, but using getchar messes everything up. Please help me out.

    Code:
    #include <stdio.h>
    
    
    
    void main(void)
    
    {        
             char str[256];
             int in_val,
                 status;
             char skip_ch;
             int error;                           
    
        printf( "Please enter your name:\t " );
        gets(str);
        printf("Hello, %s\n", str);
       do { 
            
            
            error = 0;
            printf("Please enter an integer\t"); 
        in_val = getchar( );
       
        
        
        if (in_val != 1) {
                   error = 1;
                   scanf("%c", &skip_ch);
                   printf("Invalid character, %c", skip_ch);
    }
    do
      scanf("%c", &skip_ch);
      while (skip_ch != '\n');
    } 
    while (error);
    
    return (in_val);
    
    
    
    
        
               
    system("Pause");                      
    
    
    
    
    }
    when using scanf, I did:

    Code:
    status = scanf("%d", &in_val);
    Any ideas?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    getchar() returns ASCII values. So if the user typed in "1", then getchar() would receive '1', not the integer value of 1.
    Code:
    int charVal = getchar();
    int intVal = charVal - '0';

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    26
    I'm sorry, I don't understand. I tried it like this:

    Code:
       do { 
            
            
            error = 0;
            printf("Please enter an integer\t"); 
        int charVal = getchar();
        int intVal = charVal - '0';
       
        
        
        if (intVal != 1)
    
    ...
    but it doesn't work. It brings up the invalid character message every time.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    26
    I understand how to store the number using getchar (). The problem I am having is validating the number when using getchar (). This is what I need help with. Thank you for helping, though.

  6. #6
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Look up the header file ctype.h... There is a function/macro (depends on the compiler) defined called isdigit() that might be helpful.

    Also know that the ascii characters for '0' through '9' are consecutive, so a character that is < '0' or > '9' is not a decimal digit.
    Insert obnoxious but pithy remark here

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    26
    isdigit( ) did help, and I seem to have the numbers working (I just hope I did it correctly). This is what I have so far:

    Code:
             char str[256];
             int start;      
             int num;
             int numb;
             
    
        printf( "Please enter your name:\t " );
        gets(str);
        printf("Hello, %s\n\n", str);
      
            
            
            
            printf("Please enter two numbers between 0 and 9.\t"); 
        num = getchar ( );
       numb = getchar ( );
            if (isdigit (num))
    
       printf("First number is valid!\n");
       else printf ("Your first entry was not a number.\n");
       
       
           if (isdigit (numb))
       printf ("The second number is valid!\n");
      else 
           printf("The second entry was not a number.\n");
    
    
    if (num > numb)
    printf("The first number is greater!\n");
    
    if (num < numb) 
    printf("The second number is greater!\n");
    
    if (num == numb)
    printf("You entered the same number twice!\n");

    Now, I want it so that if either or both entries are not numbers, the program will ask for 2 more numbers. I assume there is a return value that can be used for this, but am not quite sure how this works.


    There are a few more parts to this that I need help with. I also need it to compare the numbers, and find out which one is smaller. Also, I need to print out all the numbers, starting with the smaller, between the two numbers entered. Any help would be greatly appreciated. Thanks.


    Edit:

    I figured out how to tell which number is greater, and I added it above. I still need help with the return, and printing the numbers between the two numbers....entered? I'm still not sure what that means.
    Last edited by kabuatama; 02-03-2006 at 09:20 PM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Sorry, but until you fix your teacher, helping you is a waste of time because nothing good we say will ever be used by you or your teacher.

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    26
    I've gotten most of it figured out. What I need help with is printing all the numbers between the numbers entered. This means that if the two numbers entered were "2" and "5," 3 and 4 would print out. This is what I have so far for this section:
    Code:
      for ( x = 0; x < 10;) 
      
      { 
          if (x < num)
          x++;
          if (x < numb)
          x++;
      if (x > num) {
            printf ("%d\n", x);
            x++; }
            else if (x > numb) {
                 printf ("%d\n", x);
            x++; }
            
            }
    it doesn't seem to work. I am guessing that I am using something wrong. Thanks if you can help.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Does this help?
    Code:
    int x, low, high;
    scanf("%i %i", &low, &high);
    for(x = low; x < high; x ++)
        printf("%i\n", x);
    }
    (%i is the same as %d.)

    Don't use gets(). Nor void main(). Perferably not system("pause"), either.
    Last edited by dwks; 02-04-2006 at 03:47 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie question about string processing
    By prihod in forum C Programming
    Replies: 6
    Last Post: 04-15-2008, 10:14 PM
  2. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  3. question from a newbie
    By yoda05378 in forum C Programming
    Replies: 3
    Last Post: 05-12-2007, 04:52 AM
  4. Total Newbie Question
    By Kid A in forum C++ Programming
    Replies: 4
    Last Post: 06-22-2006, 05:36 AM
  5. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM