Validating loop error

Posted on 2006-05-15
Last Modified: 2010-04-15
I had a question about a program I was completing for a class assignment. I can't get the loop to work correctly. I keep getting a parse error, but can't figure out how to correct it and still work the way I wanted it to. This is the code for the lines in question:


      //initiate values
      float purchaseAmt = 0.00; //amount tax will be taken from - input from user
      float totalAmt = 0.00; //total amount after taxes are added
      double DelMarTax = 0.0725; //Del Mar tax amount
      double EncitnitasTax = 0.075; //Encitnitas tax amount
      double LaJollaTax = 0.0775; //La Jolla tax amount
      int iChoice; //choice of store from user input
      char rChoice; //choice to repeat program
            // Loop validates purchase amount to make sure it's greater than 0, otherwise it repeats
            printf("\n\nPlease enter the total purchase amount: "); // Prompts the user for purchase amount
            scanf("%f", &purchaseAmt);}  // Gets input from user
            while (purchaseAmt <= 0.00 || purchaseAmt == char); // If purchase amount less than or equal  to 0, or equals a char, reprompt user to enter correct amount

I can take out the purchaseAmt == char completely and the program still works, but if a char value is entered the progam just goes haywire and has to be closed. I was wanting to validate the input so that a char value would cause the program to just repromt. Is this possible? I've tried purchaseAmt = char, purchaseAmt != int, using && instead of ||.
Question by:rhivka
    LVL 45

    Accepted Solution

    Hi rhivka,

    char is a reserved word in C.  You cannot use an expression like "purchaseAmt == char".  That's where your parse error lies.

    The scanf() will NOT store anything in purchaseAmt if the first character entered is a non-digit (or period).  If 12.4Z is entered, 12.4 is stored but you don't know about the Z.  Also, scanf() will leave the new-line character in the buffer so you haven't really read the entire line.  The next call to scanf() will "see" the new-line and think that an empty-line has been entered.

    Try this for your scanf() line and test:

       field_count = scanf("%f%c",&PurchaseAmt,&LastCharacter);
       if (field_count == 2 && &LastCharacter = '\n')  // success
       else     // bad line

    Of course, you'll have to define field_count and LastCharacter.

    Good Luck!
    LVL 5

    Assisted Solution

    char ?

    I think your compiler should flag an error, char is a keyword which is used to declare a variable, you cannot use it there in that statement.

    use this..

    declare a string called strval with

    char strval[20];

    then use

    instead of
    scanf("%f", &purchaseAmt);}  // Gets input from user

    now replace the while loop
    while (purchaseAmt <= 0.00 || purchaseAmt == char);



    atof() function converts the string to numeric value in floating point form. If it is unable to convert the string it returns 0.

    this way you can achieve your objective.


    Author Comment


       While your answer would work, I cannot use that in a Intro to C class :) He would wonder where in the world I got that... but thank you for explaining how to make it work.
    LVL 45

    Expert Comment

    Hi rhivka,

    Sorry.  Should have been:

       field_count = scanf("%f%c",&PurchaseAmt,&LastCharacter<wbr/>);
       if (field_count == 2 && LastCharacter = '\n')  // success
       else     // bad line

    Note the change on the if statement.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now