File I/O, need help

i'm trying to create an Exam Results Information program that get its inputs from input.txt, calculate, and display the output to output.txt ..i encounter problem with the full name though..my code will work if the names in the input.txt is only 1 word instead of full name..

in the input.txt for example have:

Alissa (Name) 134322523 (ID) 8 (No. of Subject) 32(total credits)

if i write 'Alissa', program will run and display the name
but if i write 'Alissa Macmillan'...output will display exponential numbers and stuff..

how to get the program to read and display the full name in the input.txt?

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    ifstream inStream;
    ofstream outStream;

    inStream.open("input.txt");
    outStream.open("output.txt");

    string name[20];
    string ID[20];
    int NoSubjects[20];
    double GPA;
    double totalcred[20];
    int counter=0;

for(counter=0;counter<20;counter++)
    {
         inStream>>name[counter]>>ID[counter]>>NoSubjects[counter]>>totalcred[counter];

        GPA = totalcred[counter]/NoSubjects[counter];


        outStream<<name[counter]<<"\t"<<ID[counter]<<"\t"<<NoSubjects[counter]<<"\t"<<totalcred[counter]<<"\t"<<GPA<<endl;
    }


    inStream.close();
    outStream.close();
    return 0;
}
Last edited on
with the operator>> you're using whitespace (' ', '\t', '\n') as a separator. Hence only the first part of the name is read and ID[counter] gets the second part and so on

I suggest that you use newline ('\n') when writing the separator. To read the entire name you could use getline:

http://www.cplusplus.com/reference/string/string/getline/?kw=getline

Because getline leaves the the newline character in the stream use ignore:

http://www.cplusplus.com/reference/istream/istream/ignore/

after the getline to remove it
yes i read in some website that i should use getline but i don't know where should i put it..
can you point it to me?
Topic archived. No new replies allowed.