problem in writing to file

In this code I am able to enter the name and marks for the first time correctly but after that I can only enter the marks and not name.
please help

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 <fstream>
#include<iostream>

using namespace std;

int main()
{
    ofstream fout("student",ios::out);
    char name[30],ch;
    int i;
    float marks=0.0;
    for(i=0;i<5;i++)
    {
    cout<<"Student"<<(i+1)<<":\tName:";
    cin.getline(name,30);
    cout<<"\t\tMarks:";
    cin>>marks;
    fout<<name<<"\n"<<marks<<"\n";
    }
    fout.close();
    ifstream fin("student",ios::in);
    fin.seekg(0);
    cout<<"\n";
    for(i=0;i<5;i++)
    {
    fin.get(name,30);
    fin.get(ch);
    fin>>marks;
    fin.get(ch);
    cout<<"Students Name : "<<name;
    cout<<"\tMarks :"<<marks<<"\n";
    }
    fin.close();
    return 0;
}
Last edited on
You are intermingling formatted input with unformatted input.
See: http://www.cplusplus.com/forum/general/69685/#msg372532
Topic archived. No new replies allowed.