|
-
December 13th, 2006, 07:30 PM
#1
atoi is making my program crash.
I am using atoi to convert a string to an int, and It is doing that successfully. however, having the following section of code in my program is making the program deliver an error upon reaching "return 0;"
Code:
converted[0] = atoi(score[1].c_str());
converted[1] = atoi(score[3].c_str());
converted[2] = atoi(score[5].c_str());
converted[3] = atoi(score[7].c_str());
converted[4] = atoi(score[9].c_str());
-
December 13th, 2006, 07:41 PM
#2
Re: atoi is making my program crash.
 Originally Posted by JackM
I am using atoi to convert a string to an int, and It is doing that successfully. however, having the following section of code in my program is making the program deliver an error upon reaching "return 0;"
Code:
converted[0] = atoi(score[1].c_str());
converted[1] = atoi(score[3].c_str());
converted[2] = atoi(score[5].c_str());
converted[3] = atoi(score[7].c_str());
converted[4] = atoi(score[9].c_str());
Please read your own post, and see if it is at anyway helpful in finding the problem. The code you posted, will not help others find your error.
Please post a complete, small, and compilable example that demonstrates the problem. No one knows what "converted" is, "score" is, etc., or more important, how, when, where, and with what values this code is being called with.
Regards,
Paul McKenzie
-
December 13th, 2006, 08:20 PM
#3
Re: atoi is making my program crash.
true, sorry, I guess that was not very clear. hopefully this will make more sense.
Code:
//read from external file
ifstream inFile("C:\\hangman\\Highscores.txt");
//test the stream
if(!inFile.is_open()){
cout << "Error while attempting to access external file." << endl;
return 1;
}
int k = 0;
string score[9] = "";
while(!inFile.eof()){
getline(inFile, score[k], ' ');
k++;
}
cout<<endl;
inFile.close();
int converted[4];
converted[0] = atoi(score[1].c_str());
converted[1] = atoi(score[3].c_str());
converted[2] = atoi(score[5].c_str());
converted[3] = atoi(score[7].c_str());
converted[4] = atoi(score[9].c_str());
return 0;
If I leave out:
Code:
converted[0] = atoi(score[1].c_str());
converted[1] = atoi(score[3].c_str());
converted[2] = atoi(score[5].c_str());
converted[3] = atoi(score[7].c_str());
converted[4] = atoi(score[9].c_str());
then it doesnt encounter the error.
-
December 13th, 2006, 09:13 PM
#4
Re: atoi is making my program crash.
Well, this still isn't enough code to reproduce the result effectively, but I am gonna take a guess as to what is causing the problem...
Look here...
Code:
string score[9] = "";
while(!inFile.eof()){
getline(inFile, score[k], ' ');
k++;
}
cout<<endl;
inFile.close();
int converted[4];
converted[0] = atoi(score[1].c_str());
converted[1] = atoi(score[3].c_str());
converted[2] = atoi(score[5].c_str());
converted[3] = atoi(score[7].c_str());
converted[4] = atoi(score[9].c_str());
You create an array of 4 elements called converted. That's fine.
You create an array of 9 elements called score. That's fine also
But, arrays begin indexing from element 0. That means there is no index of [4] in the converted array and there is no index [9] in the score array.
The only valid indexes for converted are 0, 1, 2, 3
The only valid indexes for score are 0, 1, 2, 3, 4, 5, 6, 7, 8
Try getting rid of that last line and see what happens.
Last edited by dcjr84; December 13th, 2006 at 09:20 PM.
Please rate my post if you felt it was helpful
-
December 13th, 2006, 09:17 PM
#5
Re: atoi is making my program crash.
1) how many lines in your input file ? There are no checks
that you do not go past the array bounds.
2) You are accessing past the array bounds here (at the very least) :
Code:
string score[9] = "";
converted[4] = atoi(score[9].c_str()); // 9 is not a valid index
3) as dcjr84 mentioned, how is converted declared ?
-
December 13th, 2006, 09:20 PM
#6
Re: atoi is making my program crash.
The argument for atoi() must be a pointer.
The correct syntax is int = atoi( char* )
-
December 13th, 2006, 09:21 PM
#7
Re: atoi is making my program crash.
 Originally Posted by sstainba
The argument for atoi() must be a pointer.
The correct syntax is int = atoi( char* )
The string::c_str() member function returns a const char*.
The syntax he is using is correct.
1) how many lines in your input file ? There are no checks
that you do not go past the array bounds.
You are correct Phillip. This needs to be checked also.
3) as dcjr84 mentioned, how is converted declared ?
I admit, I didn't notice it at first either, but its there.
It's declared as int converted[4].
Note the number of elements is 4, yet he tries to access element 4, which is incorrect. I noted he should change that also in my post.
Last edited by dcjr84; December 13th, 2006 at 09:25 PM.
Please rate my post if you felt it was helpful
-
December 13th, 2006, 11:25 PM
#8
Re: atoi is making my program crash.
lol... wow, I'm an idiot. Thanks for taking time to point out my supid error tho!
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
|