So what im trying to do is to allow user to input name, age, education.
If user ended adding data, by entering 's'
the program moves to sorting.
If user didnt enter 's'
the input loop continues, and user may add more people.
So at line: std::cin>>stop;
. I'm opening input stream to check if user entered 's'
. If they didn't - this input should be closed, and then new input should start at line: while(std::cin>>name>>age>>education)
- to add more people.
My question is how to close this std::cin>>stop;
input? If it's not closed, the first character of name will be put into char stop, and name will be lacking the first character.
std::cin.unget()
does the trick but its not exactly the purpose it should serve, because if the the type of stop is changed from char to std::string, the whole name will be put into stop variable.
int main()
{
int age;
std::string name, education;
std::vector<Data> zbior;
std::cout << "Enter: name, age, education: ex: Ala 20 primary" << std::endl;
while(std::cin>>name>>age>>education)
{
zbior.push_back(Data(name, age, education));
char stop;
std::cout << "Type s to end input or continue adding data" << std::endl;
std::cin>>stop;
if(stop == 's') break;
else std::cin.unget();
system("cls");
sortManual();
std::string sort;
while (std::cin>>sort) {
if(sort =="ar") { system("cls"); print_young_to_elder(zbior); std::cout << "
" << std::endl; sortManual();}
if(sort =="am") { system("cls"); print_elder_to_younger(zbior); std::cout << "
"; sortManual();}
if(sort =="er") { system("cls"); print_Edu_rising(zbior); std::cout << "
"; sortManual();}
if(sort =="em") { system("cls"); print_Edu_descending(zbior); std::cout << "
"; sortManual();}
}
}
See Question&Answers more detail:os