New to C and needing some guidance

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2006
Posts: 4
Reputation: Sealteam56 is an unknown quantity at this point 
Solved Threads: 0
Sealteam56 Sealteam56 is offline Offline
Newbie Poster

New to C and needing some guidance

 
0
  #1
Jul 6th, 2006
Hello all,

I had posted earlier for help, but the post was so deep in an older thread do not think anyone will find it.

so here goes,

I am in a class to learn C and was tasked with writing a program that will convert currencies. Yes, the same old program is still being asked of students. Now I have written the program several times to complete previous requirements with no problems to date. But this last task has me stumped; I would like to get some guidance as to possible solutions to my code as I think it is correct but with a minor error that I seem to be missing.

Any help would be great, again not asking to have someone rewrite but perhaps guide me to the area of the area and possible ways to resolve the issue.

Now to the heart of the problem the program will allow a user to select the type of currency than it should allow the user to input how much to convert, I have added error checking here so as only valid inputs are excepted, if not than an error message will be displayed. Ok, right will on the second input from the user the program will assume any input to be an error, this is where I am stumped as I the code stipulates only if an input outside the range to than display an error.

With my little knowledge this is where I get confused.

Thanks for any help here.


  1.  
  2. /*The conversion rates of five currencies equivalent to one US dollar*/
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. int main(void)
  7. {
  8. int choice;
  9. int currency;
  10. float USD, UK, HK, EUR, NZ, AUS;
  11. char buffer[30];
  12.  
  13. /********************************************
  14.   ******Current Currency Exchange Rates*******
  15.   *
  16.   * British Pound(UK) 0.5408 6/15 8:50pm
  17.   * Hong Kong Dollar (HK) 7.7617 6/15 8:50pm
  18.   * Euro (EUR) 0.7914 6/15 8:50pm
  19.   * New Zealand (NZ) Dollar 1.6079 6/15 8:50pm
  20.   * Australian Dollar (AUS) 1.3556 6/15 8:50pm
  21.   * United States Dollar (US)
  22.   *
  23.   **********************************************/
  24.  
  25.  
  26. USD = 1.00;
  27. UK = 0.5408;
  28. HK = 7.7617;
  29. EUR = 0.7914;
  30. NZ = 1.6079;
  31. AUS = 1.3556;
  32.  
  33. do // main loop runs until choice 0 is entered
  34. {
  35. /*Version and title of program and menu selection*/
  36.  
  37. printf("\n\n Welcome to Currency Conversion Utility v. 4.2.0 \n\n\n");
  38.  
  39. printf("Please select the currency type you would like to convert into US dollars.\n\n");
  40.  
  41. printf("1. for British Pounds \n");
  42. printf("2. for Hong Kong Dollars \n");
  43. printf("3. for Euros \n");
  44. printf("4. for New Zealand Dollars \n");
  45. printf("5. for Australian Dollars \n\n");
  46.  
  47. printf("Press 0 and then return to exit the Currency Converter \n");
  48.  
  49. choice = 6; /* must be initialized to an invalid value*/
  50. /* it's not initialized the first time, it might be in the range of 1..5*/
  51. do /* 1st input loop for the choice*/
  52. {
  53. printf("\n\nEnter the number of your choice: Than hit Enter \n\n\n");
  54. /*reads the characters from the file associated with fp into the string pointed to.*/
  55. fgets(buffer, sizeof(buffer), stdin);
  56. /* allows user selcetion of type of Currency to convert and Error Checking for Numeric Values only*/
  57. if ( ( sscanf(buffer, "%u", &choice) == 0) || choice > 5 || choice < 0 )
  58.  
  59. {
  60. printf("\n\n\a Error please select agian \n");
  61. }
  62. }
  63. while(choice < 0 || choice > 5);
  64.  
  65. if ( choice > 0 )
  66. { /* no need to ask for the amount if exit was selected*/
  67.  
  68. currency = 100000; /* must be initialized to an invalid value*/
  69.  
  70. do /* 2nd input loop for the amount*/
  71. {
  72.  
  73. printf("\n\n\nEnter the amount you want to convert to U.S. Dollars: Than hit Enter \n\n");
  74.  
  75. fgets(buffer, sizeof(buffer), stdin);
  76.  
  77. /* Allows input of currency amount and Error Checking for Numeric Values only*/
  78. if ( ( sscanf(buffer, "%d", &currency) == 0) || currency > 99999 || currency < 1 )
  79. {
  80. printf("\n\n\a This is an Erroneous amount based on your input\n");
  81. printf(" please enter a numeric value");
  82. }
  83. }
  84.  
  85. while(currency > 99999 || currency < 0); /* changed the condition to int values*/
  86. }
  87.  
  88. switch(choice)
  89. {
  90.  
  91. case 1:
  92. printf("\n\n You will have %.2f US dollars \n\n", USD/UK*currency);
  93. break;
  94. case 2:
  95. printf("\n\n You will have %.2f US dollars \n\n", USD/HK*currency);
  96. break;
  97. case 3:
  98. printf("\n\n You will have %.2f US dollars\n\n", USD/EUR*currency);
  99. break;
  100. case 4:
  101. printf("\n\n You will have %.2f US dollars \n\n", USD/NZ*currency);
  102. break;
  103. case 5:
  104. printf("\n\n You will have %.2f US dollars \n\n", USD/AUS*currency);
  105. break;
  106. case 0:
  107. break;
  108. return 0;
  109. }
  110.  
  111. }
  112.  
  113. while(choice!=0 && currency != 0); /* saves the second switch.*/
  114. /* the switch would not work if I had changed currency to float*/
  115. puts("You have entered 0 and selected to exit the program.");
  116. puts("Thank you for using Currency Converter.");
  117. puts("bye.");
  118. puts("press enter.");
  119. getchar();
  120.  
  121. return 0;
  122. }

Randy
"A Noob wishing to gain more knowledge"
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,755
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is online now Online
Posting Virtuoso

Re: New to C and needing some guidance

 
0
  #2
Jul 6th, 2006
  1. if ( ( sscanf(buffer, "%d", &currency) == 0) ||
  2. currency > 99999 ||
  3. currency < 1 )
  4. {
  5. /////////////////////////////////////////////////////
  6. printf("\n\n\a This is an Erroneous amount based on your input\n");

First I'd try placing an output statement to indicate the value of currency and buffer at the place where the /////////////////////////// is. If the values of currency and buffer aren't what you expect, then track down why not.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 4
Reputation: Sealteam56 is an unknown quantity at this point 
Solved Threads: 0
Sealteam56 Sealteam56 is offline Offline
Newbie Poster

Re: New to C and needing some guidance

 
0
  #3
Jul 6th, 2006
Originally Posted by Lerner
  1. if ( ( sscanf(buffer, "%d", &currency) == 0) ||
  2. currency > 99999 ||
  3. currency < 1 )
  4. {
  5. /////////////////////////////////////////////////////
  6. printf("\n\n\a This is an Erroneous amount based on your input\n");

First I'd try placing an output statement to indicate the value of currency and buffer at the place where the /////////////////////////// is. If the values of currency and buffer aren't what you expect, then track down why not.
I actually removed the second Do statements which now allows the program to process the conditions for currency and place inthe buffer. But still have issues in that the error msg, as indicitated continues to print and there seems to be no error checking at all as I can now enter any value with a output.

here is what the code now looks like

  1.  
  2. /*The conversion rates of five currencies equivalent to one US dollar*/
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. int main(void)
  7. {
  8. int choice;
  9. int currency;
  10. float USD, UK, HK, EUR, NZ, AUS;
  11. char buffer1[30];
  12. char buffer2[90];
  13. /********************************************
  14.   ******Current Currency Exchange Rates*******
  15.   *
  16.   * British Pound(UK) 0.5408 6/15 8:50pm
  17.   * Hong Kong Dollar (HK) 7.7617 6/15 8:50pm
  18.   * Euro (EUR) 0.7914 6/15 8:50pm
  19.   * New Zealand (NZ) Dollar 1.6079 6/15 8:50pm
  20.   * Australian Dollar (AUS) 1.3556 6/15 8:50pm
  21.   * United States Dollar (US)
  22.   *
  23.   **********************************************/
  24.  
  25.  
  26. USD = 1.00;
  27. UK = 0.5408;
  28. HK = 7.7617;
  29. EUR = 0.7914;
  30. NZ = 1.6079;
  31. AUS = 1.3556;
  32.  
  33. do // main loop runs until choice 0 is entered
  34. {
  35. /*Version and title of program and menu selection*/
  36.  
  37. printf("\n\n Welcome to Currency Conversion Utility v. 4.2.0 \n\n\n");
  38.  
  39. printf("Please select the currency type you would like to convert into US dollars.\n\n");
  40.  
  41. printf("1. for British Pounds \n");
  42. printf("2. for Hong Kong Dollars \n");
  43. printf("3. for Euros \n");
  44. printf("4. for New Zealand Dollars \n");
  45. printf("5. for Australian Dollars \n\n");
  46.  
  47. printf("Press 0 and then return to exit the Currency Converter \n");
  48.  
  49. choice = 6; /* must be initialized to an invalid value*/
  50. /* it's not initialized the first time, it might be in the range of 1..5*/
  51. do /* 1st input loop for the choice*/
  52. {
  53. printf("\n\nEnter the number of your choice: Than hit Enter \n\n\n");
  54. /*reads the characters from the file associated with fp into the string pointed to.*/
  55. fgets(buffer1, sizeof(buffer1), stdin);
  56. /* allows user selection of type of Currency to convert and Error Checking for Numeric Values only*/
  57. if ( ( sscanf(buffer1, "%u", &choice) == 0) || choice > 5 || choice < 0 )
  58.  
  59. {
  60. printf("\n\n\a Error please select again \n");
  61. }
  62. }
  63. while(choice < 0 || choice > 5);
  64.  
  65. if ( choice > 0 )
  66. { /* no need to ask for the amount if exit was selected*/
  67.  
  68. currency = 100000; /* must be initialized to an invalid value*/
  69.  
  70. // do /* 2nd input loop for the amount*/
  71. {
  72.  
  73. printf("\n\n\nEnter the amount you want to convert to U.S. Dollars: Than hit Enter \n\n");
  74.  
  75. fgets(buffer2, sizeof(buffer2), stdin);
  76.  
  77. /* Allows input of currency amount and Error Checking for Numeric Values only*/
  78.  
  79. // if ( (sscanf(buffer, "%u", &currency) == 0) || currency > 99999 || currency < 1 )
  80.  
  81. scanf(buffer2, "%u", &currency);
  82.  
  83. if(currency > 99999 || currency < 0 )
  84.  
  85. {
  86. printf("\n\n\a This is an Erroneous amount based on your input\n");
  87. printf(" please enter a numeric value");
  88. }
  89. else if(currency < 99999 || currency > 0)
  90.  
  91. {
  92. printf("lets hope this works");
  93. }
  94. }
  95.  
  96. // while(currency > 99999 || currency < 0); /* changed the condition to int values*/
  97. }
  98.  
  99. switch(choice)
  100. {
  101.  
  102. case 1:
  103. printf("\n\n You will have %.2f US dollars \n\n", USD/UK*currency);
  104. break;
  105. case 2:
  106. printf("\n\n You will have %.2f US dollars \n\n", USD/HK*currency);
  107. break;
  108. case 3:
  109. printf("\n\n You will have %.2f US dollars\n\n", USD/EUR*currency);
  110. break;
  111. case 4:
  112. printf("\n\n You will have %.2f US dollars \n\n", USD/NZ*currency);
  113. break;
  114. case 5:
  115. printf("\n\n You will have %.2f US dollars \n\n", USD/AUS*currency);
  116. break;
  117. case 0:
  118. break;
  119. return 0;
  120. }
  121.  
  122. }
  123.  
  124. while(choice!=0 && currency != 0); /* saves the second switch.*/
  125. /* the switch would not work if I had changed currency to float*/
  126. puts("You have entered 0 and selected to exit the program.");
  127. puts("Thank you for using Currency Converter.");
  128. puts("bye.");
  129. puts("press enter.");
  130. getchar();
  131.  
  132. return 0;
  133. }

again, all seems somewhat correct except I can not understand why no matter what the code thinks that all values for currecny are false, but again after removing the do/while the program does continue to function, well kinda.

Randy
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: New to C and needing some guidance

 
0
  #4
Jul 6th, 2006
I compiled and ran your code and it worked as expected for me no problem. What exaclty are you inputing and what exactly is the response you're getting?

Oops you posted just before me. The code runs *with* the second do loop in tact just fine for me, I literaly just pasted your first posted code straight into a main.c and compiled it, and it ran as expected first time, if I put in dodgy input I get the error message, if the inputs ok I get the conversion.
Last edited by hollystyles; Jul 6th, 2006 at 4:53 pm.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 4
Reputation: Sealteam56 is an unknown quantity at this point 
Solved Threads: 0
Sealteam56 Sealteam56 is offline Offline
Newbie Poster

Re: New to C and needing some guidance

 
0
  #5
Jul 10th, 2006
Originally Posted by hollystyles
I compiled and ran your code and it worked as expected for me no problem. What exaclty are you inputing and what exactly is the response you're getting?

Oops you posted just before me. The code runs *with* the second do loop in tact just fine for me, I literaly just pasted your first posted code straight into a main.c and compiled it, and it ran as expected first time, if I put in dodgy input I get the error message, if the inputs ok I get the conversion.
Thank you for your feedback..

Randy
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC