1>------ Build started: Project: chapter17ex7, Configuration: Debug Win32 ------ 1>chapter17ex7.cpp 1>chapter17ex7.cpp(18,36): error : no matching function for call to 'getline' 1> for (char *str = new char[32737]; std::getline(std::cin, std::string(str), '\n') && *str != '!';) 1> ^~~~~~~~~~~~ 1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\include\string(144,33) : note: candidate function [with _Elem = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>] not viable: expects an l-value for 2nd argument 1> basic_istream<_Elem, _Traits>& getline( 1> ^ 1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\include\string(71,33) : note: candidate function [with _Elem = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>] not viable: no known conversion from 'istream' (aka 'basic_istream<char, char_traits<char> >') to 'basic_istream<char, std::char_traits<char> > &&' for 1st argument 1> basic_istream<_Elem, _Traits>& getline( 1> ^ 1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\include\string(124,33) : note: candidate function template not viable: requires 2 arguments, but 3 were provided 1> basic_istream<_Elem, _Traits>& getline( 1> ^ 1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\include\string(155,33) : note: candidate function template not viable: requires 2 arguments, but 3 were provided 1> basic_istream<_Elem, _Traits>& getline( 1> ^ 1>1 error generated. 1>Done building project "chapter17ex7.vcxproj" -- FAILED. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== |
|
|
str object only initializes the string. The string itself will maintain its own copy of the content of str. getline() would modify that temporary, which is discarded immediately after it terminates, leaving you with uninitialized content in str: it will be totally unpredictable whether or not *str has any specific value.| Do not use a std::string. |
std::cin.get(), and keep track of the current append index in str. Don’t forget to terminate the string with a null character when done getting input! |
|
Pls enter a character ( ! to end ): r Pls enter a character ( ! to end ): i Pls enter a character ( ! to end ): c Pls enter a character ( ! to end ): e Pls enter a character ( ! to end ): ! rice Program ended with exit code: 0 |
|
|
new fails.
| You can see in the instructions that it says to not worry about memory exhaustion/memory leak, right? |
This chapter does not say what happens when you run out of memory using new . That’s called memory exhaustion. |
delete[]. |
|
Please enter some characters; enter a '!' to stop Hello, world! Hello, world══════════════════════════════════════²²²²Pò"tT~k Please enter a character to exit k Press any key to continue . . . |
| So that's different from a memory leak? |
For Exercise 7, I'm getting some weird characters after the characters I'd entered are printed out. I'm guessing some weird characters are making it into the string, but I don't know how to fix this. |
std::string words; words.resize(words.capacity() + 1); |
| while (std::cin.get(ch) && ch != '!' && i < words.capacity()) |
|
|
|
|
|
|
Pls enter a character ( ! to end ): r Pls enter a character ( ! to end ): i Pls enter a character ( ! to end ): c Pls enter a character ( ! to end ): e Pls enter a character ( ! to end ): ! rice |
|
|
|
|
|
|
str[i] = ch; is done too soon, when the full capacity is reached, that will attempt to write beyond the allocated memory block. Also the while loop condition && i < capacity causes the loop to end instead of executing the code to resize the str buffer. You could remove that.
| I tried to do "words[i] = ch" before in that context, but I kept getting an "out_of_bounds" exception. |
|
|
|
|
|
|
| Edit: @Chervil: It still didn't work. What did I do wrong? |
capacity.strcpy() depends on the array having a null terminator, which can't be the case, because it isn't added until line 35. str[i] = ch; needs to be moved, to avoid writing outside the boundary of the buffer. Also I used memcpy() instead of strcpy(). |
|
| It still didn't work. |