fstream program

This code won't open, and I don't know whats wrong with it.
#include <iostream>
#include <fstream>
#include < string>
#include <iomanip>

using namespace std;


int main()
{
string tName;
string aName;
string artist;


ifstream inputFile;
ofstream outputFile;

int minutes, seconds;
int minsum, secsum;
int tNum = 0;

//output file is tracklist.txt
// input file is albuminfo.txt
inputFile.open("albuminfo.txt");
if (inputFile.fail())
cout << "Error opening file. " << endl;

else
{
cout << "Welcome to Richard's tracklist generator !" << endl;
getline(inputFile, aName);
getline(inputFile, artist);


outputFile.open("tracklist.txt");
outputFile << "Albume title: " << aName << endl;
outputFile << "Artist Name: " << artist << endl;
outputFile << "Tracks " << endl;

cout << "--------------------" << endl;


while (getline(inputFile, tName)) {
inputFile >> minutes;
minsum += minutes;

inputFile >> seconds;
secsum += seconds;
inputFile.ignore();

tNum++;

outputFile << right << setw(2) << setfill('0') << tNum << "-";
outputFile << left << setw(40) << setfill(' ') << tNum;
outputFile << minutes << ":";
outputFile << right << setw(2) << setfill('0') << seconds << endl;
}
cout << "-------------" << endl;
cout << "Processed " << tNum << "tracks" << endl;
cout << "total album lenght: " << minsum + (secsum / 60) << ":";
cout << setw(2) << setfill('0') << secsum % 60 << endl;

inputFile.close();
outputFile.close();


}
return 0;
}
littlekid wrote:
This code won't open, and I don't know whats wrong with it.


Please try to be more explicit about the problem than "code won't open". Does it compile? Does it run and crash? Does it run and produce incorrect output?

Also, PLEASE USE CODE TAGS.


#include < string>
should be (with no space):
#include <string>
After that, it compiles.

What it does after that - who knows: you haven't told us and we haven't got the input file.

If it says "Error opening file" then you haven't got the input file either (at least, not in a place where your program can find it).

minsum and secsum haven't been initialised.

You have an unholy mix of >> and getline() for input which is also going to cause you a headache.
Last edited on
Topic archived. No new replies allowed.