Thread: Currency Conversion

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    1

    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*/ 
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    There is a similar post to this that I just answered. Let me get you a link:

    http://cboard.cprogramming.com/showthread.php?t=59790

    Also, your tabs are a bit out of whack.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    lets start with the goto's
    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
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I'm using Miracle C
    Miracle C is crap
    http://cboard.cprogramming.com/showt...hlight=miracle

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    43
    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.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    >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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple currency conversion program gone south!!
    By lildroopie in forum C Programming
    Replies: 1
    Last Post: 02-01-2009, 12:45 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  4. Help with variables (newbee)
    By stugatza in forum C Programming
    Replies: 7
    Last Post: 08-18-2004, 10:40 AM
  5. Currency Conversion Program
    By kgraw21 in forum C Programming
    Replies: 5
    Last Post: 04-19-2002, 08:39 AM