Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    2
    Thanks
    1
    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.

    Code:
    #include<stdio.h>
    
    
    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
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    missing initialization of i
    Code:
    	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
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,889
    Thanks
    160
    Thanked 2,228 Times in 2,215 Posts
    Code:
    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.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    ejtucker1 (03-06-2011)

  • #4
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    1
    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
    •