Thread: what is wrong with this code ( verryyy nooobbbb )

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    1

    what is wrong with this code ( verryyy nooobbbb )

    Code:
    #include <stdio.h>
    main()
    {
     char name[10];
     short age;
     
     printf("enter your age.\n");
     scanf("%d",&age);
     fflush(stdin);
     printf("please enter your name.\n");
     scanf("%[^\n]",name);
    if (strcmp(name, "bruce") == 1 && (age >13 || age <18))
                           printf("u are the chosen one");
                else
                           printf("u are nothing");
    getchar();
    }


    the error is
    `strcmp' undeclared (first use this function)

    i'm new in c

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to #include string.h . Also, if you are trying to check a range, you want to make sure it's greater than 13 AND less than 18.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Well, a couple of things.

    1. To use string functions you need to #include<string.h>
    2. The minimum C program is:
    Code:
    int main(void){
    
         return 0;
    }
    3. Your indentation sucks. Read How should I indent my code?
    4. Read Why fflush(stdin) is wrong
    5. Your scanf statement is broke.
    Code:
    scanf(%9[^\n]s, name);//Leave space for null character.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    30
    Dude where is the string.h?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by guitargod View Post
    Dude where is the string.h?
    Probably the same place stdio.h is.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by guitargod View Post
    Dude where is the string.h?
    Quote Originally Posted by guitargod View Post
    anything with a floating point is automatically a double foe the computer. here you say 3.14f, it means.. oh wait, it's not double, it is float. use it as a float!
    also check out this discussion -
    c tricky code
    Everytime you post, an angel dies.....
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    #include <stdio.h>
    #include <string.h>  <--- needed for strcmp()
    
    int main( void )   <--- not main()
      {
        char name[10];
        short age;
     
        printf("enter your age.\n");
        scanf("%d",&age);
        fflush(stdin);              <----- remove this! 
        printf("please enter your name.\n");
        scanf("%[^\n]",name);
    
        if (strcmp(name, "bruce") == 0 && (age >13 && age <18))  <--- strcmp() returns 0 when match is found
         printf("u are the chosen one");                         <--- || is incorrect for range checking            
        else
          printf("u are nothing");   <---- learn to spell 
    
        getchar();
       return 0;  <--- main returns an errorlevel to the Operating System
      }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong with the code ?
    By zap85 in forum Linux Programming
    Replies: 3
    Last Post: 08-08-2008, 12:45 AM
  2. HELP! What's wrong with my code!
    By fp123 in forum C++ Programming
    Replies: 5
    Last Post: 01-20-2006, 12:05 PM
  3. What's Wrong With My Code?
    By javacvb in forum C Programming
    Replies: 14
    Last Post: 06-15-2003, 12:09 PM
  4. What is wrong with this code??? Can U help??
    By kickass in forum C Programming
    Replies: 1
    Last Post: 10-31-2002, 03:34 AM