c++ help

brtspears2

Diamond Member
Nov 16, 2000
8,659
1
81
I have this code snip...

while(getline(inScoresFile, name))
{
getline(inScoresFile, grades);
cout << name << grades << endl;
}

And for the buffer named grades on line 3, I want to be able to "parse a buffer" to get intergers out of it, but It's stored as string, any ideas? I want to store whats in the string grades into variables i, j, k respectivly.

The file looks like

I tried to just go straight up with insertion operators, inScoresFile >> i >> j >> k, but the loop doesnt continue for all the names in the file.

Name Lastname
10 20 30
Name AnotherName
40 50 60

and so on.....
 

Tdawg951

Member
Nov 28, 2001
169
0
0
i think i get what you are saying,
you haev a line of characters, and you want to change teh numbers in character form into actual integer form, right?
 

brtspears2

Diamond Member
Nov 16, 2000
8,659
1
81
more like a string of characters....

getline reads a line, then stores it to a buffer somewhere...

the buffers name and grades are defined as string type.

Particually for the buffer named grades, it getline's as 10 20 30.

I want to be able to parse the buffer grades for each one of those intergers, and then store each interger in each a variable thats of int.
 

BlackOmen

Senior member
Aug 23, 2001
526
0
0
Hmm, I think your syntax might me a little off.

assuming that inScoresFile is the fstream you are using for input, then the code should look something like this:

string name;
queue<int> grades;
int grade;

while( !inScoresFile.eof() )
{
inScoresFile.getline(name);
for(int i=0; i<3; i++)
{
inScoresFile >> grade;
grades.push(grade);
}
cout << name;
for(int i=0; i<3; i++)
{
cout << ' ' << grades.top();
grades.pop();
}
cout << endl;
}

Or at least that's what came off thetop of my head.

Edit: fixed syntax for queue declaration.
 

Tdawg951

Member
Nov 28, 2001
169
0
0
on second thought, i just looked at it again, and
maybe somethign like this would work:

while(getline(inScoresFile, name))
{
inScoresFile>> i >> j >> k;
cout << name <<" "<< i <<" "<< j <<" "<< k<<endl;
}

keep in mind that getline doesnt throw away the /n (at least i dont think it does), you might want to get rid of that

there is also another way do to this using getline, but it is a bit more complicated

if you take in the stuff with getline, you can go through and parse it for number characters (using spaces) and then just put out those parts of the array

hope that helps
 

brtspears2

Diamond Member
Nov 16, 2000
8,659
1
81


<<
if you take in the stuff with getline, you can go through and parse it for number characters (using spaces) and then just put out those parts of the array

>>




How does this work? cin.get ?
 

Platypus

Lifer
Apr 26, 2001
31,046
321
136


<<

<<
if you take in the stuff with getline, you can go through and parse it for number characters (using spaces) and then just put out those parts of the array

>>




How does this work? cin.get ?
>>




Do you guys have the apstring class?
 

Tdawg951

Member
Nov 28, 2001
169
0
0
if you are using apstring, its very easy, if not, it will be harder, but can still be done......what are you using? also, do you want the characters that are numbers actually converted to a integer, or just want to output the string?
 

Tdawg951

Member
Nov 28, 2001
169
0
0
i need to go, so here is some code.....

char grades[15]; //char buffer
while(getline(inScoresFile, name))
{
getline(inScoresFile, grades);
for (int i = 0; i<15;i++)
if (grades==" ") //if its the space
cout<<grades[i+1]<<grades[i+2]; //puts out the next two digits
}

this only works if there is a space before every number and if every number is 2 digits, and is not the best way to do this.
You can probably mod the code a bit to make it work. Ideally you would want to do exactly what i posted before, just take in some integers, and forget teh getline function the second time. I think your problem lies with the endline character (the /n) in that it trys to read it again, and just sees teh endline and stops, you might want to do some research on that.

hope this does it, i g2g
 

brtspears2

Diamond Member
Nov 16, 2000
8,659
1
81


<<

<<

<<
if you take in the stuff with getline, you can go through and parse it for number characters (using spaces) and then just put out those parts of the array

>>




How does this work? cin.get ?
>>




Do you guys have the apstring class?
>>



Simply put, no.