By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

Currency Program

P: 2
I have the following program and I need to add error handling to prevent negative numbers and letters being entered when this program is executed. Any help is appreciated.





Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main (void)
  4. {
  5.         // ************************************
  6.         // Code removed here
  7.         // ************************************
  8.  
  9.     // menu for user to select from
  10.      printf("\n 1. Australian Dollar\n", );        
  11.          printf("\n 2. Euro \n", );                       
  12.          printf("\n 3. Barbados Dollar\n", );      
  13.          printf("\n 4. Hong Kong Dollar\n", );         
  14.          printf("\n 5. Dominican Republic Pesos\n", );  
  15.  
  16.  
  17.     //prompt the user for what currency to convert
  18.     printf("\n\n Select the currency you want to convert? 1-5: ");
  19.     scanf("%d",&selection);
  20.  
  21.     printf("\n\n Please select how much currency you want to convert to US Dollars? ");
  22.     scanf("%f",&currency);
  23.  
  24.         // ************************************
  25.         // Code removed here
  26.         // ************************************
  27.  
  28.     getchar();
  29.  
  30.  
  31.     return 0;
  32. }
Mar 15 '07 #1
Share this Question
Share on Google+
4 Replies


Ganon11
Expert 2.5K+
P: 3,652
Use a while loop. Inside the loop, ask the user for input again, indicating that it must be a positive number. For the loop's control, keep the loop executing while the number entered is negative.
Mar 15 '07 #2

P: 2
Do you have a example of how to do this?? I'm new to c programming and I don't know exactly how to do this??
Mar 15 '07 #3

Ganon11
Expert 2.5K+
P: 3,652
Have you used a while loop before? The basic syntax is:

Expand|Select|Wrap|Line Numbers
  1. while (<boolean condition>) {
  2.    // statements...
  3.    // The boolean condition should be affected here.
  4. }
This boolean condition can be many things - it can depend on a bool variable, it can depend on a function's value, or in your case, it can depend on a number's property. A number is negative if it is less than 0. The statement

Expand|Select|Wrap|Line Numbers
  1. num < 0
is a boolean expression. It will evalute to "true" if num is less than 0, and "false" otherwise. The while() loop will execute as long as its condition remains true. Thus, you can use a while loop that will continue getting input until the number is positive.

Is that a better explanation?
Mar 15 '07 #4

P: 1
I have the following program and I need to add error handling to prevent negative numbers and letters being entered when this program is executed. Any help is appreciated.





Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main (void)
  4. {
  5.         // ************************************
  6.         // Code removed here
  7.         // ************************************
  8.  
  9.     // menu for user to select from
  10.      printf("\n 1. Australian Dollar\n", );        
  11.          printf("\n 2. Euro \n", );                       
  12.          printf("\n 3. Barbados Dollar\n", );      
  13.          printf("\n 4. Hong Kong Dollar\n", );         
  14.          printf("\n 5. Dominican Republic Pesos\n", );  
  15.  
  16.  
  17.     //prompt the user for what currency to convert
  18.     printf("\n\n Select the currency you want to convert? 1-5: ");
  19.     scanf("%d",&selection);
  20.  
  21.     printf("\n\n Please select how much currency you want to convert to US Dollars? ");
  22.     scanf("%f",&currency);
  23.  
  24.         // ************************************
  25.         // Code removed here
  26.         // ************************************
  27.  
  28.     getchar();
  29.  
  30.  
  31.     return 0;
  32. }
How do you enter decimals in this program? I am trying to convert currency to US dollar (ex:australian to US is 0.810399)
Apr 7 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.