My program is not reading in all of the .txt file
up vote
0
down vote
favorite
I am trying to read a .txt file into an object and then store it in a linked list but it only will read in half of the file.
This is what I'm calling to try and read it in but it only reads up to Dodge Demon.
while (CarsFile >> make >> model >> price >> year >> horsePower >> torque >> zeroToSixty >> weight >> quarterMile)
{
car.setMake(make);
car.setModel(model);
car.setPrice(price);
car.setYear(year);
car.setHorsePower(horsePower);
car.setTorque(torque);
car.setZeroToSixty(zeroToSixty);
car.setWeight(weight);
car.setQuarterMile(quarterMile);
list.appendNode(car);
}
c++
add a comment |
up vote
0
down vote
favorite
I am trying to read a .txt file into an object and then store it in a linked list but it only will read in half of the file.
This is what I'm calling to try and read it in but it only reads up to Dodge Demon.
while (CarsFile >> make >> model >> price >> year >> horsePower >> torque >> zeroToSixty >> weight >> quarterMile)
{
car.setMake(make);
car.setModel(model);
car.setPrice(price);
car.setYear(year);
car.setHorsePower(horsePower);
car.setTorque(torque);
car.setZeroToSixty(zeroToSixty);
car.setWeight(weight);
car.setQuarterMile(quarterMile);
list.appendNode(car);
}
c++
I misread the file. No spaces in names, but commas in the numbers are a problem.
– Johnny Mopp
Nov 7 at 21:53
I changed all the numbers with commas to ints but I'm still having problems
– Jakekone23
Nov 7 at 22:28
Here's a working demo parsing the first 4 lines. (In the future, post the file as text not image so we can test). If you still have issues, try to create a Minimal, Complete, and Verifiable example.
– Johnny Mopp
Nov 7 at 22:30
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to read a .txt file into an object and then store it in a linked list but it only will read in half of the file.
This is what I'm calling to try and read it in but it only reads up to Dodge Demon.
while (CarsFile >> make >> model >> price >> year >> horsePower >> torque >> zeroToSixty >> weight >> quarterMile)
{
car.setMake(make);
car.setModel(model);
car.setPrice(price);
car.setYear(year);
car.setHorsePower(horsePower);
car.setTorque(torque);
car.setZeroToSixty(zeroToSixty);
car.setWeight(weight);
car.setQuarterMile(quarterMile);
list.appendNode(car);
}
c++
I am trying to read a .txt file into an object and then store it in a linked list but it only will read in half of the file.
This is what I'm calling to try and read it in but it only reads up to Dodge Demon.
while (CarsFile >> make >> model >> price >> year >> horsePower >> torque >> zeroToSixty >> weight >> quarterMile)
{
car.setMake(make);
car.setModel(model);
car.setPrice(price);
car.setYear(year);
car.setHorsePower(horsePower);
car.setTorque(torque);
car.setZeroToSixty(zeroToSixty);
car.setWeight(weight);
car.setQuarterMile(quarterMile);
list.appendNode(car);
}
c++
c++
edited Nov 7 at 21:54
Johnny Mopp
6,71222344
6,71222344
asked Nov 7 at 21:52
Jakekone23
11
11
I misread the file. No spaces in names, but commas in the numbers are a problem.
– Johnny Mopp
Nov 7 at 21:53
I changed all the numbers with commas to ints but I'm still having problems
– Jakekone23
Nov 7 at 22:28
Here's a working demo parsing the first 4 lines. (In the future, post the file as text not image so we can test). If you still have issues, try to create a Minimal, Complete, and Verifiable example.
– Johnny Mopp
Nov 7 at 22:30
add a comment |
I misread the file. No spaces in names, but commas in the numbers are a problem.
– Johnny Mopp
Nov 7 at 21:53
I changed all the numbers with commas to ints but I'm still having problems
– Jakekone23
Nov 7 at 22:28
Here's a working demo parsing the first 4 lines. (In the future, post the file as text not image so we can test). If you still have issues, try to create a Minimal, Complete, and Verifiable example.
– Johnny Mopp
Nov 7 at 22:30
I misread the file. No spaces in names, but commas in the numbers are a problem.
– Johnny Mopp
Nov 7 at 21:53
I misread the file. No spaces in names, but commas in the numbers are a problem.
– Johnny Mopp
Nov 7 at 21:53
I changed all the numbers with commas to ints but I'm still having problems
– Jakekone23
Nov 7 at 22:28
I changed all the numbers with commas to ints but I'm still having problems
– Jakekone23
Nov 7 at 22:28
Here's a working demo parsing the first 4 lines. (In the future, post the file as text not image so we can test). If you still have issues, try to create a Minimal, Complete, and Verifiable example.
– Johnny Mopp
Nov 7 at 22:30
Here's a working demo parsing the first 4 lines. (In the future, post the file as text not image so we can test). If you still have issues, try to create a Minimal, Complete, and Verifiable example.
– Johnny Mopp
Nov 7 at 22:30
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
The commas in the numbers will make it unable to parse. You could read the file line-by-line, filter out the commas, and use std::stringstream
to parse.
std::string line;
while (std::getline(CarsFile, line)) {
// Remove commas
line.erase(std::remove(line.begin(), line.end(), ','), line.end());
// Use stringstream to parse
std::stringstream ss(line);
if (ss >> make >> model >> price >> year >> horsePower >> torque >> zeroToSixty >> weight >> quarterMile) {
// Add to list....
}
}
I believe that the first comma is used to separate the make from the remaining numbers. By removing the commas, the make becomes more difficult to parse, especially when the make contains spaces.
– Thomas Matthews
Nov 7 at 22:11
@ThomasMatthews I don't see that. I only see commas in the integers. Ex: line 1, "288,845" should be "288845".
– Johnny Mopp
Nov 7 at 22:13
For example "Ferrari LaFarrari 1,". In your method the 1 would be considered as the price. I don't see a global method for all columns; each column or field should be extracted separately.
– Thomas Matthews
Nov 7 at 22:16
@ThomasMatthews Possibly, but I see that as make: Ferrari, model: LaFerrari, price: 1,420,112
– Johnny Mopp
Nov 7 at 22:17
add a comment |
up vote
0
down vote
You may want to use std::getline
to read the text line, then std::istringstream
to parse the text:
std::string record;
while (std::getline(CarsFile, record))
{
std::istringstream car_stream(record);
std::string make;
std::getline(car_stream, make, ',');
//...
}
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
The commas in the numbers will make it unable to parse. You could read the file line-by-line, filter out the commas, and use std::stringstream
to parse.
std::string line;
while (std::getline(CarsFile, line)) {
// Remove commas
line.erase(std::remove(line.begin(), line.end(), ','), line.end());
// Use stringstream to parse
std::stringstream ss(line);
if (ss >> make >> model >> price >> year >> horsePower >> torque >> zeroToSixty >> weight >> quarterMile) {
// Add to list....
}
}
I believe that the first comma is used to separate the make from the remaining numbers. By removing the commas, the make becomes more difficult to parse, especially when the make contains spaces.
– Thomas Matthews
Nov 7 at 22:11
@ThomasMatthews I don't see that. I only see commas in the integers. Ex: line 1, "288,845" should be "288845".
– Johnny Mopp
Nov 7 at 22:13
For example "Ferrari LaFarrari 1,". In your method the 1 would be considered as the price. I don't see a global method for all columns; each column or field should be extracted separately.
– Thomas Matthews
Nov 7 at 22:16
@ThomasMatthews Possibly, but I see that as make: Ferrari, model: LaFerrari, price: 1,420,112
– Johnny Mopp
Nov 7 at 22:17
add a comment |
up vote
0
down vote
The commas in the numbers will make it unable to parse. You could read the file line-by-line, filter out the commas, and use std::stringstream
to parse.
std::string line;
while (std::getline(CarsFile, line)) {
// Remove commas
line.erase(std::remove(line.begin(), line.end(), ','), line.end());
// Use stringstream to parse
std::stringstream ss(line);
if (ss >> make >> model >> price >> year >> horsePower >> torque >> zeroToSixty >> weight >> quarterMile) {
// Add to list....
}
}
I believe that the first comma is used to separate the make from the remaining numbers. By removing the commas, the make becomes more difficult to parse, especially when the make contains spaces.
– Thomas Matthews
Nov 7 at 22:11
@ThomasMatthews I don't see that. I only see commas in the integers. Ex: line 1, "288,845" should be "288845".
– Johnny Mopp
Nov 7 at 22:13
For example "Ferrari LaFarrari 1,". In your method the 1 would be considered as the price. I don't see a global method for all columns; each column or field should be extracted separately.
– Thomas Matthews
Nov 7 at 22:16
@ThomasMatthews Possibly, but I see that as make: Ferrari, model: LaFerrari, price: 1,420,112
– Johnny Mopp
Nov 7 at 22:17
add a comment |
up vote
0
down vote
up vote
0
down vote
The commas in the numbers will make it unable to parse. You could read the file line-by-line, filter out the commas, and use std::stringstream
to parse.
std::string line;
while (std::getline(CarsFile, line)) {
// Remove commas
line.erase(std::remove(line.begin(), line.end(), ','), line.end());
// Use stringstream to parse
std::stringstream ss(line);
if (ss >> make >> model >> price >> year >> horsePower >> torque >> zeroToSixty >> weight >> quarterMile) {
// Add to list....
}
}
The commas in the numbers will make it unable to parse. You could read the file line-by-line, filter out the commas, and use std::stringstream
to parse.
std::string line;
while (std::getline(CarsFile, line)) {
// Remove commas
line.erase(std::remove(line.begin(), line.end(), ','), line.end());
// Use stringstream to parse
std::stringstream ss(line);
if (ss >> make >> model >> price >> year >> horsePower >> torque >> zeroToSixty >> weight >> quarterMile) {
// Add to list....
}
}
answered Nov 7 at 22:10
Johnny Mopp
6,71222344
6,71222344
I believe that the first comma is used to separate the make from the remaining numbers. By removing the commas, the make becomes more difficult to parse, especially when the make contains spaces.
– Thomas Matthews
Nov 7 at 22:11
@ThomasMatthews I don't see that. I only see commas in the integers. Ex: line 1, "288,845" should be "288845".
– Johnny Mopp
Nov 7 at 22:13
For example "Ferrari LaFarrari 1,". In your method the 1 would be considered as the price. I don't see a global method for all columns; each column or field should be extracted separately.
– Thomas Matthews
Nov 7 at 22:16
@ThomasMatthews Possibly, but I see that as make: Ferrari, model: LaFerrari, price: 1,420,112
– Johnny Mopp
Nov 7 at 22:17
add a comment |
I believe that the first comma is used to separate the make from the remaining numbers. By removing the commas, the make becomes more difficult to parse, especially when the make contains spaces.
– Thomas Matthews
Nov 7 at 22:11
@ThomasMatthews I don't see that. I only see commas in the integers. Ex: line 1, "288,845" should be "288845".
– Johnny Mopp
Nov 7 at 22:13
For example "Ferrari LaFarrari 1,". In your method the 1 would be considered as the price. I don't see a global method for all columns; each column or field should be extracted separately.
– Thomas Matthews
Nov 7 at 22:16
@ThomasMatthews Possibly, but I see that as make: Ferrari, model: LaFerrari, price: 1,420,112
– Johnny Mopp
Nov 7 at 22:17
I believe that the first comma is used to separate the make from the remaining numbers. By removing the commas, the make becomes more difficult to parse, especially when the make contains spaces.
– Thomas Matthews
Nov 7 at 22:11
I believe that the first comma is used to separate the make from the remaining numbers. By removing the commas, the make becomes more difficult to parse, especially when the make contains spaces.
– Thomas Matthews
Nov 7 at 22:11
@ThomasMatthews I don't see that. I only see commas in the integers. Ex: line 1, "288,845" should be "288845".
– Johnny Mopp
Nov 7 at 22:13
@ThomasMatthews I don't see that. I only see commas in the integers. Ex: line 1, "288,845" should be "288845".
– Johnny Mopp
Nov 7 at 22:13
For example "Ferrari LaFarrari 1,". In your method the 1 would be considered as the price. I don't see a global method for all columns; each column or field should be extracted separately.
– Thomas Matthews
Nov 7 at 22:16
For example "Ferrari LaFarrari 1,". In your method the 1 would be considered as the price. I don't see a global method for all columns; each column or field should be extracted separately.
– Thomas Matthews
Nov 7 at 22:16
@ThomasMatthews Possibly, but I see that as make: Ferrari, model: LaFerrari, price: 1,420,112
– Johnny Mopp
Nov 7 at 22:17
@ThomasMatthews Possibly, but I see that as make: Ferrari, model: LaFerrari, price: 1,420,112
– Johnny Mopp
Nov 7 at 22:17
add a comment |
up vote
0
down vote
You may want to use std::getline
to read the text line, then std::istringstream
to parse the text:
std::string record;
while (std::getline(CarsFile, record))
{
std::istringstream car_stream(record);
std::string make;
std::getline(car_stream, make, ',');
//...
}
add a comment |
up vote
0
down vote
You may want to use std::getline
to read the text line, then std::istringstream
to parse the text:
std::string record;
while (std::getline(CarsFile, record))
{
std::istringstream car_stream(record);
std::string make;
std::getline(car_stream, make, ',');
//...
}
add a comment |
up vote
0
down vote
up vote
0
down vote
You may want to use std::getline
to read the text line, then std::istringstream
to parse the text:
std::string record;
while (std::getline(CarsFile, record))
{
std::istringstream car_stream(record);
std::string make;
std::getline(car_stream, make, ',');
//...
}
You may want to use std::getline
to read the text line, then std::istringstream
to parse the text:
std::string record;
while (std::getline(CarsFile, record))
{
std::istringstream car_stream(record);
std::string make;
std::getline(car_stream, make, ',');
//...
}
answered Nov 7 at 22:10
Thomas Matthews
43.9k1168120
43.9k1168120
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53198396%2fmy-program-is-not-reading-in-all-of-the-txt-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
I misread the file. No spaces in names, but commas in the numbers are a problem.
– Johnny Mopp
Nov 7 at 21:53
I changed all the numbers with commas to ints but I'm still having problems
– Jakekone23
Nov 7 at 22:28
Here's a working demo parsing the first 4 lines. (In the future, post the file as text not image so we can test). If you still have issues, try to create a Minimal, Complete, and Verifiable example.
– Johnny Mopp
Nov 7 at 22:30