Results 1 to 6 of 6

Thread: String to Int How?

  1. #1

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335

    String to Int How?

    is this the proper way?
    5. How to convert a string to a number?

    - Use the atoi function to convert a string to an integer.

    Example:

    ---------------------------------------------------------------------------------------------------------------------------

    #include <iostream> //preprocessor statement

    #include <stdlib.h> //contains itoa & atoi

    using namespace std; //choosing the namespace that we are going to use



    int main() //this is where the program starts executing

    {

    char buffer[20];

    int i = 3445;

    _itoa( i, buffer, 10 );

    cout<<buffer<<endl;

    return 0;

    }
    Is that the way its the same as he has for the Int to String. I found this in the main tutorial link
    I Tried using the below
    VB Code:
    1. char buffer[20];
    2.                           char buffer2[20];
    3.                          
    4.                           ::GetWindowText (edit,buffer,255);
    5.                           ::GetWindowText (edit,buffer2,255);
    6.            
    7.                           int i = buffer;
    8.                           int i2 = buffer2;
    9.                           int i3 = i + i2;   
    10.  
    11.                         _itoa( i, buffer, 10 );
    12.                         _itoa( i2, buffer2, 10 );
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Code:
    char buffer2[20]; 
    
    ::GetWindowText (edit,buffer,255);
    ::GetWindowText (edit,buffer2,255);
    int i = buffer;
    int i2 = buffer2;
    int i3 = i + i2;	  
    
    _itoa( i, buffer, 10 );
    _itoa( i2, buffer2, 10 );
    Ok, think about it. First, you allocate a buffer for 19 characters. Then you retrieve the text of an edit box, where you tell windows that you have space for 255 characters. First error.
    Then you assign the buffers to integers. This won't compile. And in the thing you quoted it says "Use the atoi function to convert a string to an integer." Second error.
    Finally you convert those integers (that have a random value) back to strings and store them in the buffers. I guess that's not what you wanted to do either.

    Ok, so you basically used the wrong function. Here's how to get an integer from an edit box:
    Code:
    char buffer[256]; // You gotta keep your promises.
    ::GetWindowText(edit, buffer, 255);
    int i = atoi(buffer);
    Or simpler (if you have the id of the edit box):
    Code:
    int i = ::GetDlgItemInt(IDC_EDIT);
    _itoa is a Microsoft-specific function. The standard way to convert an integer to a string in C is sprintf:
    Code:
    sprintf(buffer, "%i", i);
    and in C++ stringstream
    Code:
    std::ostringstream oss;
    oss << i;
    std::string s = oss.str();
    (from the header <sstream>).
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335
    ok so far so good now i tried
    VB Code:
    1. std:stringstream oss;
    2. oss << i3;
    3. std::string s = oss.str();
    but no good i get like 3 errors
    DO i have to include any h files?
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  4. #4

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335
    Ok now i got it lol im so stupid thanks alot you do help so much and srry bout before
    VB Code:
    1. char buffer[256]; // You gotta keep your promises.
    2. char buffer2[256]; // You gotta keep your promises.
    3. char buffer3[256]; // You gotta keep your promises.
    4.  
    5. ::GetWindowText(edit, buffer, 255);
    6. ::GetWindowText(edit2, buffer2, 255);
    7.  
    8.      int i = atoi(buffer);            
    9.      int i2 = atoi(buffer2);
    10.      int i3 = i + i2;
    11.                
    12. sprintf(buffer3, "%i", i3);
    13. ::SetWindowText(edit3,buffer3);
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  5. #5
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    (from the header <sstream> ).
    DO i have to include any h files?
    I rest my case...
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    And <string> too.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width