|
-
Jan 7th, 2001, 02:15 PM
#1
Thread Starter
Hyperactive Member
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
-
Jan 7th, 2001, 02:21 PM
#2
Monday Morning Lunatic
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
-
Jan 8th, 2001, 10:33 AM
#3
Thread Starter
Hyperactive Member
Thanks : it worked
WP
-
Jan 8th, 2001, 11:35 AM
#4
Thread Starter
Hyperactive Member
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
-
Jan 8th, 2001, 01:22 PM
#5
Monday Morning Lunatic
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|