string problems

This is a discussion on string problems within the C Programming forums, part of the General Programming Boards category; hi there, im trying to link number to words, but i keep getting errors i cant find what is wrong ...

  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,185
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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";
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 09:33 PM

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