int problem

This is a discussion on int problem within the C++ Programming forums, part of the General Programming Boards category; OK I have the following: char msg[1024]; int test = int(msg); test++; OK the msg variable gets the value 200. ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    int problem

    OK I have the following:

    char msg[1024];
    int test = int(msg);
    test++;

    OK the msg variable gets the value 200. Why is test after this little code not 201, but 00? And what I have to change to get my 201 in test?

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    While syntactically correct, this is not doing what you think it's doing. Use the atoi function to convert a string to int.

    int i = atoi( "17" );

    or in you case

    int test = atoi( msg );
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    71
    Quote Originally Posted by nvoigt
    While syntactically correct, this is not doing what you think it's doing. Use the atoi function to convert a string to int.

    int i = atoi( "17" );

    or in you case

    int test = atoi( msg );
    hmm but than my programms crahes. msg is only a pointer to the real char variable. what can I do?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    How are you setting msg to 200?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    why don't you change the msg to string or char*. It will be much easier.
    Hello, testing testing. Everthing is running perfectly...for now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  2. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 03:24 PM
  5. Replies: 2
    Last Post: 03-24-2006, 07:36 PM

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