Click here to Skip to main content
15,918,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This was a homework assignment that has already been graded - I just want to know what I did wrong for my own benefit. Here was the comment I received back:

"There's a minor bug in fileAdder where the last value to be read is being added twice. It appears that this is an issue with text files in Linux/Unix ending with a newline. "

I use a Mac terminal.

#include <iostream>
#include <fstream>  //Needed to use files.
#include <string>
using namespace std;

int main()
    //This declares the input and output files.
    ifstream input;
    ofstream output;
    string filename;
    int value, total = 0;
    cout << "Enter the file name: " << endl;
    cin >> filename;
    //This function opens file.;
    //This checks to see if the file can be opened, 
    //if yes, then it will calculate sum.
            input >> value;      
            total += value;
    //This closes the input file.
    //This opens the output file."sum.txt");
    //This is where a file will open and the calculated sum will be shown.
       output << total;

    //This is if the file can not be opened.
       cout << "Could not access the file." << endl;
    //This closes the output file.

return 0;

Here is what the assignment asked for:

Write a program that prompts the user for the name of a file and then tries to open it. The open function needs a C-style string (or string literal) instead of a C++ string. Fortunately there is a function called c_str() that will give you the C-style string version of a C++ string. For example, instead of saying "", you can say "". If the input file is there and can be opened, the program should read the list of integers in the file, which will have one integer per line as in the following example:


Note: This example is just to demonstrate the format of the input file. Your program would not print these values out to the console or to the output file.

The program will then add together all the integers in the file, open an output file called sum.txt, and write the sum to that file (just that number - no additional text). Remember to close both the input and output files. If the input file is not there (or is there but couldn't be opened for some reason), the program should just print out "could not access file".

The file must be named: fileAdder.cpp

What I have tried:

I replace:
int value, total = 0;

with the following:

int value; // value is some positive number n
int total = 0; // total holds the sum of the first n positive numbers
int number; // the amount of numbers
Updated 7-Jul-16 2:38am

1 solution

So duplicate the problem - create a copy of your input file, and edit it until it matches your tutors description. Then use your app to prove the problem exists in your code, by feeding it the original and unmodified versions.
Then run your app in the debugger and feed it the file that shows the problem. Follow the code through, stepping over each line, and working out in advance exactly what you expect to happen. Did it do what you expected? If so, continue. If not, why not?
This is a process called debugging, and it's an important skill. Like all skills though, it has to be used to develop - and it's a lot easier to develop it on a small app like this, than it is on a 100,000 line monster!
So give it a try, and see what you can find out.

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900