File Reading Issues

Hello fellow programmers,

Recently, I have been having problems with the code below. I enter in my information perfectly fine, but after that, it will not display the information as it should. It displays
Writing to file
, but does not show the file information. To do that, I have to delete lines 13-20.

Here is program output:

What is the person's name? 
Lawrence Huang
What is Lawrence Huang's sex? (F for female, M for male)
M
What is Lawrence Huang's birthdate? 
7 26 2004
Writing to file. 

Program ended with exit code: 0


I should be getting something along the lines of this:

What is the person's name? 
Lawrence Huang
What is Lawrence Huang's sex? (F for female, M for male)
M
What is Lawrence Huang's birthdate? 
7 26 2004
Writing to file. 
Lawrence Huang M 7 26 2004

Program ended with exit code: 0


Here is my code:
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
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream inStream;
    ofstream outStream;
    const int people = 30;
    string info[people], name;
    int i = 0, input, count = 0, month, day, year;
    char sex;
    
    outStream.open("personsInfo.txt");

    cout << "What is the person's name? " << endl;
    getline (cin, name);
    cout << "What is " << name << "'s sex? (F for female, M for male)" << endl;
    cin >> sex;
    cout << "What is " << name << "'s birthdate? " << endl;
    cin >> year >> month >> day;
    
    cout << "Writing ";
    outStream << name << " " << sex << " " << year << " " << month << " " << day;
    cout << "to file. " << endl;
    
    inStream.open("personsInfo.txt");
    
    while (getline (inStream, info[i]))
        i++;
    
    for (int j = 0; j < i; j++)
        cout << info[j];
    cout << endl;
    inStream.close();
    outStream.close();
}


Thanks,
VX

Note: Some of the variables are unused, I plan to use them later.
Last edited on
I think you need to close outStream before you can read the file.
Yeah, that's true, i tried to put outStream.close(); just after writing to file. Now it's working.
Ahh, thanks. Problem is fixed now!

Thanks so much,
VX
Topic archived. No new replies allowed.