Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2011
    Thanked 0 Times in 0 Posts

    C program - Parse Errors

    Hi guys, Im very new to programming, and I am trying to write my first program using a for loop. I thought I had it correct however, I keep getting parse errors involving the for statement. I have rewrote it a couple times thinking I may have left something else, but I am still not able to get rid of the errors. Could someone give me a clue about what I have wrong.

    The purpose of the program is to convert currency. The errors I keep recieving are: parse error before 'int' ; parse error before ')' ; parse error before string constant ; warning: data definition has no type or storage class.

    int main(void)
    /*Beginning of main class. */
    	/* Declare varibles for the conversion rates of the foreign currency.  */
    	int i;
    	float us_dollar = 0;
    	float danish_krones = 0;
    	float euros = 0;
    	float hong_kong_dollar = 0;
    	float us_dollar_total = 0;
    	float converted_krones = us_dollar * danish_krones; 
    	float converted_euros = us_dollar * euros;
    	float converted_hong_kong_dollar = us_dollar * hong_kong_dollar;
    	printf ("Welcome to the Money Exchange\n\n");
    	printf ("The program will convert three US dollar amounts to"); 
    	printf ("Danish Krones, Euros, and Hong Kong Dollars.\n\n");
    	printf ("First, please enter the exchange rates:");
    	printf ("Danish Krones: ");
    	scanf  ("%f", &danish_krones);
    	printf ("Euros: ");
    	scanf  ("%f", &euros);
    	printf ("Hong Kong Dollars: ");
    	scanf  ("%f", &hong_kong_dollar);
    	for(int i; i<=3; i++){
    		printf("\n\nEnter US dollar #%u to be converted = ", i);
    		scanf ("%f", &us_dollar);
    		printf("\n\n%f US Dollars is equal to: \n", us_dollar);
    		printf("\t %f Danish Krones", converted_krones);
    		printf("\t %f Euros", converted_euros);
    		printf("\t %f Hong Kong Dollars", converted_hong_kong_dollar);
    		us_dollar_total = us_dollar_total + us_dollar;
    	printf("\n\n You have converted a total of $%f", us_dollar_total);	
    	/* return null to denote the end of program. */
    	return 0;
    }   /* End of main class. */
    Any help would be much appreciated!
    Last edited by ejtucker1; 03-06-2011 at 06:20 PM.

  2. #2
    Master Coder
    Join Date
    Dec 2007
    Thanked 890 Times in 879 Posts
    missing initialization of i
    	for(int i=0; i<=3; i++){
    check if you don't need to include a header for float.

    Edit: and don't change n to i since i edit,

    best regards

  3. #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Thanked 2,250 Times in 2,237 Posts
    for(int i; i<=3; i++)
    You can't declare variables inside a for loop in C (though it's valid in C++)

    PS: You need to initialise that variable before using in the loop, to avoid unexpected results.

  4. Users who have thanked abduraooft for this post:

    ejtucker1 (03-06-2011)

  5. #4
    New to the CF scene
    Join Date
    Mar 2011
    Thanked 0 Times in 0 Posts
    Thanks so much guys! I got it working within seconds of removing the declaration from the loop.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts