C Function won't scan for user inputted values

This is a discussion on C Function won't scan for user inputted values within the C Programming forums, part of the General Programming Boards category; Hey! I'm trying to write a C program that will eventually do operations with fractions. However, I'm stuck on one ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    C Function won't scan for user inputted values

    Hey!

    I'm trying to write a C program that will eventually do operations with fractions. However, I'm stuck on one part. The section of code for the function scanOperator, when run through the program, does not allow for user input. The weird thing is when I change %c to %d, it will allow input. However, with %c, it simply prints the line "Enter an arithmetic operator (+, -, *, /)", then exits the program. I can't figure out why it does this.

    Thanks in advance!

    Code:
    #include <stdio.h>
    
    //this function prints the user inputted fractions
    void printFraction (int n, int d)
    {
    printf("%d/%d\n",n,d);
    }
    
    
    //this function will scan for user input
    void scanFraction (int *n, int *d)
    {
    printf("Enter the numerator and denominator of a common fraction: ");
    scanf("%d %d",&*n,&*d);
    
    
    if (*d <= 0)
    {
    while (*d <= 0)
    {
    printf("The common fraction you entered is invalid.\n");
    printf("Enter the numerator and denominator of a common fraction: ");
    scanf("%d",&*n);
    scanf("%d",&*d);
    }
    }
    else
    printf("You have entered: ");
    
    printf("You have entered: ");
    }
    
    
    
    //this function will print the operator entered by the user
    char scanOperator(void)
    {
    char op;
    printf("Enter an arithmetic operator (+, -, *, /): ");
    scanf("%c",&op);
    
    return op;
    }
    
    
    
    
    
    
    
    
    int main (void)
    {
    int a,b;
    char operator;
    scanFraction(&a,&b);
    printFraction(a,b);
    scanOperator();
    return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Take a look at your code...

    First you are not making any use of the returned value from scanOperator().
    Second the program ends because it hits the end of main... return 0;

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Quote Originally Posted by CommonTater View Post
    Take a look at your code...

    First you are not making any use of the returned value from scanOperator().
    Second the program ends because it hits the end of main... return 0;
    I don't need the program to utilize the returned value yet, I'm not at that part. My problem is that I can't get the function to scan for an operator character. At one point I had written the rest of the function where it was supposed to theoretically print back what the user inputted, but for some reason it just didn't allow the user to input a value. I removed that part to see if I could simply get it to scan, but it doesn't.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That's the most frequent question we get, here.

    The answer is that scanf() LEAVES BEHIND the newline char from when you hit the enter key, right in the keyboard buffer.

    So if scanf() is looking for a char, it see's the char in the buffer, gets it, and says "OK, I've got a char", and the program continues on.

    If you are using scanf() for a number, then it's not a problem, because scanf() will see, but pass over, the char, knowing it's too small for what it needs.

    The answer is to add a getchar() after each scanf(), to pull the newline char, off the keyboard buffer. But you really only need it after the last scanf(), before the scanf() that is asking for a char.

    So: All numbers - don't need getchar(). Numbers or strings with a scanf() for a char later on, need a getchar().

    Using a getchar() after a number has a side benefit, also. If the user enters a letter, instead, the getchar() will fix the input buffer, and stop the program from going "NUTS!", and looping forever. Little sample:

    Code:
    int num=0, count = 0;
    
    do {
      printf("\nEnter any letter and watch! [100 to quit]: ");
      scanf("%d", &num);
      count++;
      if(count > 10000) {
        printf("\n Went a bit nuts there, didn't it? Now using getchar() to fix this.");
        getchar();  
      }
    }while(num < 100);
    getchar() is like a blood purifier, for the input stream. And scanf() is a rather brittle input function to use, but it can work - once you know the tricks to make it behave.

    fgets() is a much more robust input method. Look into it, but scanf() is in all the texts and classes, so you have to know how to use it, also.

    And Welcome to the Forum, Kevin250!
    Last edited by Adak; 10-20-2010 at 08:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM
  5. Replies: 5
    Last Post: 02-08-2003, 06:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21