Idiot Trying To Write A Simple Program

This is a discussion on Idiot Trying To Write A Simple Program within the C Programming forums, part of the General Programming Boards category; Hey, I'm back, and I have another simple problem that I am having trouble resolving. The following program is going ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    28

    Idiot Trying To Write A Simple Program

    Hey, I'm back, and I have another simple problem that I am having trouble resolving.
    The following program is going to yield a messed up answer, I know, but I just want to get it to compile...

    When I run the compiler, the only error I get is: "59: error: parse error at end of input"
    (line 59 is the last line of the program, and it only contain's the '}')

    Here it is:

    Code:
    #include <stdio.h>
    
    #define METER_in_inches 39.37
    #define FOOT 12
    #define YARD 36
    
    int main( void )
    {
    	double METERS; /*INPUT: total number of meters*/
    	
    	int INCHES; /*OUTPUT: Total number of inches*/
    	int Tyards; /*OUTPUT: Total number of yards*/
    	int Ryards; /*OUTPUT: Remainder of inches after yards*/
    	int Tfeet; /*OUTPUT: Number of feet after yards*/
    	int Rfeet; /*OUTPUT: Remainder of inches after feet*/
    	
    	/*Enter number of meters*/
    	printf("Enter the exact number of meters> ");
    	scanf("%lf", &METERS);
    	
    		/*Calculate number of inches*/
    	INCHES = METERS * METER_in_inches;
    	
    	/*Calculate number of yards and feet with remainder in inches*/
    	if (INCHES >= 36) {
    		Tyards = INCHES / YARD;
    		Ryards = INCHES % YARD;
    	}
    	else {
    		if (INCHES >= 12) {
    			Tfeet = INCHES / FOOT;
    			Rfeet = INCHES % FOOT;
    		}
    		else
    			printf("We like to dance. Yes we do.");
    			
    	/*Display meters in yards, feet, and inches*/
    	printf("%0.2f meters is equivalent to %d yards, %d feet, and %d inches.", METERS, Tyards, Tfeet, Rfeet);
    	
    	return 0;
    }
    I'm sure it's something obvious- ...'if it was a snake it would have bit me' thing...

    But does anyone see what my error is?


    Thanks,
    Confused Student. Again.

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    Code:
    else {
            if (INCHES >= 12) {
                Tfeet = INCHES / FOOT;
                Rfeet = INCHES % FOOT;
            }
    this should be

    Code:
     else if(INCHES >= 12) {
                Tfeet = INCHES / FOOT;
                Rfeet = INCHES % FOOT;
            }
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Count your braces. The else in if (inches>=36) doesn't have a closing brace, so the compiler thinks everything to the end of the file is part of the else clause. That means there is no closing brace for main() which is why it's giving the error. Very easy thing to miss.

    What I normally do is type both the opening and closing braces when I am coding an if or while or whatever. Then I just insert the appropriate code between them. If you get in the habit of doing something like that, you will avoid these kinds of omissions.

    [EDIT] Looks like Ping just got it in before me. heh

    You could do either.
    Code:
    if (something)
    {
      do stuff
    }
    else if (something else)
    {
      do other stuff
    }
    is almost exactly the same as
    Code:
    if (something)
    {
      do stuff
    }
    else
    {
      if (something else)
      {
        do other stuff
      }
    }
    Unless you need to do more than just an if statement in the else clause, it's a matter of style.
    Last edited by jEssYcAt; 02-05-2005 at 02:27 AM.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    that wont work jessycat, it will result in a syntax error for the next else that the compiler encounters
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    The following is for general information purposes for the OP...

    Code:
        printf("Enter the exact number of meters> "); /* Your input prompt may not be displayed */
    The following might 'force' the text to display...
    Code:
        printf("Enter the exact number of meters> \n");
    ...but, you probably did not want the newline because it is an input prompt. If you are using printf() statements without using the '\n' character at the end, you may want to use the fflush() function like this:

    Code:
        printf("Enter the exact number of meters> ");
        fflush(stdout);

    You can read a little more about the subject here.

    ~/
    Last edited by kermit; 02-05-2005 at 06:22 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-30-2007, 09:08 AM
  2. Need help with simple, simple program.
    By LightsOut06 in forum C Programming
    Replies: 5
    Last Post: 09-01-2005, 08:31 PM
  3. Problem with simple XOR program
    By spike_ in forum C++ Programming
    Replies: 8
    Last Post: 08-17-2005, 12:09 AM
  4. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM
  5. I want to write a simple program in C++ to...
    By Danhash in forum Windows Programming
    Replies: 3
    Last Post: 03-07-2003, 12:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21