Thread: CurrencyConverter

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    7

    CurrencyConverter

    Hi guys,

    I am just learning c programming for the first time and having problems reading in a file into a 2d array.

    The file contains the following:
    1 euro
    0.99 dollars
    0.64 pounds
    121.42 yen
    4.65 shekels

    I am trying to create a currency converter. When the user chooses to convert a currency, the program reads the releveant cell in the 2d array and performs the conversion. The amount converted to the new currency should be eisplayed back and the user given the option to convert a different amount to another currency.

    The code I have already looks like this, I really need to know how to read the information in the file to a 2d array. Thanks in advance:
    Code:
    #include <stdio.h>
    	
    
    
    main()
    {
    	
    	 
    
       void menu_option(); /* prototyping functions */
    	char valid_c();
    	char execute_option(char c);
    	char c; 
    fp=fopen("rates.txt", "wt+");
    if (fp==NULL)
    {
    	printf("Error Opening File\n");
    	return 0;
    }
    	
    	menu_option();
    
    			do {
    				 
    				  c=valid_c();
    }
    					
    			while ( execute_option(c));
    			
    }
    
    void menu_option()
    {
        printf("\n***********************************");
    	printf("\n	Currency Converter");
    	printf("\n***********************************\n\n");
    	printf("	C: Convert Money\n");
    	printf("	U: Update Conversion Rates\n");
    	printf("	X: Exit\n\n\n");
    	printf("Please enter your choice or X to quit:\n");
    }
    
    
    char valid_c()
    
    {
    	char c;
    
    	do {
    			
    		c=getchar();
    	} while (c!='C' && c!='c' && c!='U' && c!='u' && c!='X' && c!='x');
    
    	return (c);
    
    }
    
    char execute_option(char c)
    
    {
    	switch(c) {
    					
    	case 'c': printf("\n You have chosen to Convert Money\n");
    			  break;
    	case 'C': printf("\n You have chosen to Convert Money\n");
    			  break;
    	case 'U': printf("\n You have chosen to Update Conversion Rates\n");
    			  break;
    	case 'u': printf("\n You have chose to Update Conversion Rates\n");
    			  break;
    	case 'X': return (0);
    	case 'x': return (0);
    	}
    	return (1);
    }

    &#91;code]&#91;/code]tagged by Salem

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Here some tips:
    [list=1][*] use int main(void) [*] use fgets to read a complete line from file[*] use sscanf to break down the line in smaller parts[*] function prototypes do not belong IN main, place them outside main[*] use code tags: type [CODE] then copy your code and type [/CODE] at the end of your code[*] open file for reading: fopen(filename, "r");[*]
    Code:
    case 'c': printf("\n You have chosen to Convert Money\n");
    break;
    case 'C': printf("\n You have chosen to Convert Money\n");
    break;
    You can replace this by:
    Code:
    case 'c': 
    case 'C': printf("\n You have chosen to Convert Money\n");
              break;
    Or just use the toupper function (don't forget to include ctype.h)[/list=1]
    Hope this helps.

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    You can use a static array:
    Code:
    #define MAX_CUR 10 /* max 10 currencies in file */
    
    int main(void)
    {
       float rate[MAX_CUR]; char currency[MAX_CUR][100];
       char buf[100]; int index = 0;
    
       /* do something: open file, initialize vars, etc */
    
       while(fgets(buf, 100, fp) != NULL)
       {
          if(sscanf(buf, "%f %s", &rate[index], currency[index]) != 2)
          {
             printf("Invalid line(#%d): %s", index+1, buf);
          }
          index++;
          if(index == MAX_CUR)
          {
             printf("not enough space in 2d array\n");
             break;
          }
       }
    
       /* do something print array, flose file, etc */
    
       return 0;
    }

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    7
    Thanks for your help monster, much appreciated......I will give it a go

Popular pages Recent additions subscribe to a feed