简体   繁体   中英

c++ while infinite loop and confused about I/O stream

For my class we have to create a program that computes numeric grades for a course. The grades are in a file that will be the input file. Input file follows the following format: Each line contains a students last name, one space, followed by the students last name, then one space, then 10 quiz scores all on one line, each are whole numbers separated by one space. The program should read data from the inputfile and put it in the output file with the same format except there will be an additional number (of type double) at the end of each line. The number will be the average of the ten quizes.

At this point, I am able to get my program to write in the output file the data from the input, however its getting stuck in an infinite loop because my computer slows down and the file sizes are huge. Its also not placing the average on the last line either. Anyone have any advice? This chapter with I/O streams has been pretty confusing to me.

I have two sets of the score, the first one only creates one line and the rest is just random letters for infinity, however its not in a loop.

The second one goes into an infinite loop, but if you close it the original data is stored in the output file, but no average and the file size is huge. The formatting is also off for some reason. http://prntscr.com/8yl0u6

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <fstream>

using namespace std;


int main()
{
    ifstream infile;
    ofstream outfile;

    char Name;
    int Num_Space, scores;
    double total_scores;

    outfile.open("Output.dat");
    if (outfile.fail())
        exit(1);
    infile.open("scores.txt");
    if (infile.fail())
        exit(2);

    while (!infile.eof())
    {
        cout << "in first while loop";
        infile.get(Name);
        outfile.put(Name);
        Num_Space = 0;
        while (Num_Space < 2)
        {
            if (Name == ' ')
            {
                Num_Space++;
                outfile << Name;
            }

            infile.get(Name);
            outfile.put(Name);
        }
    }

    while (infile.eof())
    {
        total_scores = 0; 
        infile >> scores;
        total_scores += scores;
        outfile << scores;
        infile >> scores;
        total_scores += scores;
        outfile << scores;
        infile >> scores;
        total_scores += scores;
        outfile << scores;
        infile >> scores;
        total_scores += scores;
        outfile << scores;
        infile >> scores;
        total_scores += scores;
        outfile << scores;
        infile >> scores;
        total_scores += scores;
        outfile << scores;
        infile >> scores;
        total_scores += scores;
        outfile << scores;
        infile >> scores;
        total_scores += scores;
        outfile << scores;
        infile >> scores;
        total_scores += scores;
        outfile << scores;
        outfile << total_scores / 10;
    }

    outfile.close();
    infile.close();
    return 0;
}

In both loops, you are calling multiple times get without checking for eof between so you probably miss the fact that you're reaching the end of the file. Now as said in the comments, eof is not what you want to check anyway. I don't know the exact scope of your assignment but you may want to have a look at getline that would ease your life.

Then, your second loop is after having reached eof. How can you expect it to still be able to read something if eof was reached ? In your first loop, you're waiting for 2 spaces to start reading the next name, without reading grades in between. What you would need is to read the name, then all the grades and then come back on name.

Moreover, your second while loop is quite frightening: you duplicate the exact same code for each column. What if you want to add a column in your file ? You need to modify your code (and not just a number in it, you actually need to copy/paste some code) ! As it's an assignment, I won't give you the fixed code but it's easy to factorize :-) Note: you should do it without hard-coding the 10 a second time. If you want to add a column, you should at worse have to update only one constant.

By the way, unless I'm wrong, you're summing 9 grades and dividing by 10. Having a factorized version as I propose would prevent such a bug :-)

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