Full string not appearing
Oct 24, 2014 at 9:00pm UTC
I've a problem here that I can't find a solution for.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <conio.h>
#include <string>
#include <iomanip>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
double tallyCount1, tallyCount2, tallyCount3;
double sum = 0;
double TCount;
double TMark;
int count = 0;
string FName, MName, LName;
ifstream inFile;
inFile.open("Info1.txt" );
// inFile.open("Info2.txt");
if (!inFile.is_open())
{
cout << "File Error. " << "Program Ends!" << endl;
return 0;
}
cout << fixed << showpoint << setprecision(2);
inFile >> FName >> LName;
inFile >> tallyCount1 >> tallyCount2 >> tallyCount3;
TMark = (tallyCount1 + tallyCount2 + tallyCount3) / 3;
cout << "Survey Results: " << endl << endl;
cout << left << setw(14) << "Name" << "\t\t" << "Results" << endl;
while (inFile)
{
TCount = tallyCount1 + tallyCount2 + tallyCount3;
TMark = (tallyCount1 + tallyCount2 + tallyCount3) / 3;
sum = sum + TMark;
count++;
cout << left << setw(13) << FName << setw(4) << LName << right << setw(13) << TCount << endl;
inFile >> FName >> LName;
inFile >> tallyCount1 >> tallyCount2 >> tallyCount3;
}
cout << "\nPress any key to continue..." ;
_getch();
return 0;
}
The program needs to be able to read the middle names within the text file named Info2.txt
Simply adding the Mname or any related value where the other to name values doesn't do anything and only the 1st line appears on the output.
1 2 3 4 5
Don Jon 11 10 12
Jack Bo Todd 15 10 15
Jill Jo Jane 16 10 11
Bob Jack Chuck 13 15 13
Da Fu 11 10 12
That is the correct way it appears in the text file. Also I know it's commented out, but that just to keep the program from crashing.
current output
1 2 3 4
Survey Results:
Name Results
Don Jon 33.00
This is the Info1.txt file info by the way.
1 2 3 4 5 6
Ken A 20 20 17
Gus B 15 10 15
Bill C 15 15 12
Jara C 15 17 20
Lu E 10 15 10
Chow B 17 10 15
and the output
Survey Results:
1 2 3 4 5 6 7
Name Results
Ken A 57.00
Gus B 40.00
Bill C 42.00
Jara C 52.00
Lu E 35.00
Chow B 42.00
Which when ran with Info1 selected, it is the correct output I'm looking for.
I've tried getline and other solutions and they didn't work.
Oct 24, 2014 at 10:27pm UTC
The difference between fields is a HT character. The name field contains at least one name, but it is still a single field -- there are no tabs in it.
Unfortunately, your code requires there to be exactly two fields -- no more, no less.
Instead, try reading the entire field, then splitting it into first, middle, last names.
1 2 3 4 5 6 7 8 9 10 11 12
string FName, MName, LName;
{
string name;
getline( inFile, name, '\t' );
istringstream ss( name );
ss >> FName;
ss >> MName;
ss >> LName;
if (LName.empty()) swap( MName, LName );
}
// now you can read the remaining data using the ws-delimited extraction operator:
inFile >> tallyCount1 >> ...
Hope this helps.
Topic archived. No new replies allowed.