0

HOW TO FIX DISPLAY/OUTPUT USING .TXT FILE

Using a .txt file contains: Miya, Balmond Huskar, IO Zed Yasuo, Gragas Cait, Miss Fortune ONLY the words after the delimiter should be displayed But sometimes if i type 2 words it will also display 2 words like: ERRORS: Input: Zed Yasuo, Gragas Output: Yasuo, Gragas IT SHOULD BE LIKE THIS Input: Zed Yasuo, Gragas Output Gragas OTHER EXAMPLE: Input: Miya Output: Balmond https://code.sololearn.com/cJH8COecU2m5/?ref=app

2nd Apr 2020, 3:17 PM
Kool
Kool - avatar
5 ответов
+ 3
That happens because you use std::cin to retrieve the word to search for. If you enter "Zed Yasuo", std::cin stops reading at the whitespace, so when "Zed" is actually found in a line, you print everything to the right of it, including "Yasuo". Using std::getline() should fix this. By the way, you could simplify things a lot by using std::find() both for looking if the line contains the search word, and for retrieving the position of the delimiter: https://en.cppreference.com/w/cpp/string/basic_string/find
2nd Apr 2020, 3:50 PM
Shadow
Shadow - avatar
+ 1
Yes, I didn't look in too deeply, but replacing cin >> search; with getline( cin, search ); should fix the issue. If it doesn't, I'll take another look.
2nd Apr 2020, 3:59 PM
Shadow
Shadow - avatar
0
Shadow Thank you
2nd Apr 2020, 3:52 PM
Kool
Kool - avatar
0
Shadow should i just replace Cin with getline?
2nd Apr 2020, 3:57 PM
Kool
Kool - avatar
0
Shadow Thank you it fixed the problem Godbless
2nd Apr 2020, 4:16 PM
Kool
Kool - avatar