-5

i quiet am new in this community and greet you all. I just have a question about my coding

Here is the assignment;

"Problem Write a program to simulate managing test scores. Specification Randomly generate 100 "test scores" in the range 60-100 and save them in a text file. Read the text file and do the following: Count the number of test scores between 60-69, 70-79, 80-89, 90-100. Determine the high, low and average score of all test scores. Display summary results (number of scores in each range; high, low and average of all scores). Save the results in a separate text file in the same formatting they are displayed on the screen. "

This is what i have done so far. I accomplished creating the file with 100 numbers between 60 and 100 and saved it in a text file. However, when it comes to reading the file, it either does not read it or give me a blank console page. What should i do? also lets say if i did this part, for the part that says "Count the number of test scores between 60-69, 70-79, 80-89, 90-100" how would you create this structure? (for loop, if statement) Most of the videos only show either just to create a .txt file or read it. I haven't encountered with an example that combines both creating and reading it at the same time. (Note: I watched bunch of videos but it didn't solve my problem, you are my last hope)

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <string>
#include <fstream>

using namespace std;
int main ()   
{

int testnumber;
int count;

ofstream data;
data.open("data.txt"); 

int fromfile; //gets the integer from file.

ifstream readFile; // reading from file streamer;
readFile.open("data.txt"); //reads data.text




for (count = 1; count <= 100; count++)
      {
          testnumber =rand() % 40+60;
          cout << "test score " << testnumber << endl;                                  
          data << testnumber << endl;

}
        data.close();


while(!readFile.eof() ) 
        {
            getline(readFile,fromfile); 
            cout<<fromfile; // Prints our STRING.

        }
readFile.close();


system("pause");                              
}

4 Answers 4

0

Code works perfectly fine once you change int fromfile; //gets the integer from file. to std::string fromfile;. I do not understand how your compiler did not complain about it. As other answers say, it is best practice to not have file open multiple times but that is not issue in your case.

0

You are opening both the ofstreamand ifstream at the same time before the data has been written to the file. Open the ofstream, write the data to it, close it, then open the ifstream and read from it.

For the reading portion, all you have to do is convert fromLine into an int, such as with an istringstream, and then you can act on the value as needed.

0

It seems that the open operation for reading silently fails. To fix this, open the file for reading only after you have closed it for writing:

ofstream data;
data.open("data.txt"); 

ifstream readFile; // reading from file streamer; not open yet

...

data.close();

readFile.open("data.txt"); // open the file that we have just created

You can avoid this issue automatically if you separate unrelated parts of your program. In this case, it might be good to have one function for reading and another for writing:

void write_stuff()
{
    ofstream data("data.txt"); // one line of code is sufficient to open a file

    ...

    // No need to close - file is closed automatically when exiting a function
}

void read_stuff()
{
    ifstream readFile("data.txt"); //reads data.text

    ...

    // No need to close either
}

int main()
{
    write_stuff();
    read_stuff();
}

As you can see, a side-effect of having 2 functions is that you don't need to think about the open/close order; the "system" does that "automatically".

0

See here:

for (count = 1; count <= 100; count++)
{
    ...
}

data.close();                                                               /*
^^^^^^^^^^^^^                                                               */


while (!readFile.eof()) 
{
    getline(readFile, fromfile);                                          /*
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                          */
}

You closed the file but then wrote to it later on before opening it again. The thing is, you shouldn't have closed it in the first place - std::ofstream will automatically close the file upon destruction.

Moreover, while (!file.eof()) is considered bad practice. It's recommended that you perform the I/O operation in the while() expression and let valid file conditions be accessed thereafter:

while (std::getline(readFile, fromFile))

Another thing - fromFile is an integer, but getline retrieves the entire line. Its second argument is meant to be a string. You should be using the formatted input operator operator >> for the extraction here:

while (readFile >> fromFile)

Here is your new and improved program:

#include <iostream>
#include <fstream>

// no more using namespace std please

int main()   
{
    int testnumber;
    int count;

    std::fstream data("data.txt"); // use `std::fstream` for both input
                                   // and output

    int fromfile;

    for (count = 1; count <= 100; ++count)
    {
        testnumber = rand() % 40 + 60;
        std::cout << "test score " << testnumber << std::endl;
        data << testnumber << std::endl;
    }

    while (readFile >> fromFile) 
    {
        std::cout << fromfile;
    }

    std::cin.get(); // don't use `system("pause")
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.