# Currency Conversion

• 12-16-2004
Currency Conversion
Hello all,
I'm working on this program (like you haven't seen this before, right) and I'm at the point where I cannot get it to error check when a character is used. I'm using Miracle C to compile and it compiles just fine and runs fine, I would just like a little guidance on where I'm wrong. This is all new to me and I'm learning as best I can.
Also, what would be a better way than to use goto?
Thanks.

Code:

``` #include <stdio.h> int main(void)        { /* Here is the conversion rate of one currency equivalent to one US dollar. Comparison price rates acquired on Monday, December 12, 2004 */ // Declare and define the conversion rate of one currency equivalent to one US dollar.     char cur[6][20]={"Canadian Dollar ", "Exit"}; //name of the lone currency     float rates[5] ={1.2263}; //equivalent exchange rate     float usd; /* US Dollars */     float amount; /* Amount to Convert*/     int r, i;     printf("    Al Emmi's Currency Conversion Program\n\n");     printf("Listed below is one lone country available for conversion\n");     printf("    into a US Dollar equivalent amount \n");     printf("\nHere is your only available choice for conversion: ");     printf("\n    (Sorry, budget cuts...d'oh!)\n");     for (i=0;i<2;i++) //a for loop to display the currencies to convert from         {     printf("\n\n\n%d, %s", i+1, cur[i]);         }           fflush(stdin); choice:    printf("\n\nDo me a favor and select that lone currency you want to convert to USD:\n");           printf("\nHint...it's number 1\n");                     scanf("%d", &i);           clrscr(); if ((i >2)||(i <=0)) //when the user chooses anything other than choices given     {     printf("Nice try chucklehead, that choice is not available! \n\n");     goto choice;     }else if (i == 2){  //if user just wants to exit because they don't like Canadian mulah         printf("\n\n\n\n      Oh, running away already! \n\n");         printf("\n\n\n      Well have a groovy day anyway!");         }else{    ask:    fflush(stdin);          printf("Very good!\n");          printf("\nSo, how much dough do you want to convert to USD?: \n\n ");         scanf("%f", &amount); //Patiently wait for the user to input something         clrscr(); if (amount <=(float)0)    //if user chooses a negative or zero             {               printf("\nYo numbnuts, This thing can't convert to US Dollars\n");               printf("  if the number is zero or negative!\n\n");               goto ask;               }else{           usd = amount /rates[i-1]; /*Convert to US Dollars*/     // Calculation: finding the equivalent amount of the US dollar from the 1 input currency.       printf("\nLike it or not this is what you get:  \$ %f US Greenbacks! \n\n", usd);       printf("  Now go buy yourself a beer aye!\n");       printf("      So make like a tree, and get outta here!\n");       printf("\n        Press and key to exit!\n");               }         }         getch();       return 0;/*End of program*/ }```
• 12-16-2004
master5001
There is a similar post to this that I just answered. Let me get you a link:

http://cboard.cprogramming.com/c-programming/59790-program-loops-forver-help.html

Also, your tabs are a bit out of whack.
• 12-16-2004
misplaced
change them to functions...i can't begin to explain functions here, but look them up.

next, indent consistently
Code:

```int main() {         if(...)         {                 //some code                 //some code                 //some code                 //some code         }else{                 //some code                 //some code                 //some code         }         while(...)         {                 if(...)                 {                         //some code                         //some code                         //some code                         //some code                         if(...)                         {                                 //some code                                 //some code                         }                 }         }         return 0; }```

after you do that i can help with the logic
• 12-16-2004
quzah
I started to help, but the combination of all of the gotos and fflush(stdin) usage caused my head to explode and I'm just now getting all of the pieces gathered again. I now know what Humpty Dumpty must feel like.

Quzah.
• 12-16-2004
master5001
Yeah I think the code can use some sprucing up a bit. I didn't run your code or anything but I assume you are having issues with scanf(). fflush(stdin) doesn't quite make my head explode since its undefined. I don't dislike gotos as much as most, but thats only because they are handy under some specific conditions. In your case they aren't exactly necessary though. 100% of the time you can do without them. I only use them for optimizing certain operations.
• 12-16-2004
Salem
> I'm using Miracle C
Miracle C is crap
http://cboard.cprogramming.com/showt...hlight=miracle
• 12-16-2004
chris1985
I get a distinct feeling I am being followed.... Anyway, Roadrunner as you are aware the Oanda website provides currency conversion services including live streaming quotes.
• 12-16-2004
quzah
Quote:

Originally Posted by chris1985
I get a distinct feeling I am being followed.... Anyway, Roadrunner as you are aware the Oanda website provides currency conversion services including live streaming quotes.

I'm not quite sure how this exactly benifits their programming exercise.

Quzah.
• 12-16-2004
master5001
>I'm not quite sure how this exactly benifits their programming exercise.
Yeah I agree. This has more of a homework sound to me rather than this guy writing bank software. Especially considering that this only converts one way across only two systems of money.

Since everyone has thus far only picked at your code I'll pick at it more and offer more suggestions.

Code:

```// careful when you do this. As it is your first string is damn near close to the cut off. // Your compiler will truncate a string that doesn't fit, but it also won't put a null terminator // so your code will start being a bit screwy and reading two strings, or hit a segment fault // if the last string is the trouble maker. char cur[6][20]={"Canadian Dollar ", "Exit"}; //name of the lone currency // Alternative would be: const char *cur[6]={"Canadian Dollar ", "Exit"}; //name of the lone currency // Notice I also made it constant since it indicates that the string // values shouldn't be altered. Its completely optional, but without it // your compiler won't complain if you accidentally try altering it. // Next big gripe of mine that I see is: printf("    Al Emmi's Currency Conversion Program\n\n"); printf("Listed below is one lone country available for conversion\n"); printf("    into a US Dollar equivalent amount \n"); printf("\nHere is your only available choice for conversion: "); printf("\n    (Sorry, budget cuts...d'oh!)\n"); // You can use multi-line strings to create the same "pretty" look without the clutter and // expense of multiple printf() calls. printf("    Al Emmi's Currency Conversion Program\n\n"   "Listed below is one lone country available for conversion\n"   "    into a US Dollar equivalent amount \n"   "\nHere is your only available choice for conversion: "   "\n    (Sorry, budget cuts...d'oh!)\n"); // Ok since this next part is mostly stuff that has been flamed by everyone else I won't // give you too much crap.           fflush(stdin); choice:    printf("\n\nDo me a favor and select that lone currency you want to convert to USD:\n");           printf("\nHint...it's number 1\n");                     scanf("%d", &i);           clrscr(); if ((i >2)||(i <=0)) //when the user chooses anything other than choices given     {     printf("Nice try chucklehead, that choice is not available! \n\n");     goto choice;     }else if (i == 2){  //if user just wants to exit because they don't like Canadian mulah         printf("\n\n\n\n      Oh, running away already! \n\n");         printf("\n\n\n      Well have a groovy day anyway!");         }else{    ask:    fflush(stdin);          printf("Very good!\n");          printf("\nSo, how much dough do you want to convert to USD?: \n\n ");         scanf("%f", &amount); //Patiently wait for the user to input something         clrscr(); // fflush() forces the IO cache to be written. In the case of input nothing is being writen. // Again multiline strings do wonders. And when you use scanf() with user input information, // you should always make sure the user wasn't being naughty. while(scanf("%f", &amount) != 1) {   printf("I'm sorry, your input must be in money format. Example: 1.50\n"); } // I'm also seeing conio.h functions here. Expect a thorough pistol whipping from // someone. // Ok the next part that bugs me is the way you are using gotos is making your code very // messy and hard to follow. I recommend looking at what misplaced suggested. I'd fix it for // you, but i'm sure you would gain no learning experience that way.```