Thread: scanf not working

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    14

    scanf not working

    Hi,

    My program works fine but after execution it does not allow me to scanf anything at the end of program, etc (R to reset Q to quit).
    thanks in advance.



    Code:
     
    #include <stdio.h>
    #include <stdlib.h>
    #define    A     "Candy"
    #define    A_P    4.55
    #define    B     "Flour"
    #define    B_P    1.5
    #define    C     "Soup"
    #define    C_P    2.88
    #define    D     "Walnuts"
    #define    D_P    5.25
    #define    E     "Rice"
    #define    E_P    1.05
    #define    F    "Noodles"
    #define    F_P    0.35
    #define    G    "Sugar"
    #define    G_P    0.92
    #define    H    "Cake"
    #define    H_P    1.45
    
    char menu (float candy_1 , float flour_1, float soup_1, float walnuts_1,  float rice_1, float noodles_1, float sugar_1, float cake_1);
    
    int main ()
    {
        char choice;
        float candy_pound = 0, flour_pound = 0, soup_pound = 0 ,  walnuts_pound = 0, rice_pound = 0, noodles_pound = 0,  sugar_pound = 0,   cake_pound = 0;
        printf("WECOME\n");
        printf("Please PRESS ENTER FIRST and then choose from the following choices\n");
        printf("Choice [A] for candy\n");
        printf("Choice [B] for flour\n");
        printf("Choice [C] for soup\n");
        printf("Choice [D] for walnuts\n");
        printf("Choice [E] for rice\n");
        printf("Choice [F] for noodles\n");
        printf("Choice [G] for sugar\n");
        printf("Choice [H] for cake\n");
        printf("Choice [R] to reset program\n");
        scanf("%c", &choice);
        while ((choice = getchar()) != '#')
        {
            switch(choice)
            {
                    
                case 'A':
                case 'a':
                    
                    printf("Please enter number of pounds\n");
                    scanf("%f", &candy_pound);
                    while (candy_pound<=0)
                    {
                        printf("\a\n");
                        printf("Oops, you have entered negative pound\n");
                        printf("Please try again\n");
                        scanf("%f",&candy_pound);
                    }
                    
                    break;
                    
                case 'B' :
                case 'b':
                    
                    printf("Please enter number of pounds\n");
                    scanf("%f", &flour_pound);
                    while (flour_pound<=0)
                    {
                        printf("\a\n");
                        printf("Oops, you have entered negative pound\n");
                        printf("Please try again\n");
                        scanf("%f",&flour_pound);
                    }
                    
                    break;
                    
                case 'C' :
                case 'c' :
                    
                    printf("Please enter number of pounds\n");
                    scanf("%f", &soup_pound);
                    while (soup_pound<=0)
                    {
                        printf("\a\n");
                        printf("Oops, you have entered negative pound\n");
                        printf("Please try again\n");
                        scanf("%f",&flour_pound);
                    }
                    break;
                    
                case 'D':
                case 'd':
                    printf("Please enter number of pounds\n");
                    scanf("%f", &walnuts_pound);
                    while (walnuts_pound<=0)
                    {
                        printf("\a\n");
                        printf("Oops, you have entered negative pound\n");
                        printf("Please try again\n");
                        scanf("%f",&walnuts_pound);
                    }
                    break;
                case 'E':
                case 'e':
                    printf("Please enter number of pounds\n");
                    scanf("%f", &rice_pound);
                    while (rice_pound<=0)
                    {
                        printf("\a\n");
                        printf("Oops, you have entered negative pound\n");
                        printf("Please try again\n");
                        scanf("%f",&rice_pound);
                    }
                    break;
                case 'F':
                case 'f':
                    printf("Please enter number of pounds\n");
                    scanf("%f", &noodles_pound);
                    while (noodles_pound<=0)
                    {
                        printf("\a\n");
                        printf("Oops, you have entered negative pound\n");
                        printf("Please try again\n");
                        scanf("%f",&noodles_pound);
                    }
                    break;
                case 'G':
                case 'g':
                    printf("Please enter number of pounds\n");
                    scanf("%f", &sugar_pound);
                    while (sugar_pound<=0)
                    {
                        printf("\a\n");
                        printf("Oops, you have entered negative pound\n");
                        printf("Please try again\n");
                        scanf("%f",&sugar_pound);
                    }
                    break;
                case 'H':
                case 'h':
                    printf("Please enter number of pounds\n");
                    scanf("%f", &cake_pound);
                    while (cake_pound<=0)
                    {
                        printf("\a\n");
                        printf("Oops, you have entered negative pound\n");
                        printf("Please try again\n");
                        scanf("%f",&cake_pound);
                    }
                    break;
                case 'R':
                case 'r':
                {
                    candy_pound=0, flour_pound=0, soup_pound=0,  walnuts_pound=0, rice_pound=0, noodles_pound=0, sugar_pound=0,  cake_pound=0;
                    break;
                }
                case 'q':
                case 'Q':
                    exit(0);
                    
                default :
                    
                    printf("Oops you have entered invalid choice. Please choose again\n");
                    
            }
            
            while (getchar() != '\n')
            continue;
            printf("Please enter another choice or enter # to checkout\n");
            
        }
        choice = menu(candy_pound, flour_pound, soup_pound, walnuts_pound, rice_pound, noodles_pound, sugar_pound, cake_pound);
        
    }
    
    
    
    char menu (float candy_1 , float flour_1, float soup_1, float walnuts_1,  float rice_1, float noodles_1, float sugar_1, float cake_1)
    {
        char choice1;
        float subtotal=0, totalpounds=0, averagecost=0, hst=0, total=0;
        float candy_1_total =0 , flour_1_total = 0 , soup_1_total=0,  walnuts_1_total=0, rice_1_total=0, noodles_1_total=0, sugar_1_total=0,  cake_1_total=0;
        candy_1_total = A_P* candy_1;
        flour_1_total = B_P* flour_1;
        soup_1_total = C_P* soup_1;
        walnuts_1_total = D_P* walnuts_1;
        rice_1_total = E_P* rice_1;
        noodles_1_total = F_P* noodles_1;
        sugar_1_total= G_P* sugar_1;
        cake_1_total= H_P* cake_1;
        subtotal = candy_1_total+  flour_1_total+  soup_1_total+    walnuts_1_total+ rice_1_total+  noodles_1_total+  sugar_1_total+  cake_1_total;
        totalpounds =  candy_1 + flour_1 + soup_1 + walnuts_1 + rice_1 + noodles_1 + sugar_1 + cake_1;
        hst = subtotal * 0.13;
        total = subtotal + hst;
        
        if (subtotal > 0  && totalpounds > 0)
        {
            averagecost = subtotal/totalpounds;
        }
        
        printf(" WELCOME TO THE BULK FOOD STORE PROGRAM\n");
        printf("Item \t Description \t Cost/LB \t\t # Pounds \t Subtotal\n");
        printf("A\t\t %s\t\t\t %2.2f\t\t\t %2.2f\t\t $  %2.2f\n",A, A_P, candy_1, candy_1_total);
        printf("B\t\t %s\t\t\t %2.2f\t\t\t %2.2f\t\t $  %2.2f\n",B, B_P, flour_1, flour_1_total);
        printf("C\t\t %s\t\t\t %2.2f\t\t\t %2.2f\t\t $  %2.2f\n",C, C_P, soup_1, soup_1_total);
        printf("D\t\t %s\t\t %2.2f\t\t\t %2.2f\t\t $  %2.2f\n",D, D_P, walnuts_1, walnuts_1_total);
        printf("E\t\t %s\t\t\t %2.2f\t\t\t %2.2f\t\t $  %2.2f\n",E, E_P, rice_1, rice_1_total);
        printf("F\t\t %s\t\t %2.2f\t\t\t %2.2f\t\t $  %2.2f\n",F, F_P, noodles_1, noodles_1_total);
        printf("G\t\t %s\t\t\t %2.2f\t\t\t %2.2f\t\t $  %2.2f\n",G, G_P, sugar_1, sugar_1_total);
        printf("H\t\t %s\t\t\t %2.2f\t\t\t %2.2f\t\t $  %2.2f\n",H, H_P, cake_1, cake_1_total);
        printf("\ntotal pounds = %2.2f\t\t\t SUBTOTAL\t$  %2.2f", totalpounds, subtotal);
        printf("\nAvg Cost/Lb = %2.2f\t\t\tHST Ammount\t$  %2.2f", averagecost, hst);
        printf("\n\n\t\t\t\t\t\tTOTAL Ammount\t$  %.2f", total);
        
        printf("\n\nYour choice (R to RESET, Q to QUIT) ==>");
        scanf("%c", &choice1);
        return(choice1);
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I suspect you have some newlines floating around the input buffer from previous "scanf()" calls, and they are being picked up by subsequent "scanf()" calls scanning for character input.

    FAQ > How do I avoid a "dangling" newline when reading single character user input? - Cprogramming.com

    I also suspect you somewhat convoluted your logic to "correct" for this, but without a full understanding of the issue at hand (as explained in the link above). Perhaps with a better understanding of issues associated with "scanf()"ing for characters, you can approach the implementation of user input more directly.

  3. #3
    Registered User
    Join Date
    Feb 2015
    Posts
    14
    Thank you, problem solved .

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
        scanf("%c", &choice);
        while ((choice = getchar()) != '#')
    using these 2 lines - the value read by scanf will be overwritten by getchar - so you have to fix your logic as well
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scanf Working ?
    By Geetika in forum C Programming
    Replies: 12
    Last Post: 12-05-2013, 09:36 AM
  2. scanf not working
    By sourpatchkid in forum C Programming
    Replies: 2
    Last Post: 04-27-2013, 10:10 AM
  3. scanf is not working
    By learning_grc in forum C Programming
    Replies: 9
    Last Post: 11-16-2011, 04:09 PM
  4. Working with strings [using scanf()]
    By Luponius in forum C Programming
    Replies: 4
    Last Post: 04-26-2011, 10:27 AM
  5. Every other scanf not working
    By Rob20050 in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 12:48 AM