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

Thread Solved

Join Date: Jan 2005
Posts: 5
Reputation: wardduncan is an unknown quantity at this point 
Solved Threads: 0
wardduncan wardduncan is offline Offline
Newbie Poster

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

 
0
  #1
Jan 8th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

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

 
0
  #2
Jan 8th, 2005
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. }
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,619
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #3
Jan 8th, 2005
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 5
Reputation: wardduncan is an unknown quantity at this point 
Solved Threads: 0
wardduncan wardduncan is offline Offline
Newbie Poster

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

 
0
  #4
Jan 9th, 2005
Thanks for your help. This is exactly what I needed. You made it look so easy.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC