Code to get user input not executing/skipping in C++

前端 未结 2 1585
刺人心
刺人心 2021-01-26 04:21

In the below code, I\'m running into an error when I try to get the user to input their name. My program just skips it over and goes right over to making the function calls with

2条回答
  •  再見小時候
    2021-01-26 04:33

    from looking at code showMenu function has problem. and it's not returning asccii equivalent of '1' that is: 31 integer. try printing value returned by showmenu. you will get that

    UPDATE: It is because cin in delimited by ' '(whitespace) and getline by '\n' character, so when enter name and press enter cin in showmenu will consume whole string except '\n' from istream and that is read by getline. to see this when it ask for choice enter string like 1 myname (1 whitespace myname)and press ENTER will display name. now cin will read 1 in choice and myname in name by getline.

提交回复
热议问题