Results 1 to 5 of 5

Thread: string -> int

  1. #1

    Thread Starter
    Hyperactive Member WP's Avatar
    Join Date
    Aug 2000
    Location
    Belgium
    Posts
    278

    Angry

    Hi,

    I made a program in C++ and now I will pass parameters to it. I used this:
    Code:
    #include <iostream>
    #include <ctype.h>
    #include <fstream>
    #include <string>
    #define cls() system("cls");
    
    using namespace std;
    int myinteger;
    
    int main(int argc, char* argv[])
    {
       myinteger = argv[1];  //the first parameter in a number
       return 0;
    }
    he gives the error of the reinterpret_cast stuff, so I added these:
    Code:
    int main(int argc, char* argv[])
    {
       myinteger = reinterpret_cast <int> (argv[1]);  //the number in the parameter was 5, but he returns 8392093 !!!
       return 0;
    }
    any idea's ?

    WP

    Visual Basic 6.0 EE SP5 / .Net
    Windows XP

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    argv[1] is a pointer to a character array, so you've put the memory address of the string into the variable. You need to use the atoi function:
    Code:
    int main(int argc, char **argv) {
       myinteger = atoi(argv[1]);  //the first parameter in a number
       return 0;
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Hyperactive Member WP's Avatar
    Join Date
    Aug 2000
    Location
    Belgium
    Posts
    278

    Thumbs up

    Thanks : it worked

    WP

    Visual Basic 6.0 EE SP5 / .Net
    Windows XP

  4. #4

    Thread Starter
    Hyperactive Member WP's Avatar
    Join Date
    Aug 2000
    Location
    Belgium
    Posts
    278

    Unhappy What about this

    [code]
    int main(...)
    {
    int myinteger;
    string mystring;
    char file;
    ifstream file("myfile.dat")
    getline(file,mystring); 'mystring is "10"
    myinteger = mystring; 'ERROR
    file.close()
    }

    It isn't the same is it ?

    WP

    Visual Basic 6.0 EE SP5 / .Net
    Windows XP

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Hehehe...you forgot your [ /code ] tag

    Just use:
    Code:
    myinteger = atoi(mystring.c_str());
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

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