How to read data from text file in same format as file?

Here is my information in my text file:
Lily Grand
4622 C 32.00 10.50
Tony Stone
5014 A 43.00 7.25
Jessica Land
3311 B 20.00 10.15
Emily Martin
6570 C 23.00 12.50

I need to cout this info to the screen in the exact way that it appears in the text file. Is this possible? Right now it only reads one letter or number per line. so it looks like this:

L
i
l
y
G
r
a
n
d
And so on until it reaches the end of the file.

NOTE: This program is not complete. I am working on the individual functions before I finish the rest. I am currently working in the OpenTheFile function.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*Program to determine company's weekly payroll*/

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

void OpenTheFile()
{
ifstream inputFile;
string filename;
char info;
int number;

//Opens the file
inputFile.open("Lab2part2.txt");

//If file opens successfully, process it.
if (inputFile)
{
    //Displays info from file.
    while (inputFile >> info)
    {
        cout << info << endl;
    }
cout << "File opened successfully.\n";
}
}

void NameTheFile()
{

ifstream inputFile;
string filename;

//Asks user for file name.

cout << "What is the file name?\n.";
cin  >> filename;

    if (filename == "Lab2part2")
    {
        cout << "Opening the file...\n";
        OpenTheFile();
    }
    else
    {
        //Why does this message also display after file opens?
        cout << "Error opening the file.\n";
    }
}

void GetTheData()
{
    //statement
    //statement
    //statement
}

void CalculateTheTotalPay()
{

    //statement
    //statement
    //statement
}

void PrintTheData()
{
    //statement
    //statement
    //statement
}

int main()
{
    //Main to call other functions
    cout << "Welcome.\n";
    NameTheFile();

    return 0;
}
Last edited on
You're reading into info, which is defined as a char type variable - char types only hold one character.

Looking back at the assignment description in the other thread - it sounds like you need to create 5 parallel arrays and load the data into the arrays. (e.g. string array for names, integer array for department number etc.) It doesn't sound like you need to print anything out until the last function to be called from main?

Line 44 - wouldn't you be calling this function from main after line 79?
I changed it to a string so it prints the data out in a better format. It's not the same as the file, but you're right thats not really the point of the assignment.

I'm not really sure how to work my arrays into the program using the information from the file.

I've never worked with arrays before.

If you could help me figure this out it would be greatly appreciated.
It's possible to read all the fields for each record in one go. Take this code:
1
2
3
4
    while (inputFile >> info)
    {
        cout << info << endl;
    }


and expand it to something like:
1
2
3
4
    while (inputFile >> fname >> lname >> three >> four >> five >> six )
    {
        cout << first << ' ' << last << endl;
    }

In my example, fname and lname are strings into which the first and last names will be read. The other variables will need meaningful names, and an appropriate type such as int or double or char as applicable.

Instead of just printing out the details, you can then store each set of details into your array(s).

Alternatively, the first and last names might be read as a single item, though that would make the code slightly different. It all depends upon the requirements of your project.
Last edited on
There's a bit on the basics of arrays here.

http://www.cplusplus.com/doc/tutorial/arrays/


Registered users can post here. Sign in or register to post.