?
Solved

Validating loop error

Posted on 2006-05-15
4
Medium Priority
?
232 Views
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
      
      do
      {
            // Loop validates purchase amount to make sure it's greater than 0, otherwise it repeats
            do
            {
            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 ||.
0
Comment
Question by:rhivka
  • 2
4 Comments
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 1600 total points
ID: 16684823
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!
Kent
0
 
LVL 5

Assisted Solution

by:cryptosid
cryptosid earned 400 total points
ID: 16684881
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
scanf("%s",&strval);

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


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

with

while(atof(strval)>0);

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.

Regards,
Siddhesh
0
 

Author Comment

by:rhivka
ID: 16684931
Siddhesh,

   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.
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 16685655
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.

Kent
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses
Course of the Month15 days, 13 hours left to enroll

850 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