Thread: C Function won't scan for user inputted value

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

    C Function won't scan for user inputted value

    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
    Quote Originally Posted by kevin250 View Post
    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;
    }

    First you are not using the return value for sccanOperator();
    Second it ends there because it runs out of things to do...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    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();
    
    
    getchar() ;
    return 0;
    }
    Ok dude, I have fixed your code as best as I can, because I have no idea what you want to do.
    I am surprised you didn't get bugs when you ran it.
    When you pass by point value, you don't have to dereference a the pointer again and use ampersand in your scanf(). That doesn't make sense. n and d have an address, so the address operator - & is irrelevant in this case.

    as for your scanOperator(), the reason it closes is because you do not use it. You return a value - the value is floating somewhere in memory it isn't used. return 0 ; just closes the program.

    Also your while loop is messed up.
    if the condition is true, who do you terminate the while loop?
    You do not decrement or increment.
    Look at your code again, fix it and write back.
    good luck

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    %c (unlike the rest of the normal conversions) does NOT skip whitespace.

    So you invariably get whatever trailing space/newline that some other conversion left behind.

    It might be easier to use %s, even if you´re just reading one character (but be aware that whatever you read, it will have a \0 appended, so allocate space accordingly)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Easiest thing to do would be to change the scanf to have a space in front of the %c. This will instruct the scanf function to ignore leading whitespace (such as the newline that's probably left over in the input buffer after the previous scanf calls and causing your troubles).

    Code:
    char scanOperator(void)
    {
        char op;
        printf("Enter an arithmetic operator (+, -, *, /): ");
        scanf(" %c",&op);  /* Notice space in front of %c */
    
        return op;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    even then surely his code still has errors in it..
    The while loop is infinite if the if statement is true
    and the way he inputs stuff in the Scan fraction, i think is wrong as well...

  8. #8
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by hk_mp5kpdw View Post
    Easiest thing to do would be to change the scanf to have a space in front of the %c. This will instruct the scanf function to ignore leading whitespace (such as the newline that's probably left over in the input buffer after the previous scanf calls and causing your troubles).

    Code:
    char scanOperator(void)
    {
        char op;
        printf("Enter an arithmetic operator (+, -, *, /): ");
        scanf(" %c",&op);  /* Notice space in front of %c */
    
        return op;
    }
    wow I didn't know putting a space and then the delimiter meant anything..wow cool

    since scanf() returns when it detects a newline character, I guess you can do this as well
    Code:
    while(getc(op)!='\n') ;
    //or 
    while(getchar(op)!='\n') ;
    something like that I believe
    the byte will be assigned to the variable op
    but it will remove the newline character from the buffer and any other stuff there..

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Quote Originally Posted by Eman View Post

    Ok dude, I have fixed your code as best as I can, because I have no idea what you want to do.
    I am surprised you didn't get bugs when you ran it.
    When you pass by point value, you don't have to dereference a the pointer again and use ampersand in your scanf(). That doesn't make sense. n and d have an address, so the address operator - & is irrelevant in this case.

    as for your scanOperator(), the reason it closes is because you do not use it. You return a value - the value is floating somewhere in memory it isn't used. return 0 ; just closes the program.

    Also your while loop is messed up.
    if the condition is true, who do you terminate the while loop?
    You do not decrement or increment.
    Look at your code again, fix it and write back.
    good luck
    I thought that since in the while loop the user could re enter a value that would be outside the loop conditions, that would terminate the loop?

    I've managed to get the program to scan for a value. There is still a slight problem though. The first two functions work and run fine. However, in the scanOperator function, if the user enters one of (+, -, *, /) the initial time, it will correctly print back the operator. However, if the user enters in an incorrect value, then regardless of what is printed the second time, it keeps saying it is incorrect. I can't seem to fix the problem which I believe would be in the loop.

    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: ");
    
    }
    
    
    
    //this function will print the operator entered by the user
    char scanOperator(void)
    {
    char op;
    printf("Enter an arithmetic operator (+, -, *, /): ");
    getchar();
    scanf("%c",&op);
    if (op =='+' || op =='-' || op=='*' || op =='/')
            printf("You have entered: %c",op);
    else
    {
            while (op !='+' || op !='-' || op!='*' || op !='/')
            {
            printf("The arithmetic operator you have entered is invalid.\n");
            printf("Enter an arithmetic operator (+, -, *, /): ");
            getchar();
            scanf("%c",&op);
            }
            printf("You have entered: %c\n",op);
    
    }
    
    
    return op;
    }
    
    
    
    
    
    
    
    
    int main (void)
    {
    int a,b;
    char operator;
    scanFraction(&a,&b);
    printFraction(a,b);
    scanOperator();
    return 0;
    }

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Try inserting this before each call to scanf() and getchar()

    Code:
    while (getchar());
    No matter how much junk is in your input buffer this will loop until it's empty. Then you can ask for user input knowing the next thing in line is what they type.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by kevin250 View Post
    However, in the scanOperator function, if the user enters one of (+, -, *, /) the initial time, it will correctly print back the operator. However, if the user enters in an incorrect value, then regardless of what is printed the second time, it keeps saying it is incorrect. I can't seem to fix the problem which I believe would be in the loop.
    Code:
            while (op !='+' || op !='-' || op!='*' || op !='/')
            {
            printf("The arithmetic operator you have entered is invalid.\n");
            printf("Enter an arithmetic operator (+, -, *, /): ");
            getchar();
            scanf("%c",&op);
            }
            printf("You have entered: %c\n",op);
    The problem is definitely your Boolean logic.

    op !='+' || op !='-' || op!='*' || op !='/'

    Go through this and evaluate every listed possibility in a truth table. Stop when you find truth. Your individual results should match this

    Code:
    OP	EVAL	RESULT
    +	0 || 1	1
    -	1	1
    *	1	1
    /	1	1
    You've just evaluated like the computer. It turns out that any other character will eventually evaluate to truth because it is not '/'. So there is your infinite loop. Use AND here. It works differently (since the expression is true only if all of the operands are true). Put the same expression with AND in a truth table and see why it works.

    Also if your loop and if-else tree evaluate the same condition the if-else tree is redundant.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive function
    By WatchTower in forum C Programming
    Replies: 11
    Last Post: 07-15-2009, 07:42 AM
  2. Brand new to C need favor
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 10:08 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM