Witam =, mam problem z podanym niżej kodem.. Ma on za zadanie z wczytanego zdania, gdzie słowa są rozdzielone srednikami wczytać po kolei dane do struktur ... Z tym że zamiast wczytać np. . "poland" wczytuje "pola", a zamiast "audi a3 1.9" wczytuje "audi a"... oraz jeszcze inny problem, ponieważ ostatnia dana jest prędkość i jest napisane np. "130 km/h" i to muszę odczytać tylko do pierwszej napotkanej " " spacji... i tu program się wysypuje.
#include<iostream>
#include<infstr.h>
#include<string>
using namespace std;
struct Car
{
int year;
string country;
string brand;
int speed;
};
Car Read_line(string line)
{
Car temp;
unsigned long pos = line.find(';');
temp.year = stoi(line.substr(0, pos));
pos = line.find(';');
line = line.substr(pos);
temp.country = line.substr(0, pos);
pos = line.find(';');
line = line.substr(pos + 1);
temp.brand = line.substr(0, pos);
pos = line.find(' ');
line = line.substr(pos + 1);
temp.speed = stoi(line.substr(0, pos));
return temp;
}
int main()
{
string car = "1997;Poland;Audi A3 1.9;193 km/h";
Car temp = Read_line(car);
cout << temp.year << endl << temp.country << endl << temp.brand << endl << temp.speed;
system("pause");
}