469,898 Members | 1,768 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

Currency Program

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
4 1817
Ganon11
3,652 Expert 2GB
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
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
3,652 Expert 2GB
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
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.

Similar topics

2 posts views Thread by Willing 2 Learn | last post: by
12 posts views Thread by Ramesh | last post: by
6 posts views Thread by jcnews | last post: by
7 posts views Thread by meenasamy | last post: by
5 posts views Thread by Steven Smith | last post: by
25 posts views Thread by mereba | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.