Trying to read data from a file display it and the write it into a new txt file.

Been running at this problem all day, have a programming assignment due tomorrow night. The program asks for you to read data from a text file of orders of album, artist, and pricing display it and then get the total of cost and then print it into a summary txt file. I have the first line, badly formated, but it prints. When I try to implement anyway to get the second it gives me just the album title and then what I am guessing is the price in scientific notation. Here is my code as of now:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

int main()
{
ifstream inputFile;

string album_Name,
artist_Name;

double cost_Of_Album1,
cost_Of_Album2,
cost_Of_Album3,
cost_Of_Album4,
cost_Of_Album5,
total_Cost;

//Takes the values from file orders.txt to be brought onto the program.
inputFile.open("orders.txt");

cout << "Welcome to the Online Music store.\n";

cout << "You have submitted the following order: \n";

cout << setfill('*') << setw(70) << "*" << endl;

cout << setfill(' ');


cout << left << setw(5) << "Title" << right << setw(41) << "Artist" << setw(24) << "Cost" << endl;

getline(inputFile, album_Name);
getline(inputFile, artist_Name);
inputFile >> cost_Of_Album1;
cout << album_Name << setw(41) << artist_Name << setw(24) << cost_Of_Album1 << endl;

getline(inputFile, album_Name);
getline(inputFile, artist_Name);
inputFile >> cost_Of_Album2;
cout << album_Name << setw(41) << artist_Name << setw(24) << cost_Of_Album2 << endl;

getline(inputFile, album_Name);
getline(inputFile, artist_Name);
inputFile >> cost_Of_Album3;
cout << album_Name << setw(41) << artist_Name << setw(24) << cost_Of_Album3 << endl;

getline(inputFile, album_Name);
getline(inputFile, artist_Name);
inputFile >> cost_Of_Album4;
cout << album_Name << setw(41) << artist_Name << setw(24) << cost_Of_Album4 << endl;

getline(inputFile, album_Name);
getline(inputFile, artist_Name);
inputFile >> cost_Of_Album5;
cout << album_Name << setw(41) << artist_Name << setw(24) << cost_Of_Album5 << endl;

total_Cost = cost_Of_Album1 + cost_Of_Album2 + cost_Of_Album3 + cost_Of_Album4 + cost_Of_Album5;

cout << "Total" << setw(65) << total_Cost;


inputFile.close();



system("PAUSE");
return 0;
}

Right now it just displays the first line and then second album name with scientific notation number:

You have submitted the following order:
**********************************************************************
Title Artist Cost
Turn on the Bright Lights Interpol 9.49
House of Jealous Lovers -9.25596e+61
House of Jealous Lovers -9.25596e+61
House of Jealous Lovers -9.25596e+61
House of Jealous Lovers -9.25596e+61
Total -3.70239e+62


I'm just looking to get to the next line. Any help would be great.
check the input file. what is in it?
Turn on the Bright Lights
Interpol
9.49
House of Jealous Lovers
The Rapture
1.29
Fever to Tell
Yeah Yeah Yeahs
6.99
Desperate Youth, Blood Thirsty Babes
TV on the Radio
8.91
The Fragile
Nine Inch Nails
12.49

This is it for txt file.
That... is a... an interesting... file content?

Took me a minute to realize they were names of songs/albums.

Anyway, for yourself, look into how to use a while loop or a for loop, it will save you a lot of typing.

Loops and checking for file.good() will make it so you can deal with any size of file without knowing it's size before hand.


Your problem is that the fileInput >> variable; doesn't seem to go to the next line so your next getline is called on the same line as the price of the previous album. This throws off the rest of your blocks because it's then trying to read author information into a double which is not going to work.

Try inputFile.ignore(); at the top of each of your following album info blocks.

Edit:

Are you ok with the aspect of writing to a file using ofstream? From what you did with ifstream it looks like you're going to be comfortable with ofstream, it's pretty similar. http://www.cplusplus.com/reference/fstream/ofstream/ofstream/
Last edited on
Thanks for responding I'm just eating dinner now and going to try your suggestion. The ofstream seems like I can get that fine it was just this because of the outputs I was getting. I'll respond with my results.
Thanks so much that function fixed my whole output. I'm now just working on formatting, really thanks man I can not thank ya enough.
Glad to help, but in the future you might get quicker answers in the beginner's forums. (More eyes over there.)

After you post, you could also hit edit and wrap your code in code brackets (its the <> symbol in the format buttons). For some reason on this site they don't always work when writing a main post but work fine when editing or replying. Just highlight the code and hit that button.

Glad the fix worked,
good luck!
Last edited on
Topic archived. No new replies allowed.