Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
165 views
Welcome To Ask or Share your Answers For Others

1 Answer

Please use string not char to read one of the words (like name) and compare it to your "s" and if equal take approprioate action.

I would not reverse input streams or things like that in that simple program as it like adds a layer of complication when it is not needed..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...