Can't find error in code

This is a discussion on Can't find error in code within the C Programming forums, part of the General Programming Boards category; This section of my code keeps coming up with a local function error, but i can't find any braces that ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    8

    Can't find error in code

    This section of my code keeps coming up with a local function error, but i can't find any braces that aren't closed. Any help is greatly appreciated!

    Code:
    while (1)
    
    {
    
    printf("What would you like to do next (1 - turn car on; 2 - turn car off; 3 - change position of car)?");
          scanf ("%d", &selectedOption);
    
    	  switch (selectedOption)
    
    	  {	  
    
    	  case 1: 
    
    		 int IgnitionOn (int stateOfIgnition);
    
    	  break;
    
    	  case 2:
    
    		  int IgnitionOff(int stateOfIgnition);
    		  void StateOfCar(int stateOfIgnition, int currentHorizontalPosition, int currentVerticalPosition, char color);
    	  break;
    	  
    	  
    	  case 3:
    
    		  printf("Would you like to move the car 1 - horizontally or 2 - vertically?\n");
    			  scanf("%d", &direction);
    
    		  switch (direction)
    		  {
    		  
    		  
    		  case 4:
    		         printf("How many horizontal spaces would you like to move the car? (- to move left, positive to move right");
    		         scanf("%d", &horizontalSpaces);
    
    				 int MoveHorizontally (int horizontalSpaces, int currentHorizontalPosition);
    				 void StateOfCar(int stateOfIgnition, int currentHorizontalPosition, int currentVerticalPosition, char color);
    
    		  break;
    
    		  case 5:
    
    		  printf("How many vertical spaces would you like to move the car? (positive to move up, negative to move down)");
    		  scanf("%d", &verticalSpaces);
    		  
    		  int MoveVertically(int verticalSpaces, int currentVerticalPosition);
    		  void StateOfCar(int stateOfIgnition, int currentHorizontalPosition, int currentVerticalPosition, char color);
    	      break;
              }
    	  
    	  break;
    
    }
    return 0;
    
    }
    
    
    char ColorAssignment(char color)
    
    {
    
    srand (time (NULL));
    switch ( rand() % 5 )
    {
       case 0: color = 'R'; 
    	   break;
       case 1: color = 'G'; 
    	   break;
       case 2: color = 'B';
    	   break;
       case 3: color = 'W'; 
    	   break;
       case 4: color = 'S'; 
    	   break;
    }}
    return color;
    
    }
    PS. Yes, it is suppossed to be an infinite loop.

  2. #2
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Code:
    char ColorAssignment(char color)
    {
        srand (time (NULL));
        switch ( rand() % 5 )
        {
           case 0: color = 'R'; 
    	   break;
           case 1: color = 'G'; 
    	   break;
           case 2: color = 'B';
    	   break;
           case 3: color = 'W'; 
    	   break;
           case 4: color = 'S'; 
    	   break;
        }}    <---------------------- remove one of the brackets!
        return color;
    }

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,657
    >>case 1:
    >>int IgnitionOn (int stateOfIgnition);


    C'mon man, you can't declare a function within a function!



    ITSA
    Socket Library!

  4. #4
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Sebastiani
    >>case 1:
    >>int IgnitionOn (int stateOfIgnition);


    C'mon man, you can't declare a function within a function!
    yes you can!!! just not at that particular location (same location restrictions as any other declaration)
    hello, internet!

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    8
    Ok, removing that bracket didn't help. In fact it created more errors. And this is my first attempt at anything involving functions, so i'd be grateful for any help you can give me.


    would it just be

    case 1:
    IgnitionOn()
    break;

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    17

    Hope it answers

    if your function is declared as

    int IgnitionOn (int stateOfIgnition);

    your case code would be something like:

    int SI; (just to show you SI is an int that must be declared)

    case 1:

    IgnitionOn (SI);

    break;

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    8
    Great Thank you. I did all that, and now that section of my code works at least..... heh....can't say the same for the rest of it, but i'll keep working.
    Last edited by blackgingr; 11-14-2002 at 06:29 PM.

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    17

    Wink this is C :oP

    Well, that's life, that's C!!!

    It's always like it... sometimes it doesn't show some errors since you fix other "main" ones... If you are a beginner i think you should read some good tutorial before you start just trying to write a code. It will probably help you a lot in find your "errors" :oP

    Or don't complain about error messages :oPPP

    try to make your code cleanner:

    case 1:

    IgnitionOn (SI);

    break;

    can be write in one line

    case 1: IgnitionOn (SI); break;

    and i think this sometimes it makes the code easier to read (although sometimes it doesn't :oP)

    And just one thing more, people are here to help, but i give you just one advice, if you start programming in C just "writing code" you will probably get bored and will never learn it... C it's a cool language I think but you will never learn it if you don't learn its basic concepts... and this you will not learn here :o)

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    8
    I'm not just randomly trying to learn C. I'm actually taking a college course for it. I just occasionally need help with what may seem like very easy concepts to other people, but are foreign to a beginner. I only post here after i've been working on and playing with my code for a long time.

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    17

    i didn't mean...

    i'm not complaining about your post and i didn't want to be rude if you thought i sounded like it... It was REALLY an advice...
    I even don't call myself a C programmer because i think i still have a lot to learn, and if i told you what i did it was because i'm studying yet and i see people complaining about C ALL THE TIME!!! And usually the main reason is: they don't understand how it works really...

    I have seen that if you try to understand what is "behind" the code you are typing it makes things a LOT of easier... (specially when you start to work with POINTERS, specially when talking about memmory allocation)

    I was not even talking about your post itself and sorry if i have sounded rude

  11. #11
    Registered User
    Join Date
    Oct 2002
    Posts
    8
    s'ok, i'm sorry if i also sounded rude. I normally do try to understand what's behind it, but I've already been working on this code for days, and its due tonight, and i'm panicking slightly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code to find th square root of a number
    By CODY21 in forum C++ Programming
    Replies: 34
    Last Post: 10-29-2010, 09:27 AM
  2. Replies: 12
    Last Post: 06-08-2005, 11:23 AM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C# Programming
    Replies: 0
    Last Post: 10-14-2002, 01:26 PM
  4. Code to find the day type of day in the year?
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-01-2002, 07:58 PM
  5. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM

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