Reputation: 45
I wonder if there is any way to not ignore spaces when trying to assign a value to name.
I would like to keep this conditional structure* in the while loop but getting the ClientFile >> like getline and not as cin.
*I know I could use substring and find, that is not the idea.
Line example from text file:
1 ; Iron Man ; 10.70
Problem: The program does not enter the loop because name is being assigned as only Iron.
using namespace std;
int main()
{
ifstream ClientsFile("clients.txt");
int id;
string name;
double money;
char sep1;
char sep2;
while (ClientsFile >> id >> sep1 >> name >> sep2 >> money)
{
cout << "id: " << id << endl << "name: " << name << endl << "money: " << money << endl << endl;
}
return 0;
}
Thanks.
Upvotes: 2
Views: 1519
Reputation: 409136
The input operator >>
separates on white-space. Instead you might want to use std::getline
to read the semicolon separated fields.
Something like
std::string id_string, money_string;
while (std::getline(ClientsFile, id_string, ';') &&
std::getline(ClientsFile, name, ';') &&
std::getline(ClientsFile, money_string))
{
id = std::stoi(id_string);
money = std::stod(money_string);
...
}
Upvotes: 3