Thread: Help with my code

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    10

    Help with my code

    Hi, I was wondering if anyone could offer an insight into why my piece of code isn't working.

    Code:
    #include <stdio.h>
    //This includes the standard input output library.
    
    main ()
    {
    
    float price, VAT, price_plus_VAT, VATD;
    char a, new_VAT;
    //This defines the variables 'price' and 'VAT' as floating integer. I chose floating integers because the 'price' and 'VAT' will most likely
    //require a decimal place.
    
    start:
    printf("\nWould you like to enter a new value for the VAT? (Y/N):\n");
    new_VAT=getchar();
    if(new_VAT=='Y')
    {
    		printf("\nPlease enter the new value for VAT:");
          scanf("%f",&VAT);
          VATD=VAT/100;
    }
    else
    {
          VAT=17.5;
          VATD=VAT/100;
    }
    printf("\nPlease enter a price to calculate the VAT off:");
    //This prints text prompting the office worker to enter a price.
    scanf("%f",&price);
    //This scans for the value entered by the office worker and stores it in the variable 'price'.
    
    price_plus_VAT=price+price*VATD;
    
    printf("The price you entered %.2f\n",price);
    printf("The current VAT ammount is %.2f%\n",VAT);
    printf("The price including VAT is %.2f\n",price_plus_VAT);
    //This prints text telling the office worker what price he entered.
    //By default the program displays 5 decimal places which is more than necessary for this program.
    //The .2 means only two decimal places will be shown.
    
    printf("Would you like to repeat this program? Y/N");
    a=getchar();
    if(a=='Y')
    goto start;
    
    
    }
    The first IF function works fine, but it just displays the three printf functions displaying information an the "would you like to repeat this program? Y/N" printf (like it should) then quits.

    I can't see why it completly ignores the second IF function and the a=getchar();

    I've spent hours trying to see if i've missed something.

    Can anyone help? It would be greatly appreciated.

    Cheers

    Whytock

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The getchar() is ignored because you still have a newline in the keyboard buffer, from the scanf(). The getchar() "see's" the newline and say's "OK, I'm done", and goes right on.

    Add one getchar() right after the user's first input, and then have your second getchar(), get the new char.

    If you only want to scanf() numbers, then it's OK. scanf() will skip over newlines. But for char's, scanf() can't skip over a newline, since it is a char.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    10
    Works perfectly!

    Adak you are a legend!

    Thank you!

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    10
    I say it works perfectly, it goes back to start: but only prints "Would you like to enter a new value for VAT? (Y/N)", it dosen't actually let me enter anything for it, it just goes to "enter a price to calculate the VAT off"



    Ahhhhh

    Same problem again, sorted it
    Last edited by Whytock; 02-28-2010 at 11:00 AM.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Dont use scanf. Its just bad.

    Write your own function to process input. You'll learn alot. And your program will be by far better.

    For example, what happens if i enter a letter? How will your program behave?

    And also, CHECK HOW scanf WORKS before using it.

    As Adak says, scanf returns the isspace() characters back to input when used to get a number.

    Have a look at fgets(), gets() and atoi() or atof().
    Last edited by Tool; 02-28-2010 at 05:06 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Say I had two scanf()'s for float or integer input from the user, followed by one scanf() for a single char (again).

    After the first scanf(), I'd have one newline remaining in the keyboard buffer
    After the second scanf(), I'd have still one newline in the keyboard buffer, because the earlier one had been passed over by scanf()'s second call for a number.

    Now, I want to get a char with scanf(), to do that, I have to first pull off one newline, with getchar(), first, then I can use scanf() to get that char, with no problem.

    Tool has it right, fgets() and sscanf() make a much better combo, but if you have just a little snippet of a program you want to do,and the user is only going to be you 95% of the time, for a small few bits of input, then you should be able to use scanf() - but you have to understand what it's doing to the newlines.

    Numbers - leave a newline, but that's no problem for subsequent numbers input with scanf().

    Strings - usually don't leave a newline, but they CAN if the input format is shorter than the data, including the newline, to fit.

    Char's - always need the buffer cleared, beforehand, to avoid the newline being accepted as the input char from the user. Always leaves a newline behind, as well.

    Why not use a do while((again != 'N') && (again != 'n')); loop, instead of this goto start?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM