istream& operator>>(istream& s, vector<Order>& ord) {
string c{};
bool got_customer = false;
Customer temp_customer{};
vector<Purchase>vec_purchase{};
while (s >> c) {
cout << "Erstes Wort der Zeile: "<<c << endl;
if (!got_customer) { //c[0] == '#'
c.erase(c.begin()); //löscht erstes Element vom string
int k_nummer{};
string k_vname{};
string k_nname{};
string k_plz{};
string k_stadt{};
k_nummer = stoi(c); //string to int
s >> k_vname >> k_nname >> k_plz>>k_stadt;
k_vname += " " + k_nname;
temp_customer = Customer(k_nummer, k_vname, k_plz, k_stadt);
got_customer = true;
} else {
string p_name{};
double p_preis{};
int p_menge{};
p_name = c;
s >> p_preis>>p_menge;
cout << p_name << " " << p_preis << " " << p_menge << endl;
vec_purchase.push_back(Purchase{p_name, p_preis, p_menge});
}
cout<<s.peek()<<endl;
if (s.peek() == '#') {
ord.push_back(Order{temp_customer, vec_purchase});
temp_customer = Customer{};
vec_purchase.clear();
got_customer = false;
}
}
}
This is the txt file:
#725454 Schenker Rudolf DE-30159 Hannover
Shirt 135.95 1
Tie 89.59 1
#987654 Orbison Roy US-TN37075 Hendersonville
Mug 1.49 3
T-Shirt 14.90 1
#123456 Petty Tom US-FL32641 Gainesville
Flashlight 12.95 2
#246802 Hite Bob US-CA90291 Venice
CannedBeans 0.89 10
CannedTomatoes 1.79 6
#246802 Hite Bob US-CA90291 Venice
CanOpener 0.48 1
Spoon 1.49 4
Needle 0.05 100
The lines starting with '#' are Customer objects. The other lines are Purchase objects.
I'm trying to peek into the upcoming char but im getting the same char again and again. In this case i always get '13' as output of s.peek().
€dit: Thank you @Mark Ransom. Here is the working code if anybody has the same problem. :)
istream& operator>>(istream& s, vector<Order>& ord) {
string c{};
bool got_customer = false;
Customer temp_customer{};
vector<Purchase>vec_purchase{};
while (s >> c) {
if (!got_customer) {
c.erase(c.begin()); //deletes first char of string
int k_nummer{};
string k_vname{};
string k_nname{};
string k_plz{};
string k_stadt{};
k_nummer = stoi(c); //string to int
s >> k_vname >> k_nname >> k_plz>>k_stadt>>ws;
k_vname += " " + k_nname;
temp_customer = Customer(k_nummer, k_vname, k_plz, k_stadt);
got_customer = true;
} else {
string p_name{};
double p_preis{};
int p_menge{};
p_name = c;
s >> p_preis>>p_menge>>ws;
vec_purchase.push_back(Purchase{p_name, p_preis, p_menge});
}
if (s.peek() == 35||s.peek() == -1 ) { //35 (in ascii) -> # , -1 -> end of txt file
ord.push_back(Order{temp_customer, vec_purchase});
temp_customer = Customer{};
vec_purchase.clear();
got_customer = false;
}
}
}
When you read from a stream, the input will stop on the first whitespace character. The character with a decimal code of 13 is the ASCII CR, which comes at the end of every line in Windows; it is considered whitespace. You need to skip the whitespace to find the next significant character. You do this with
std::ws
.