Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,984 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,406 people online right now. Registration is fast and FREE... Join Now!




Validation problems in while loop

 
Reply to this topicStart new topic

Validation problems in while loop, Help with school assignment

liquidz76
3 Dec, 2006 - 07:27 PM
Post #1

New D.I.C Head
*

Joined: 3 Dec, 2006
Posts: 1


My Contributions
Hi everyone...

I'm pretty satisfied with one of my first C++ programs; however, I'm having trouble with the error checking.

The program works fine with the while loop as long as you enter a valid numeral greater than 1. If anything else is entered that is not a numeral, the program will properly print a message denoting this and then ends. What I'm trying to do is make it so that "if" an invalid value is entered, the program prompts you to try it again. (The program does not have to end-- just keep looping with valid and invalid entries).

I've tried if/then statements without success. Below is the code that works before I got stuck. Any advice on how to create a proper loop that works with both valid and invalid entries?

Thanks so much!!!


------------------------------
CODE

//adding  stdio.h header file
#include <stdio.h>

//start of main function
main()
{
   //US value of one Brazilian Reais
   float fReais = .46;
   //Amount of Brazilian Reais entered
   float fAmount = 0.0;
   //Display of Program name
   printf("\n\n");
   printf("-----------------------------------------------------------\n\n");
   printf("- US Dollar and Brazilian Reais Conversion Currency Table -\n\n");
   printf("-----------------------------------------------------------\n\n");
   //Prompt user for an amount to convert
   printf("Please enter an amount of Brazilian Reais to convert into US Dollars: $");  
          
    //While loop makes sure user enters a proper amount for conversion
    while (scanf("%f", &fAmount) == 1)
    {
        //calculate amount of conversion
        printf("\nYour amount in US Dollars is $%.2f\n", fReais * fAmount);
        printf("\nThanks for using the currency converter!\n");    
        //Keep asking user to enter amount if a valid number is entered
        printf("\nPlease enter another amount: $");
      }

    //Diplays once user has entered an invalid amount
    printf("\nYou have entered an invalid amount!\n");
    printf("\nPlease try again...\n");
    
}


edit: added [code] tags ~ jayman9

This post has been edited by jayman9: 3 Dec, 2006 - 10:19 PM
User is offlineProfile CardPM
+Quote Post

UMTopSpinC7
RE: Validation Problems In While Loop
4 Dec, 2006 - 07:43 AM
Post #2

New D.I.C Head
*

Joined: 20 Oct, 2006
Posts: 47


My Contributions
You could just do this but it would put your code in an infinite loop. You'll have to find some other way to make it exit. Maybe make the user enter a special value or something.

QUOTE(liquidz76 @ 3 Dec, 2006 - 08:27 PM) *

CODE

//adding  stdio.h header file
#include <stdio.h>

//start of main function
main()
{
   //US value of one Brazilian Reais
   float fReais = .46;
   //Amount of Brazilian Reais entered
   float fAmount = 0.0;
   //Display of Program name
   printf("\n\n");
   printf("-----------------------------------------------------------\n\n");
   printf("- US Dollar and Brazilian Reais Conversion Currency Table -\n\n");
   printf("-----------------------------------------------------------\n\n");
   //Prompt user for an amount to convert
   printf("Please enter an amount of Brazilian Reais to convert into US Dollars: $");  
          
    //While loop makes sure user enters a proper amount for conversion
    while (1)
    {
                if(!scanf("%f", &fAmount) == 1) {
                           //Diplays once user has entered an invalid amount
                       printf("\nYou have entered an invalid amount!\n");
                       printf("\nPlease try again...\n");
                }
                else {
                   //calculate amount of conversion
                  printf("\nYour amount in US Dollars is $%.2f\n", fReais * fAmount);
                  printf("\nThanks for using the currency converter!\n");    
                  //Keep asking user to enter amount if a valid number is entered
                  printf("\nPlease enter another amount: $");
                 }
      }
      printf("This part of the program will never be reached.\n");
}


edit: added [code] tags ~ jayman9


User is offlineProfile CardPM
+Quote Post

horace
RE: Validation Problems In While Loop
4 Dec, 2006 - 08:25 AM
Post #3

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
if you get a conversion error when using scanf() the faulty character is left in the input stream and has to be removed. This simplest what is to read characters until newline is found, e.g.
CODE

//adding  stdio.h header file
#include <stdio.h>

//start of main function
main()
{
   //US value of one Brazilian Reais
   float fReais = .46;
   //Amount of Brazilian Reais entered
   float fAmount = 0.0;
   //Display of Program name
   printf("\n\n");
   printf("-----------------------------------------------------------\n\n");
   printf("- US Dollar and Brazilian Reais Conversion Currency Table -\n\n");
   printf("-----------------------------------------------------------\n\n");
   //Prompt user for an amount to convert
   printf("Please enter an amount of Brazilian Reais to convert into US Dollars: $");  
          
    //While loop makes sure user enters a proper amount for conversion
    while (1)
    {
                if(!scanf("%f", &fAmount) == 1) {
                           //Diplays once user has entered an invalid amount
                       printf("\nYou have entered an invalid amount!\n");
                       printf("\nPlease try again...\n");
                       // extract (faulty) characters up to end of line then read again
                       while (getchar() != '\n');  
                }
                else {
                   //calculate amount of conversion
                  printf("\nYour amount in US Dollars is $%.2f\n", fReais * fAmount);
                  printf("\nThanks for using the currency converter!\n");    
                  //Keep asking user to enter amount if a valid number is entered
                  printf("\nPlease enter another amount: $");
                 }
      }
      printf("This part of the program will never be reached.\n");
}

hitting ctrl/c will usually exit the program

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 07:27PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month