Thread: sequence issue in coding...

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    2

    sequence issue in coding...

    Code:
    #include <stdio.h>
    main()
    {
        float price, new_price;
        char code;
        
        printf("Enter the price: ");
        scanf("%f", &price);
        
        printf("Enter the pricing code: ");
        scanf("%c", &code);
    
    
        if(code=='A')
        {
            final_price=price*0.5;
            printf("New discounted price is %.2f\n", final_price);
        }
        else if(code=='B')
        {
            final_price=price*0.6;
            printf("New discounted price is %.2f\n", new_price);
        }
        else if(code=='C')
        {
            final_price=price*0.7;
            printf("New discounted price is %.2f\n", new_price);
        }
        else if(code=='D')
        {
            final_price=price*0.9;
            printf("New discounted price is %.2f\n", new_price);
        }
        else if(code=='E')
        {
            final_price=price;
            printf("New discounted price is %.2f\n", new_price);
        }
        else
        {
            final_price=price;
            printf("Invalid pricing code. \n");
        }
    
    }
    if i place:
    printf("Enter the pricing code: ");scanf("%c", &code);
    before
    printf("Enter the price: ");scanf("%f", &price);
    the program runs, but if i put it this way, it doesn't, please help me out here, thanks.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    When you type in a "price" and press enter, the "price" is stored - but the newline (from pressing enter) is waiting in the input buffer.

    Then, when you attempt to scan a single character, it reads the newline waiting in the buffer, making it appear as if the second "scanf()" is skipped.

    More information, as well as solutions, in the FAQ: FAQ > How do I avoid a "dangling" newline when reading single character user input? - Cprogramming.com

    I didn't actually try running your program because it can't compile (the variable "final_price" is not declared anywhere).

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    2
    so sorry, the "final_price" was changed to "new_price", i must have paste the older code, my bad... And thank you for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fib sequence
    By cmp in forum C Programming
    Replies: 12
    Last Post: 03-10-2014, 11:56 PM
  2. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-05-2010, 11:52 PM
  3. % sequence
    By goran00 in forum C Programming
    Replies: 9
    Last Post: 01-28-2008, 04:48 PM
  4. sequence
    By braddy in forum C Programming
    Replies: 2
    Last Post: 03-30-2006, 02:15 PM
  5. sequence
    By Space_Cowboy in forum C++ Programming
    Replies: 1
    Last Post: 12-08-2002, 06:54 PM