944,161 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 4804
  • C RSS
Jan 8th, 2005
0

Need some help with putting a looping into a converter program.

Expand Post »
Hey guys, I need some help with looping. I am writing a currency converter (who in begining programming isn't these days) and I want to have it loop so that I can it keep asking for another entry or make it quit. Here is what I have so far:

  1. include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /* Main Program */
  5. int main(void)
  6. {
  7.  
  8. float USD;
  9. float CAD;
  10. float EUR;
  11. float GBP;
  12. float CHF;
  13. float YEN;
  14. int input;
  15. char q;
  16.  
  17. /*Title of program*/
  18. printf("\t This program will convert foreign currency to US Dollars\n\n");
  19.  
  20. /*Assigning Values*/
  21. USD = 1.0; /* US Dollar*/
  22. CAD = 1.2257; /* Canadian Dollar*/
  23. EUR = 0.752162; /* European Euro */
  24. GBP = .51573; /* British Pound */
  25. CHF = 1.134; /* Swiss Franc*/
  26. YEN = 1.038; /* Japanese Yen*/
  27.  
  28. printf("Please choose form the following list of currency or press q to quit.\n");
  29. printf("\n");
  30. printf("[1] Canadian Dollar\n");
  31. printf("[2] British Pound\n");
  32. printf("[3] European Euro\n");
  33. printf("[4] Swiss Franc\n");
  34. printf("[5] Japanese Yen\n");
  35. printf("\n");
  36. scanf("%d", &input);
  37.  
  38.  
  39. /*switching statements */
  40. switch (input)
  41. {
  42. case 1 :
  43. printf("%1.6f Canadian Dollars is equal to %1.1f US Dollar.\n", CAD,USD);
  44. break;
  45. case 2 :
  46. printf("%1.6f European Euro is equal to %1.1f US Dollar.\n", EUR,USD);
  47. break;
  48. case 3 :
  49. printf("%1.6f British Pound is equal to %1.1f US Dollar.\n", GBP,USD);
  50. break;
  51. case 4 :
  52. printf("%1.6f Swiss Franc is equal to %1.1f US Dollar.\n", CHF,USD);
  53. break;
  54. case 5 :
  55. printf("%1.6f Japanese Yen is equal to %1.1f US Dollar.\n", YEN,USD);
  56. break;
  57. }
  58.  
  59. scanf("%d");
  60. return 0;
  61. }
Last edited by alc6379; Jan 10th, 2005 at 9:50 pm. Reason: added [code] tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wardduncan is offline Offline
5 posts
since Jan 2005
Jan 8th, 2005
0

Re: Need some help with putting a looping into a converter program.

use a while loop checking if q is pressed. You havent actually used char, and are asking the user to input a letter or number, but only grabbing the number! I would convert to c++ and use iostream, I dont know the C equivalent

  1. char input; // user will type, so input is a char!
  2.  
  3. while (input != 'q' || input != 'Q') // loops untill you put q or Q in input
  4. {
  5. code here...
  6. I would use cin >> input;
  7. then a strtoi (i think that is how it is spelled) call to convert the char to a number
  8. }
Reputation Points: 16
Solved Threads: 6
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004
Jan 8th, 2005
0

Re: Need some help with putting a looping into a converter program.

Unless you want to do precise error handling and throw a message if the user enters something other than q or Q, you can simply place the entire processing code into an infinite loop and break if scanf fails:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /* Main Program */
  5. int main(void)
  6. {
  7. float USD;
  8. float CAD;
  9. float EUR;
  10. float GBP;
  11. float CHF;
  12. float YEN;
  13. int input;
  14.  
  15. /*Title of program*/
  16. printf("\t This program will convert foreign currency to US Dollars\n\n");
  17.  
  18. /*Assigning Values*/
  19. USD = 1.0f; /* US Dollar*/
  20. CAD = 1.2257f; /* Canadian Dollar*/
  21. EUR = 0.752162f; /* European Euro */
  22. GBP = .51573f; /* British Pound */
  23. CHF = 1.134f; /* Swiss Franc*/
  24. YEN = 1.038f; /* Japanese Yen*/
  25.  
  26. for (;;) {
  27. printf("Please choose form the following list of currency or press q to quit.\n");
  28. printf("\n");
  29. printf("[1] Canadian Dollar\n");
  30. printf("[2] British Pound\n");
  31. printf("[3] European Euro\n");
  32. printf("[4] Swiss Franc\n");
  33. printf("[5] Japanese Yen\n");
  34. printf("\n");
  35. if (scanf("%d", &input) != 1)
  36. break;
  37.  
  38. /*switching statements */
  39. switch (input)
  40. {
  41. case 1 :
  42. printf("%1.6f Canadian Dollars is equal to %1.1f US Dollar.\n", CAD,USD);
  43. break;
  44. case 2 :
  45. printf("%1.6f European Euro is equal to %1.1f US Dollar.\n", EUR,USD);
  46. break;
  47. case 3 :
  48. printf("%1.6f British Pound is equal to %1.1f US Dollar.\n", GBP,USD);
  49. break;
  50. case 4 :
  51. printf("%1.6f Swiss Franc is equal to %1.1f US Dollar.\n", CHF,USD);
  52. break;
  53. case 5 :
  54. printf("%1.6f Japanese Yen is equal to %1.1f US Dollar.\n", YEN,USD);
  55. break;
  56. }
  57. }
  58.  
  59. return 0;
  60. }
That's the easiest solution. If you want something more structured and good error messages, it takes more effort, usually in the form of string input and validation followed by conversions.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 9th, 2005
0

Re: Need some help with putting a looping into a converter program.

Thanks for your help. This is exactly what I needed. You made it look so easy.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wardduncan is offline Offline
5 posts
since Jan 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: inserting an element into an array in c language
Next Thread in C Forum Timeline: bubble sorting in an array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC