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