File Input/output to arrays

Hi People,

Can anybody help me with my problem, I'm really confused.

I have an assignment to write a program which will read data from the file text.txt which contains names of the name and surname of a student separated by a space and 10 numbers which represent his scores:

Jonh Smith 12 34 56 78 14 67 89 89 76 54
Mark Kruger 23 45 67 87 65 43 47 87 42 19
Anna Krofman 34 55 75 23 45 78 78 45 56 19
Hanz Shertz 45 67 78 54 35 46 54 36 4


My task is to output the content of the file and add an additional number which represents compute numeric grades for a course.

my Problem is how to READ Character and numerical data from the file and assign it to 4 different strings and numerical arrays and and how to pass it to to pass it to the function which will calculate complete score.

I don't know howto stop input in a certain place (when first name finishes) and start input again from a certain place again (when another name starts)

#include <iostream>
#include <fstream>
using namespace std;

int calcul(int x[], int z){
double y;
for (int i = 0; y<10; i++){
y = y + x[i];
}
return y;
}


int main(){
char reply;

do{

ifstream fin;
string name1, name2, name3, name4;
int score1[10];
int score2[10];
int score3[10];
int score4[10];

fin.open("text.txt");

getline(fin, name1);
//here i can't understand how I can stop input after the first name finishes and continue input for the next string from the beginning of the next line

for (int a=0; a<10; a++){
fin>> score[a];
}

//the same problem here. How to assign an exact place in the file where program has to start reading the input for the array and where it should finish


calcil(score1,10);
calcil(score2,10);
calcil(score3,10);
calcil(score4,10);



cin>> reply;
}while (reply != 'n');
return 0;
}

getline has an optional parameter that is '/n' by default. By setting it to ' '(space), you can call it twice (using temporary strings) and put the name together. getline discards the deliminator, so the get pointer will be positioned correctly when the time comes to extract the test scores.
Thanks for the suggestions. It really helped a lot. But I still have a question. How to stop input of the array of numeric scores in case a student has less than ten grades/
After getting the name, you could load the rest of the line into a string using getline(file, string) without the delimiter parameter. Once loaded, you could parse it manually, keeping track of the amount of scores you get. For parsing, I would recommend iterating through the string one char at a time, storing chars up to ' ' into a temporary string. Upon hitting ' ', you could use a stringstream to convert it to an int
Topic archived. No new replies allowed.