c++ issue reading a file with getline() and getting output formatted properly











up vote
-3
down vote

favorite












Using the below code I am reading a .txt file and I can't get it to output properly. It either doesn't break the first input correctly (with all comma delimiters) or it doesn't break properly if I only use a comma delimiter after only the item name (2nd item in the text file).



Here the code:



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

int main()
{
ifstream inventoryFile;
string itemType[100], itemName[100], matCost[100], manHours[100];
// float matCost[100], manHours[100];
string temp;

inventoryFile.open("items2.txt");

cout << endl << endl;

if(!inventoryFile)
{
cout << "nnCan't read from file.n";
exit(1);
}
int numItems=0;

getline(inventoryFile, temp, ',');
while(!inventoryFile.eof())
{
itemType[numItems] = temp;
inventoryFile >> itemName[numItems];
inventoryFile.ignore();
inventoryFile >> matCost[numItems];
inventoryFile.ignore();
inventoryFile >> manHours[numItems];
inventoryFile.ignore();
numItems++;
getline(inventoryFile, temp, ',');
}

for(int j=0; j<numItems; j++)
{
cout << "Item Type:t" << itemType[j] << endl;
cout << "ITem Name:t" << itemName[j] << endl;
cout << "Mats Cost:t" << matCost[j] << endl;
cout << "Man Hours:t$" << manHours[j] << endl << endl;
}

return 0;
}


The items.txt file is:



1, Xiphos, 7.46, 2
2, Dao, 3.45, 2.7
3, Jian, 2.31, 0.5
1, Rapier, 8.32, 2.3
2, Hook Sword, 2.11, 0.75
1, Panzerstecher, 2.23, 1.25
2, Kopis, 14.89, 2.3
3, Longsword, 5.43, 0.5
1, Tuck, 2.5, 15
1, Small Sword, 7.5, 2
3, Broadsword, 0.5, 0.25


The items2.txt file is:



1 Xiphos, 7.46 2
2 Dao, 3.45 2.7
3 Jian, 2.31 0.5
1 Rapier, 8.32 2.3
2 Hook Sword, 2.11 0.75
1 Panzerstecher, 2.23 1.25
2 Kopis, 14.89 2.3
3 Longsword, 5.43 0.5
1 Tuck, 2.5 15
1 Small Sword, 7.5 2
3 Broadsword, 0.5 0.25









share|improve this question
























  • Related, see How does ifstream's eof() work? and Why is iostream::eof inside a loop condition considered wrong?
    – jww
    Nov 11 at 16:58












  • Please add an example of how you'd like it to look. Also, there's no reference to items.txt in the code. Remove it if it's not needed.
    – Ted Lyngmo
    Nov 11 at 16:59










  • If you want to continue to use line-based parsing, then you might consider while(getline(stream, line)), put line in a stringstream ss(line), and then follow with stream parsing with the insertion operator.
    – jww
    Nov 11 at 17:04












  • Possible duplicate of Why does std::getline() skip input after a formatted extraction?.
    – πάντα ῥεῖ
    Nov 11 at 17:07










  • Possible duplicate of Why does std::getline() skip input after a formatted extraction?
    – jww
    Nov 11 at 17:11















up vote
-3
down vote

favorite












Using the below code I am reading a .txt file and I can't get it to output properly. It either doesn't break the first input correctly (with all comma delimiters) or it doesn't break properly if I only use a comma delimiter after only the item name (2nd item in the text file).



Here the code:



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

int main()
{
ifstream inventoryFile;
string itemType[100], itemName[100], matCost[100], manHours[100];
// float matCost[100], manHours[100];
string temp;

inventoryFile.open("items2.txt");

cout << endl << endl;

if(!inventoryFile)
{
cout << "nnCan't read from file.n";
exit(1);
}
int numItems=0;

getline(inventoryFile, temp, ',');
while(!inventoryFile.eof())
{
itemType[numItems] = temp;
inventoryFile >> itemName[numItems];
inventoryFile.ignore();
inventoryFile >> matCost[numItems];
inventoryFile.ignore();
inventoryFile >> manHours[numItems];
inventoryFile.ignore();
numItems++;
getline(inventoryFile, temp, ',');
}

for(int j=0; j<numItems; j++)
{
cout << "Item Type:t" << itemType[j] << endl;
cout << "ITem Name:t" << itemName[j] << endl;
cout << "Mats Cost:t" << matCost[j] << endl;
cout << "Man Hours:t$" << manHours[j] << endl << endl;
}

return 0;
}


The items.txt file is:



1, Xiphos, 7.46, 2
2, Dao, 3.45, 2.7
3, Jian, 2.31, 0.5
1, Rapier, 8.32, 2.3
2, Hook Sword, 2.11, 0.75
1, Panzerstecher, 2.23, 1.25
2, Kopis, 14.89, 2.3
3, Longsword, 5.43, 0.5
1, Tuck, 2.5, 15
1, Small Sword, 7.5, 2
3, Broadsword, 0.5, 0.25


The items2.txt file is:



1 Xiphos, 7.46 2
2 Dao, 3.45 2.7
3 Jian, 2.31 0.5
1 Rapier, 8.32 2.3
2 Hook Sword, 2.11 0.75
1 Panzerstecher, 2.23 1.25
2 Kopis, 14.89 2.3
3 Longsword, 5.43 0.5
1 Tuck, 2.5 15
1 Small Sword, 7.5 2
3 Broadsword, 0.5 0.25









share|improve this question
























  • Related, see How does ifstream's eof() work? and Why is iostream::eof inside a loop condition considered wrong?
    – jww
    Nov 11 at 16:58












  • Please add an example of how you'd like it to look. Also, there's no reference to items.txt in the code. Remove it if it's not needed.
    – Ted Lyngmo
    Nov 11 at 16:59










  • If you want to continue to use line-based parsing, then you might consider while(getline(stream, line)), put line in a stringstream ss(line), and then follow with stream parsing with the insertion operator.
    – jww
    Nov 11 at 17:04












  • Possible duplicate of Why does std::getline() skip input after a formatted extraction?.
    – πάντα ῥεῖ
    Nov 11 at 17:07










  • Possible duplicate of Why does std::getline() skip input after a formatted extraction?
    – jww
    Nov 11 at 17:11













up vote
-3
down vote

favorite









up vote
-3
down vote

favorite











Using the below code I am reading a .txt file and I can't get it to output properly. It either doesn't break the first input correctly (with all comma delimiters) or it doesn't break properly if I only use a comma delimiter after only the item name (2nd item in the text file).



Here the code:



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

int main()
{
ifstream inventoryFile;
string itemType[100], itemName[100], matCost[100], manHours[100];
// float matCost[100], manHours[100];
string temp;

inventoryFile.open("items2.txt");

cout << endl << endl;

if(!inventoryFile)
{
cout << "nnCan't read from file.n";
exit(1);
}
int numItems=0;

getline(inventoryFile, temp, ',');
while(!inventoryFile.eof())
{
itemType[numItems] = temp;
inventoryFile >> itemName[numItems];
inventoryFile.ignore();
inventoryFile >> matCost[numItems];
inventoryFile.ignore();
inventoryFile >> manHours[numItems];
inventoryFile.ignore();
numItems++;
getline(inventoryFile, temp, ',');
}

for(int j=0; j<numItems; j++)
{
cout << "Item Type:t" << itemType[j] << endl;
cout << "ITem Name:t" << itemName[j] << endl;
cout << "Mats Cost:t" << matCost[j] << endl;
cout << "Man Hours:t$" << manHours[j] << endl << endl;
}

return 0;
}


The items.txt file is:



1, Xiphos, 7.46, 2
2, Dao, 3.45, 2.7
3, Jian, 2.31, 0.5
1, Rapier, 8.32, 2.3
2, Hook Sword, 2.11, 0.75
1, Panzerstecher, 2.23, 1.25
2, Kopis, 14.89, 2.3
3, Longsword, 5.43, 0.5
1, Tuck, 2.5, 15
1, Small Sword, 7.5, 2
3, Broadsword, 0.5, 0.25


The items2.txt file is:



1 Xiphos, 7.46 2
2 Dao, 3.45 2.7
3 Jian, 2.31 0.5
1 Rapier, 8.32 2.3
2 Hook Sword, 2.11 0.75
1 Panzerstecher, 2.23 1.25
2 Kopis, 14.89 2.3
3 Longsword, 5.43 0.5
1 Tuck, 2.5 15
1 Small Sword, 7.5 2
3 Broadsword, 0.5 0.25









share|improve this question















Using the below code I am reading a .txt file and I can't get it to output properly. It either doesn't break the first input correctly (with all comma delimiters) or it doesn't break properly if I only use a comma delimiter after only the item name (2nd item in the text file).



Here the code:



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

int main()
{
ifstream inventoryFile;
string itemType[100], itemName[100], matCost[100], manHours[100];
// float matCost[100], manHours[100];
string temp;

inventoryFile.open("items2.txt");

cout << endl << endl;

if(!inventoryFile)
{
cout << "nnCan't read from file.n";
exit(1);
}
int numItems=0;

getline(inventoryFile, temp, ',');
while(!inventoryFile.eof())
{
itemType[numItems] = temp;
inventoryFile >> itemName[numItems];
inventoryFile.ignore();
inventoryFile >> matCost[numItems];
inventoryFile.ignore();
inventoryFile >> manHours[numItems];
inventoryFile.ignore();
numItems++;
getline(inventoryFile, temp, ',');
}

for(int j=0; j<numItems; j++)
{
cout << "Item Type:t" << itemType[j] << endl;
cout << "ITem Name:t" << itemName[j] << endl;
cout << "Mats Cost:t" << matCost[j] << endl;
cout << "Man Hours:t$" << manHours[j] << endl << endl;
}

return 0;
}


The items.txt file is:



1, Xiphos, 7.46, 2
2, Dao, 3.45, 2.7
3, Jian, 2.31, 0.5
1, Rapier, 8.32, 2.3
2, Hook Sword, 2.11, 0.75
1, Panzerstecher, 2.23, 1.25
2, Kopis, 14.89, 2.3
3, Longsword, 5.43, 0.5
1, Tuck, 2.5, 15
1, Small Sword, 7.5, 2
3, Broadsword, 0.5, 0.25


The items2.txt file is:



1 Xiphos, 7.46 2
2 Dao, 3.45 2.7
3 Jian, 2.31 0.5
1 Rapier, 8.32 2.3
2 Hook Sword, 2.11 0.75
1 Panzerstecher, 2.23 1.25
2 Kopis, 14.89 2.3
3 Longsword, 5.43 0.5
1 Tuck, 2.5 15
1 Small Sword, 7.5 2
3 Broadsword, 0.5 0.25






c++ fstream getline






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 17:08









Christophe

38.7k43473




38.7k43473










asked Nov 11 at 16:49









RockRat

103




103












  • Related, see How does ifstream's eof() work? and Why is iostream::eof inside a loop condition considered wrong?
    – jww
    Nov 11 at 16:58












  • Please add an example of how you'd like it to look. Also, there's no reference to items.txt in the code. Remove it if it's not needed.
    – Ted Lyngmo
    Nov 11 at 16:59










  • If you want to continue to use line-based parsing, then you might consider while(getline(stream, line)), put line in a stringstream ss(line), and then follow with stream parsing with the insertion operator.
    – jww
    Nov 11 at 17:04












  • Possible duplicate of Why does std::getline() skip input after a formatted extraction?.
    – πάντα ῥεῖ
    Nov 11 at 17:07










  • Possible duplicate of Why does std::getline() skip input after a formatted extraction?
    – jww
    Nov 11 at 17:11


















  • Related, see How does ifstream's eof() work? and Why is iostream::eof inside a loop condition considered wrong?
    – jww
    Nov 11 at 16:58












  • Please add an example of how you'd like it to look. Also, there's no reference to items.txt in the code. Remove it if it's not needed.
    – Ted Lyngmo
    Nov 11 at 16:59










  • If you want to continue to use line-based parsing, then you might consider while(getline(stream, line)), put line in a stringstream ss(line), and then follow with stream parsing with the insertion operator.
    – jww
    Nov 11 at 17:04












  • Possible duplicate of Why does std::getline() skip input after a formatted extraction?.
    – πάντα ῥεῖ
    Nov 11 at 17:07










  • Possible duplicate of Why does std::getline() skip input after a formatted extraction?
    – jww
    Nov 11 at 17:11
















Related, see How does ifstream's eof() work? and Why is iostream::eof inside a loop condition considered wrong?
– jww
Nov 11 at 16:58






Related, see How does ifstream's eof() work? and Why is iostream::eof inside a loop condition considered wrong?
– jww
Nov 11 at 16:58














Please add an example of how you'd like it to look. Also, there's no reference to items.txt in the code. Remove it if it's not needed.
– Ted Lyngmo
Nov 11 at 16:59




Please add an example of how you'd like it to look. Also, there's no reference to items.txt in the code. Remove it if it's not needed.
– Ted Lyngmo
Nov 11 at 16:59












If you want to continue to use line-based parsing, then you might consider while(getline(stream, line)), put line in a stringstream ss(line), and then follow with stream parsing with the insertion operator.
– jww
Nov 11 at 17:04






If you want to continue to use line-based parsing, then you might consider while(getline(stream, line)), put line in a stringstream ss(line), and then follow with stream parsing with the insertion operator.
– jww
Nov 11 at 17:04














Possible duplicate of Why does std::getline() skip input after a formatted extraction?.
– πάντα ῥεῖ
Nov 11 at 17:07




Possible duplicate of Why does std::getline() skip input after a formatted extraction?.
– πάντα ῥεῖ
Nov 11 at 17:07












Possible duplicate of Why does std::getline() skip input after a formatted extraction?
– jww
Nov 11 at 17:11




Possible duplicate of Why does std::getline() skip input after a formatted extraction?
– jww
Nov 11 at 17:11












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










This should work for the format in items2.txt.



string itemType[100], itemName[100];
float matCost[100], manHours[100];
//...
//getline(inventoryFile, temp, ',');
// get itemType and the whitespace after it
while(inventoryFile >> itemType[numItems] >> std::ws)
{
std::getline(inventoryFile, itemName[numItems], ',');
inventoryFile >> matCost[numItems] >> manHours[numItems];
// don't count this entry if the stream is in a failed state
if(inventoryFile.fail()) break;
inventoryFile.ignore();
++numItems;
}





share|improve this answer





















  • Thanks so much for the help! This works perfectly.
    – RockRat
    Nov 12 at 14:32


















up vote
0
down vote













For items.txt this should work:



#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

struct Row {
int itemType;
string itemName;
float matCost;
float manHours;
};

istream& operator>>(istream& in, Row& row){
char comma;
char firstCharOfName;
in >> row.itemType >> comma >> firstCharOfName;
in.putback(firstCharOfName);
getline(in, row.itemName, ',');
return in >> row.matCost >> comma >> row.manHours;
}

int main() {
vector<Row> table;
{
ifstream inventoryFile("item.txt");
string line;
cout << "nn";

if (!inventoryFile) {
cerr << "nnCan't read from file.n";
return EXIT_FAILURE;
}
while (getline(inventoryFile, line)) {
// make room for next row
istringstream iss(line);
table.resize(table.size() + 1U);
Row &newRow = table.back();
iss >> newRow;
if (!iss) {
// skip row on error
table.resize(table.size() - 1U);
}
}
}

for (int j = 0; j < table.size(); j++) {
cout << "Item Type:t" << table[j].itemType << 'n'
<< "ITem Name:t" << table[j].itemName << 'n'
<< "Mats Cost:t$" << table[j].matCost << 'n'
<< "Man Hours:t" << table[j].manHours << "nn";
}

return EXIT_SUCCESS;
}


PS: Moved the $ to Mats Cost.






share|improve this answer





















  • Thanks so much for the help!
    – RockRat
    Nov 12 at 14:32











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250972%2fc-issue-reading-a-file-with-getline-and-getting-output-formatted-properly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










This should work for the format in items2.txt.



string itemType[100], itemName[100];
float matCost[100], manHours[100];
//...
//getline(inventoryFile, temp, ',');
// get itemType and the whitespace after it
while(inventoryFile >> itemType[numItems] >> std::ws)
{
std::getline(inventoryFile, itemName[numItems], ',');
inventoryFile >> matCost[numItems] >> manHours[numItems];
// don't count this entry if the stream is in a failed state
if(inventoryFile.fail()) break;
inventoryFile.ignore();
++numItems;
}





share|improve this answer





















  • Thanks so much for the help! This works perfectly.
    – RockRat
    Nov 12 at 14:32















up vote
0
down vote



accepted










This should work for the format in items2.txt.



string itemType[100], itemName[100];
float matCost[100], manHours[100];
//...
//getline(inventoryFile, temp, ',');
// get itemType and the whitespace after it
while(inventoryFile >> itemType[numItems] >> std::ws)
{
std::getline(inventoryFile, itemName[numItems], ',');
inventoryFile >> matCost[numItems] >> manHours[numItems];
// don't count this entry if the stream is in a failed state
if(inventoryFile.fail()) break;
inventoryFile.ignore();
++numItems;
}





share|improve this answer





















  • Thanks so much for the help! This works perfectly.
    – RockRat
    Nov 12 at 14:32













up vote
0
down vote



accepted







up vote
0
down vote



accepted






This should work for the format in items2.txt.



string itemType[100], itemName[100];
float matCost[100], manHours[100];
//...
//getline(inventoryFile, temp, ',');
// get itemType and the whitespace after it
while(inventoryFile >> itemType[numItems] >> std::ws)
{
std::getline(inventoryFile, itemName[numItems], ',');
inventoryFile >> matCost[numItems] >> manHours[numItems];
// don't count this entry if the stream is in a failed state
if(inventoryFile.fail()) break;
inventoryFile.ignore();
++numItems;
}





share|improve this answer












This should work for the format in items2.txt.



string itemType[100], itemName[100];
float matCost[100], manHours[100];
//...
//getline(inventoryFile, temp, ',');
// get itemType and the whitespace after it
while(inventoryFile >> itemType[numItems] >> std::ws)
{
std::getline(inventoryFile, itemName[numItems], ',');
inventoryFile >> matCost[numItems] >> manHours[numItems];
// don't count this entry if the stream is in a failed state
if(inventoryFile.fail()) break;
inventoryFile.ignore();
++numItems;
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 18:12









Ted Lyngmo

1,447314




1,447314












  • Thanks so much for the help! This works perfectly.
    – RockRat
    Nov 12 at 14:32


















  • Thanks so much for the help! This works perfectly.
    – RockRat
    Nov 12 at 14:32
















Thanks so much for the help! This works perfectly.
– RockRat
Nov 12 at 14:32




Thanks so much for the help! This works perfectly.
– RockRat
Nov 12 at 14:32












up vote
0
down vote













For items.txt this should work:



#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

struct Row {
int itemType;
string itemName;
float matCost;
float manHours;
};

istream& operator>>(istream& in, Row& row){
char comma;
char firstCharOfName;
in >> row.itemType >> comma >> firstCharOfName;
in.putback(firstCharOfName);
getline(in, row.itemName, ',');
return in >> row.matCost >> comma >> row.manHours;
}

int main() {
vector<Row> table;
{
ifstream inventoryFile("item.txt");
string line;
cout << "nn";

if (!inventoryFile) {
cerr << "nnCan't read from file.n";
return EXIT_FAILURE;
}
while (getline(inventoryFile, line)) {
// make room for next row
istringstream iss(line);
table.resize(table.size() + 1U);
Row &newRow = table.back();
iss >> newRow;
if (!iss) {
// skip row on error
table.resize(table.size() - 1U);
}
}
}

for (int j = 0; j < table.size(); j++) {
cout << "Item Type:t" << table[j].itemType << 'n'
<< "ITem Name:t" << table[j].itemName << 'n'
<< "Mats Cost:t$" << table[j].matCost << 'n'
<< "Man Hours:t" << table[j].manHours << "nn";
}

return EXIT_SUCCESS;
}


PS: Moved the $ to Mats Cost.






share|improve this answer





















  • Thanks so much for the help!
    – RockRat
    Nov 12 at 14:32















up vote
0
down vote













For items.txt this should work:



#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

struct Row {
int itemType;
string itemName;
float matCost;
float manHours;
};

istream& operator>>(istream& in, Row& row){
char comma;
char firstCharOfName;
in >> row.itemType >> comma >> firstCharOfName;
in.putback(firstCharOfName);
getline(in, row.itemName, ',');
return in >> row.matCost >> comma >> row.manHours;
}

int main() {
vector<Row> table;
{
ifstream inventoryFile("item.txt");
string line;
cout << "nn";

if (!inventoryFile) {
cerr << "nnCan't read from file.n";
return EXIT_FAILURE;
}
while (getline(inventoryFile, line)) {
// make room for next row
istringstream iss(line);
table.resize(table.size() + 1U);
Row &newRow = table.back();
iss >> newRow;
if (!iss) {
// skip row on error
table.resize(table.size() - 1U);
}
}
}

for (int j = 0; j < table.size(); j++) {
cout << "Item Type:t" << table[j].itemType << 'n'
<< "ITem Name:t" << table[j].itemName << 'n'
<< "Mats Cost:t$" << table[j].matCost << 'n'
<< "Man Hours:t" << table[j].manHours << "nn";
}

return EXIT_SUCCESS;
}


PS: Moved the $ to Mats Cost.






share|improve this answer





















  • Thanks so much for the help!
    – RockRat
    Nov 12 at 14:32













up vote
0
down vote










up vote
0
down vote









For items.txt this should work:



#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

struct Row {
int itemType;
string itemName;
float matCost;
float manHours;
};

istream& operator>>(istream& in, Row& row){
char comma;
char firstCharOfName;
in >> row.itemType >> comma >> firstCharOfName;
in.putback(firstCharOfName);
getline(in, row.itemName, ',');
return in >> row.matCost >> comma >> row.manHours;
}

int main() {
vector<Row> table;
{
ifstream inventoryFile("item.txt");
string line;
cout << "nn";

if (!inventoryFile) {
cerr << "nnCan't read from file.n";
return EXIT_FAILURE;
}
while (getline(inventoryFile, line)) {
// make room for next row
istringstream iss(line);
table.resize(table.size() + 1U);
Row &newRow = table.back();
iss >> newRow;
if (!iss) {
// skip row on error
table.resize(table.size() - 1U);
}
}
}

for (int j = 0; j < table.size(); j++) {
cout << "Item Type:t" << table[j].itemType << 'n'
<< "ITem Name:t" << table[j].itemName << 'n'
<< "Mats Cost:t$" << table[j].matCost << 'n'
<< "Man Hours:t" << table[j].manHours << "nn";
}

return EXIT_SUCCESS;
}


PS: Moved the $ to Mats Cost.






share|improve this answer












For items.txt this should work:



#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

struct Row {
int itemType;
string itemName;
float matCost;
float manHours;
};

istream& operator>>(istream& in, Row& row){
char comma;
char firstCharOfName;
in >> row.itemType >> comma >> firstCharOfName;
in.putback(firstCharOfName);
getline(in, row.itemName, ',');
return in >> row.matCost >> comma >> row.manHours;
}

int main() {
vector<Row> table;
{
ifstream inventoryFile("item.txt");
string line;
cout << "nn";

if (!inventoryFile) {
cerr << "nnCan't read from file.n";
return EXIT_FAILURE;
}
while (getline(inventoryFile, line)) {
// make room for next row
istringstream iss(line);
table.resize(table.size() + 1U);
Row &newRow = table.back();
iss >> newRow;
if (!iss) {
// skip row on error
table.resize(table.size() - 1U);
}
}
}

for (int j = 0; j < table.size(); j++) {
cout << "Item Type:t" << table[j].itemType << 'n'
<< "ITem Name:t" << table[j].itemName << 'n'
<< "Mats Cost:t$" << table[j].matCost << 'n'
<< "Man Hours:t" << table[j].manHours << "nn";
}

return EXIT_SUCCESS;
}


PS: Moved the $ to Mats Cost.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 19:58









Bo R

606110




606110












  • Thanks so much for the help!
    – RockRat
    Nov 12 at 14:32


















  • Thanks so much for the help!
    – RockRat
    Nov 12 at 14:32
















Thanks so much for the help!
– RockRat
Nov 12 at 14:32




Thanks so much for the help!
– RockRat
Nov 12 at 14:32


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250972%2fc-issue-reading-a-file-with-getline-and-getting-output-formatted-properly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation