Thread: is this right

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    34

    is this right

    Code:
    #include <stdio.h>
    
    int main()
    
    {
        
        int age;
        int name;
        
        printf( "enter your name" );
        scanf( "%d", name );
        if ( name == reece ) {
             printf( "wow thats my name aswell\n" );
             }
             else {
                  printf( "you are &name\n" );
                  }
             printf( "enter your age" );
             scanf( "%d", age );
             if ( age == 17 ) {
                  printf( "wow I'm that age\n" );
                  }
                  else {
                       printf( "your name is &name\n" );
                       }
                       getchar();
                       reutnr 0;
                       }
    i wrote this on my own, just for fun to see if it works but it gets an error on the if ( name == Reece ); line... ne suggestions on it?

    and also is this the best way to write a code like?

  2. #2
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    you can't compare strings like that...you need to use strcmp function of <string.h>

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yeah name is an integer not a string and reese isn't declared.

    You'd want something like this:

    Code:
    char name[20];
    scanf("%s", name);
    if (strcmp(name, "Reese") == 0) {}
    Look at this:
    http://www.cprogramming.com/tutorial/lesson9.html
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    thanks it works now.

    but when i type something other than Reece, how do i get it to say, your name is "....." and same with age ?

    + when it says enter your name the thing u type starts right at the end of the sentance with no space.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    else {
          printf( "You are %s\n", name );
    }
    Sent from my iPadŽ

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Stop spamming the board with identical questions.

Popular pages Recent additions subscribe to a feed