Thread: string problems

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    2

    string problems

    hi there,

    im trying to link number to words, but i keep getting errors i cant find what is wrong can someone help, i no is something basic, im just missing it. the code is here:
    Code:
    void main()
    {
    int number;
    char name[100];
    
    printf("type a number");
    scanf("%d",&number");
    
    if (number == 14) {
     name = tom; }
    else if (number ==10) {
     name == gary;}
    
    printf("the name is %s/n", name);
    
    }
    thanks

    for the help

    pure!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You did not declare any variables called tom or gary. If you want strings, then you have to use quotes like you did everywhere else you used strings.

    To put information into a string, like name, you use strcpy.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. main should be int main(void)
    2. when you have compiler errors - post them as well and point the lines where errors occured
    3. tom and gary are not strings, they are variable names. To make strings use ""
    "tom" and "gary" are strings
    4. you cannot assign to arrays - use strcpy
    5. or declare your name variable as pointer to string
    const char* name = "";

    in this case you can assign to this pointer addres of the string literal

    name = "gary";
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by vart View Post
    1. main should be int main(void)
    2. when you have compiler errors - post them as well and point the lines where errors occured
    3. tom and gary are not strings, they are variable names. To make strings use ""
    "tom" and "gary" are strings
    4. you cannot assign to arrays - use strcpy
    5. or declare your name variable as pointer to string
    const char* name = "";

    in this case you can assign to this pointer addres of the string literal

    name = "gary";
    6. /n should be \n

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. io stream and string class problems
    By quizteamer in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2005, 12:07 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM