PDA

Click to See Complete Forum and Search --> : string -> int


WP
Jan 7th, 2001, 01:15 PM
Hi,

I made a program in C++ and now I will pass parameters to it. I used this:

#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:

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

parksie
Jan 7th, 2001, 01:21 PM
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:

int main(int argc, char **argv) {
myinteger = atoi(argv[1]); //the first parameter in a number
return 0;
}

WP
Jan 8th, 2001, 09:33 AM
Thanks :D: it worked

WP

WP
Jan 8th, 2001, 10:35 AM
[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

parksie
Jan 8th, 2001, 12:22 PM
Hehehe...you forgot your [ /code ] tag ;)

Just use:

myinteger = atoi(mystring.c_str());