Dummy string used for getline?

I have access to a completed c++ program, but I don't understand one aspect of it. To me, it seems as if an extra getline is used[to extract the string dummy from file]. However, when I comment out that getline statement, my numbers are wrong. When I leave the statement in, and cout the value of dummy, nothing is returned. And I know the value of dummy is "\0", but why would I even need to manually extract that? I've seen this in a few other programs, but in others it's absent. Why must we getline twice?

Here's the program:

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

const double INTEREST = 0.075 / 12.0 + 1.0;

int main()
{
string id,
dummy;
int months;
double principle,
totalOldPrinciple=0,
totalNewPrinciple=0;
ifstream inFile;

inFile.open("pa6.dat");
if (!inFile)
{
cout << "No data file!" << endl;
exit(1);
}

getline(inFile, id);
inFile >> principle >> months;
while (inFile)
{
totalOldPrinciple += principle;
for (int m=0; m<months; m++)
principle *= INTEREST;
totalNewPrinciple += principle;

getline(inFile, dummy);
getline(inFile, id);
inFile >> principle >> months;
}

inFile.close();

cout << totalNewPrinciple - totalOldPrinciple << endl;

return 0;
}

Heres the file:

760E 60GC6
1743507.36 46
PXY WHK4 O
2958864.42 109
PEV4LE8J 7
1890624.22 61
R27U108IPR
2232449.51 37

P.S. What is the difference between while(inFile) and while(!inFile.eof()) ?

if(!inFile) and if(inFile.fail()) ?
Last edited on
Is anybody out there?

Please?
And I know the value of dummy is "\0", but why would I even need to manually extract that?
Because operator >> stops when it encountered \0

What is the difference between while(inFile) and while(!inFile.eof()) ?

As far as I know.. while(inFile) is equal to while(inFile.fail())


You don't get answers because your code is not readable in the forums..
Post your code in code tags [code] [/code] like this

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

const double INTEREST = 0.075 / 12.0 + 1.0;

int main()
{
    string id, dummy;
    int months;
    double principle=0, totalOldPrinciple=0, totalNewPrinciple=0;

    ifstream inFile;

    inFile.open("pa6.dat");
    if (!inFile)
    {
        cout << "No data file!" << endl;
        exit(1);
    }

    getline(inFile, id);
    inFile >> principle >> months;

    while (inFile)
    {
        totalOldPrinciple += principle;
        for (int m=0; m<months; m++)
            principle *= INTEREST;
        totalNewPrinciple += principle;

        getline(inFile, dummy);
        getline(inFile, id);
        inFile >> principle >> months;
    }

    inFile.close();

    cout << totalNewPrinciple - totalOldPrinciple << endl;
    return 0;
}
Thanks a bunch!
but . . .for clarification:
while(inFile) == while(inFile.eof()) ? I thought it was while(!inFile.eof())
while(inFile) == while(inFile.eof()) ? I thought it was while(!inFile.eof())
No, they are not equal..

while(inFile) actually calls operator bool() which returns a value that is returned by fail() member function
Topic archived. No new replies allowed.