Thread: Why won't this change string to int?

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    4

    Why won't this change string to int?

    comm keeps giving me 0, and I don't know why...

    Code:
    int main()
    {
    
     int memory[64];
     string command, s1, input, s2, space;
     int eight, num1, num2, temp, comm;
    
     int valid_location(int);
     void get_numbers(string, string&, string&);
    
     space = " ";
    
     for (int i = 0; i < 64; i++)
            {
              memory[i] = -999;
            }
    
     cout << endl << "Command: ";
     getline(cin,input);
    
     command = input.substr(0,1);
     comm = atoi(command.c_str());
    
     cout << command << endl;
     cout << comm;
    
     return 0;
    }
    Thanks!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Jenica
    comm keeps giving me 0, and I don't know why...
    What input are you trying to enter at your "Command:" prompt?

    Code:
    int main()
    {
    
     int memory[64];
     string command, s1, input, s2, space;
     int eight, num1, num2, temp, comm;
    
     
     // These two lines should not be here, they belong outside of the main function
     int valid_location(int);
     void get_numbers(string, string&, string&);
    
     space = " ";
    
     for (int i = 0; i < 64; i++)
            {
              memory[i] = -999;
            }
    
     cout << endl << "Command: ";
     getline(cin,input);
    
     command = input.substr(0,1);
     comm = atoi(command.c_str());
    
     cout << command << endl;
     cout << comm;
    
     return 0;
    }
    "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

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    If the 1st char of the command is not a number, atoi will not convert it and will simply return 0.

    You need to validate the input before you give it to atoi

    PK

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Code:
    int valid_location(int);
    void get_numbers(string, string&, string&);
    I'm surprised you're not getting errors here. Function prototypes have to be declared outside of main().

    Also, if you declare command as a char, you can also replace input.substr(0,1) with input[0]. Then, instead of using atoi(), you just need to do:
    Code:
    char command;
    command = input[0];
    comm = command - '0';
    Other than that, assuming that your input has a digit as the first character and you have your parameters in substr() correct, I don't see anything wrong (except for the function prototypes that are stuck in there out of nowhere).
    Last edited by Hunter2; 10-07-2004 at 11:48 AM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM