Read from a text file with both variables and strings

Hello yall,

I am trying to do an assignment where I created an input file to be read then outputted with information read in to be sorted into three columns. Problem is the first two lines are word lines followed by the 3rd line being a double variable which goes on in the pattern for 15 lines. My problem with the coding is how do i get the getline to work after the variable because i have played with it to figure that part out but it doesnt want to work at all with reading the 4th line at all after the first variable is read. I am not asking for the assingment to be completed but a nudge in the right direction would be greatly appreciated. I have the code up to this:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
ifstream in;
ofstream out;

int number = 5;
string title, artist;
double cost1, totalCost;

cout << "You have Submitted the following order:" << endl;
cout << "*****************************************************************" << endl;
cout << "Title" << setw(30) << "Artist" << setw(25) << "Cost" << endl;
cout << fixed << setprecision(2);

in.open("orders.txt");

getline(in, title);
getline(in, artist);
in >> cost1;
cout << right << title << setw(22) << artist << setw(20) << cost1 << endl;

????

in.close();
When you use >> to read the double value, it doesn't remove the newline character on the end, so the next time you try to read with getline() it hits the newline immediately and gives you the empty string. You can ignore the rest of the line after the double value with something like:
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
which just ignores all characters up to and including the newline.
Topic archived. No new replies allowed.