Thread: what is wrong =(

  1. #1
    Unregistered
    Guest

    what is wrong =(

    i keep tring and trying but i cant seem to find out what is wrong with this damn program.. this is the error that i get..

    error C2143: syntax error : missing ';' before 'type'
    Error executing cl.exe.

    sec.obj - 1 error(s), 0 warning(s)

    and here is the code...

    Code:
    #include <stdio.h>
    
        char name(void);
    
    int main()
    {
    
    	char nam;
    
    	 printf("What is your name?\n");
    	 scanf("%c",&nam);
    
    	 if (nam != name())
    	 {
    		 printf("No your name is not %c\n",nam);
    		 printf("Your name is %c \n",name());
    	 }
    	 else
     {
    		 printf("Hmm i dont recognize this name!\n");
    		 return 0;
     }
    
    	 char name(void)
    {
    		 char nick[0] = ****y;
    		 return 0;
    	 }
    any help... ?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    A char holds ONE character. An array of chars (a string) holds more (as many as you specify). Strings, and indeed ALL arrays cannot be compared like:
    if(a == b) ...etc.
    The strcmp() function returns 0 if two strings match, non-zero otherwise...


    Code:
    
    #include <stdio.h>
    
        char* name(void);
        int Identical(char a[], b[] ) { return strcmp(a, b) ? 0 : 1 } 
        
    int main()
    {
    
    	char nam[100];
    
    	 printf("What is your name?\n");
    	 scanf("%s",&nam);
    
     if ( !Identical(nam, name() ) )
     {
        printf("No your name is not %s\n",nam);
        printf("Your name is %s \n",name());
      }
     else
      {
        printf("I know you, %s!\n", name() );
      }
    
    system("PAUSE"); //..edit: you may need this too :)
    
    return 0;
    }
    
    
    
    
    
    
    char *name(void)
    {
     char nick[] = "****y";
     return nick;
     }
    Last edited by Sebastiani; 06-30-2002 at 11:05 PM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    1.) Try to indent your code, looks neat and helps in organising your code
    2.) Close the } in main(), you have missed it
    3.) int main() should return a value
    4.) char nick[0] = ****y;
    &nbsp;&nbsp;&nbsp;&nbsp; Cannot have 0 there
    &nbsp;&nbsp;&nbsp;&nbsp; What is this 'y'?
    5.) Can you have a name with just one character? Make it a string

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I'm looking at your name() function and seeing some new problems that may arise.

    Here is what you should do:

    Code:
    char *name(void) {
        char *nick = (char *)malloc(5);
        strcpy(nick, "****y");
        return nick;
    }
    The reason why you should do it this way is because before you were returning the address of a local variable. A local variable is temporary and will be deleted once the function is done

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by master5001
    I'm looking at your name() function and seeing some new problems that may arise.

    Here is what you should do:

    Code:
    char *name(void) {
        char *nick = (char *)malloc(5);
        strcpy(nick, "****y");
        return nick;
    }
    The reason why you should do it this way is because before you were returning the address of a local variable. A local variable is temporary and will be deleted once the function is done
    Almost right The string ****y is 5 in length, therefore you need to malloc 6 bytes to hold it. Also, you should really check the return code from malloc to ensure it's not NULL. To be honest, though, the use of malloc() is not really required for storing hardcoded value. If you are going to use it though, don't forget to free() the memory later.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM