简体   繁体   中英

ifstream not reading values from file in C++

I'm a novice user to C++ currently taking college courses in CS, and I'm stuck since my code does not read values from my input file, "OH-in.dat". Moreover, I don't quite know what to do when it comes to a sentinel value (since I require a do while loop, I think I'll just have to make the sentinel value be the first value I take and simply stop the loop from then on.)

Here's the code I have so far:

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

int main()
{
// Declare the variables to be used in the program.

ifstream infile;
string schoolname, location;
int tuition, enrollnum, i;
double average;

//Obtain the values for the variables from the file given.

infile.open("OH-in.dat");               //Accomplish task 1.
{
    i = 0;
    cout << "Schools in Cincinnati with tuition below $20,000." << endl;
    cout << "-----------------------------------------------------" << endl;
    do 
    {
        // Read the values in the data file.
        infile >> schoolname;
        infile >> location;
        infile >> enrollnum;
        infile >> tuition;

        // Separate the values that contain Cincinnati and if it's tuition is over 20000 dollars.

        if (schoolname == "Cincinnati" && tuition < 20000)
        {
            cout << schoolname << "                     $" << tuition << endl;
            i++;
        }
        // Display the  number of schools that fit the criteria.
        cout << "Number of schools:                     " << i << endl;
    }
    //While the 1st value read in is not ***, the loop will continue.
    while (schoolname != "***");
    //Close the file.
    infile.close();
}
system("pause");
return 0;
}

Here's the first four values I will be inputting.

AntiochCollege

YellowSprings

330

27800

...

Finally, it will come to this.

'* * *'

This data was collected from petersons.com

on October 10 and 11. Some name and location

data has been altered.


Now, the main two problems I've had with this code have been simply getting C++ to read in the values and secondly getting the code to not infinitely repeat itself.

You can use something like

if (infile.fail()) break;

immediately after the four "infile >>" statements. Or you can use .eof() rather than .fail() if you like. Either way, it will exit your loop when infile has no more data to give.

In your specific case, of course, you're looking for the sentinel rather than the file's end, but the same principle applies. This is an ideal case for C++'s break statement.

Incidentally, once you add the break, you probably won't want the do-while any longer. A while(true) or for(;;) -- that is, a loop whose condition always passes -- will probably serve. This loop's natural exit point is in the middle, at the break you will add to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM