+ 3

How is my program not outputting the requirements?

I'm suppose to write a program that takes a integer of the number of people and then their names. The program is supposed to print out a space separated list of initial. My output looks the exact same as the test cases but it still fails. Test case 1: Input: 4 Andy Smith Jamie Anderson Walt Peterson Trent Rogers Expected output: AS JA WP TR My output: AS JA WP TR https://code.sololearn.com/cet95xjX22M5/?ref=app

17th Nov 2022, 1:45 PM
Malachite
Malachite - avatar
18 Respostas
+ 2
Try by adding getchar() after cin cin >> numPeeps; getchar(); // consume line feed Loop till i < numPeeps ; only not <=
17th Nov 2022, 2:38 PM
Jayakrishna 🇮🇳
+ 2
Malachite Is that getchar() solution not working?
17th Nov 2022, 4:32 PM
Jayakrishna 🇮🇳
+ 2
Malachite By cin , the input pointer will not go into next line. So getline() at first reads empty new line character only because next input is in next line. getchar() comsume line feed. And input pointer goes to next line. So readline() reads first string input line at first time then. i < numPeems is enough then. Hope it helps... You're welcome..
17th Nov 2022, 4:45 PM
Jayakrishna 🇮🇳
+ 1
Task: Given a list of first and last names, take the first letter from each name to create initials and output them as a space-separated string. Input Format: The first input denotes the number of names in the list (N). The next N lines contain the list elements as strings. Output Format: A string containing the initials of each name in the original list, separated by spaces.
17th Nov 2022, 1:46 PM
Malachite
Malachite - avatar
+ 1
at the print try this cout << inti << '#'; i tried and the # symbol was a lot far away from the text. it means you print a lot white spaces at the end
17th Nov 2022, 4:27 PM
Giannis
Giannis - avatar
+ 1
Thank you anyway
17th Nov 2022, 4:32 PM
Malachite
Malachite - avatar
+ 1
Malachite i think i found the issue. you print 1 white space at the beginning not the end, try print a character before print the result and you will see it
17th Nov 2022, 4:35 PM
Giannis
Giannis - avatar
+ 1
Jayakrishna the getchar() worked. I'm not exactly sure how or why but I'm happy to have the fix so now i can figure that part out. Sorry I missed that at first. I was a bit distracted. Anyway thank you both for the help. 🙂
17th Nov 2022, 4:40 PM
Malachite
Malachite - avatar
+ 1
Oh ok. That's great to know. Thank you
17th Nov 2022, 4:48 PM
Malachite
Malachite - avatar
0
what's the issue exactly? It stuck or output wrong results? try change your stringstream to this, i had many times this problem. stringstream sso; sso << names;
17th Nov 2022, 4:15 PM
Giannis
Giannis - avatar
0
The sstream is working. It fills in properly. For whatever reason my output isn't matching the required output even though they look identical
17th Nov 2022, 4:16 PM
Malachite
Malachite - avatar
0
oh then probably you have a space at the very end or you need to have but you not have?
17th Nov 2022, 4:19 PM
Giannis
Giannis - avatar
0
Also if I loop til just i< numpeeps I won't have the last set of initials.
17th Nov 2022, 4:20 PM
Malachite
Malachite - avatar
0
no white space. In the code I erased the final spot just to prevent that. I also tried without and still no luck
17th Nov 2022, 4:21 PM
Malachite
Malachite - avatar
0
you have <=
17th Nov 2022, 4:22 PM
Giannis
Giannis - avatar
0
Yes I do. If it's erased it won't get the last initials. I've also tried different versions of that
17th Nov 2022, 4:23 PM
Malachite
Malachite - avatar
0
weird maybe sololearn get my input wrong, so i think I can't help sorry
17th Nov 2022, 4:31 PM
Giannis
Giannis - avatar
0
Hmm it just printed out right after my output when I tried
17th Nov 2022, 4:32 PM
Malachite
Malachite - avatar