Input into array from .txt

Hello there, I am trying to input data from a .txt into an array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int HighScore(int PlayerScore){
int x;   
					
fstream scorefile ("scores.txt");
string line;
				  
if (scorefile.is_open())
{
  while (scorefile.good() )
  {
  getline (scorefile,line);
  scoresort[x] = line.c_str();
  cout << line << endl;
				      
  x++;
  }
				    
}
				
else {cout << "File NOT opening";}


This crashes my code, I do not know why. Maybe a conflicting data type? What data type comes out of .txt? I am trying to input it into an array of strings.

The data is for a high score sorting system. It is in the format SNI4465 where SNI would be the first three letters of a name and number following will be the score.

I will then manipulate this data to get the scores of each. Sort them, then rearrange. Repackage the string and put back into the txt file, ready to be read off when hiscores is called.
Also is there anyway to use classes for this problem?
Can you post your program's complete code? Easier to tell what's wrong by running it and can't do that without your headers and main().

Otherwise, just quickly looking at it from what you posted... scoresort[x] is undefined.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <string>
#include <iostream>
#include <fstream>
#include <math.h>
#include <sstream>
string scoresort[9];

int HighScore(int PlayerScore){
int x;   
					
  				  fstream scorefile ("scores.txt");

				  string line;
				  
				  if (scorefile.is_open())
				  {
				    while (scorefile.good() )
				    {
					getline (scorefile,line);
        			scoresort[x] = line.c_str();
				    cout << line << endl;
				      
					  x++;
				    }
				    
				  }
				
				  else {cout << "File NOT opening";}
for(int i;i < 10; i++){ 				
cout << scoresort[0].substr (3,0) << endl;	  	  	  	  
}
				
				
				scorefile.close();
   
//  std::ostringstream o;
//  o << playerscore;
  	      
  
   
  return(PlayerScore);
}




Always crashes when it hits the input into the array part. :(
I have tried scoresort[x] = line.c_str();
and scoresort[x] = line;
Anyone have any ideas?? I think it may be to the type of variable I am using in comparison to the data type input from the .txt
well line 20: you are putting char* into strings and that might do it
you didnt intiliaze x.
why are you taking an int and then returning it without changing it? and why is it in ()?
Taking an int at the moment due to the code not being fully functional yet, therefore I don't want to change the variable until this first section works. Once the code reorganises the structure of highscores then it will find the place to input the new player score (the variable passed).
Also this has been answered now, Thank you very much guys.

Sorry for duplicate post by the way, I didn't realise moderators had to check all posts, therefore I simply though I did not hit "submit" therefore wrote my post again.

Snipes <-- Noob
really? mods check all posts? i didnt know that. so this isn't sarsasm i just want to make sure i understand right. you will edit playerscore but your just not right now becuase you wanted to get the other stuff working?
int x;
x isn't initialized. That's the first problem you must solve i guess.
EDIT: Aha ok, sorry then, I think i missed something.
Last edited on
yeah we covered that. i think he solved it
Topic archived. No new replies allowed.