I'm supposed to read a text file line by line an extract three variables:
1- int index -> index of list
2- string name -> name of a person
3- string gift -> Christmas gift
An example:
1 Robert XBox360 Gaming Console
2 Harold Dress Shirt
So for the first line, index = 1 , name = Robert , gift = XBox360 Gaming Console
This is what I have been doing:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
int main() {
std::ifstream infile("giftlist.txt");
std::string line;
while (std::getline(infile, line))
{
std::istringstream iss(line);
int index;
std:: string name, gift;
if (!(iss >> index >> name >> gift)) { break; } // error
std::cout << index << " "<< name << " " << gift << std::endl;
}
return 0;
}
Output:
1 Robert XBox360
So it does not read the rest of the gift line, it stops at white space (i.e gift = XBox360
). How can I fix this to read the rest of the gift name? Thanks
Aucun commentaire:
Enregistrer un commentaire