Thread: Why the function getchar () is skipped, and does not run in the above program in C

  1. #1
    Registered User
    Join Date
    Aug 2018
    Posts
    3

    Why the function getchar () is skipped, and does not run in the above program in C

    Code:
    /*I tried to make a simple program, that calculate: 
     *1) how much money will cost us a product if we have percentage decrease for it; 
     *2) how much money we can save after buying a reduced/decreased product versus its real price; */
    
    
    /*I want to implement some of the two cases in switch statement, but the program does not allow me to do that, because the getchar () function is skipped*/
    
    
    #include <stdio.h>
    
    
    int main (void)
    {
        float real_price, percentage;
        char ch;
    
    
        printf ("Enter the real price of the product: ");
        scanf ("%f", &real_price);
        printf ("\nEnter the percentage decrease: ");
        scanf ("%f", &percentage);
    
    
        printf ("\nPress 1 if you want to calculate the PRICE OF THE PRODUCT AFTER THE REDUCING\nPress 2 if you want to calculate THE REDUCED PRICE OF THE PRODUCT: ");
        ch = getchar ();  //this statement is not implemented
    
    
        switch (ch) {
            case '1':
                printf ("The price of the product after the reducing is: %f ", real_price - (percentage / 100) * real_price);
    
    
            case '2':
                printf ("The reduced price of the product is: %f", (percentage/100)*real_price);
                break;
    
    
            default:
                printf ("getchar () is not implemented\n");  //respectively this statement is implemented
        }
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    scanf leaves '\n' in the input buffer, that's what getchar reads so it doesn't stop for input.
    To fix put another getchar after the last scanf.

  3. #3
    Registered User
    Join Date
    Aug 2018
    Posts
    3
    I understand you and i am thankful for your explanation and advice, but I do not understand a few things associated about what you wrote about getchar()
    First thing: why scanf() still keeps the '\n' after releasing the '\n' on printf() ?
    Second thing: why scanf() does not release the '\n' on such functions as getche(), getch() ?
    Last edited by timcook; 08-07-2018 at 04:37 AM.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    1) Because its input is buffered, and it doesn't discard characters willy-nilly.
    2) Because getche() and getch() aren't buffered
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Aug 2018
    Posts
    3
    I understand the conception, and I will follow the URLs that you shared with me. Thank you very much!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 09-24-2015, 01:51 AM
  2. My setup() function is being skipped.
    By howardbc14 in forum C Programming
    Replies: 3
    Last Post: 05-23-2015, 01:47 PM
  3. My getline function is skipped whenver I use case 10.
    By howardbc14 in forum C++ Programming
    Replies: 2
    Last Post: 05-17-2015, 12:12 AM
  4. My setup() function is skipped.
    By howardbc14 in forum C Programming
    Replies: 3
    Last Post: 05-14-2015, 11:29 PM
  5. gets function skipped 1st time
    By ambrown782 in forum C Programming
    Replies: 6
    Last Post: 07-26-2014, 12:30 AM

Tags for this Thread