Recently, I began to learn C ++ from Schildt’s book, C ++ Basic Course, and was learning how to handle pointers and their interaction with character arrays. Whether the code in the book is outdated, or VS 2012 fails, but I have complete disagreement between gets () and any character arrays, and another big problem is the complete failure of gets_s () (which works with character arrays and is a valid function) to read the data into the index character array According to Shildt, the code is correct, the header files are all that is needed. What could be the problem? In general, the whole point of the problem is that gets () does not work at all and that gets_s () does not perform all the necessary functions. Below is the code from the book Schildt for carbon paper.

#include <iostream> #include <cstring> #include <cstdio> #include <stdio.h> using namespace std; int main(){ setlocale(LC_ALL,"Russian"); char s[80]; int i=0; char *p; do { p = s; cout << "Введите строчку: "; gets(p); while(*p) { cout << (int) *p++ << ' '; i++;} cout << '\n'; }while(strcmp(s,"end")); } 
  • one
    Always use fgets instead of gets. - avp pm
  • Why are you all learning the worst book? - VladD
  • @Fania What does it mean that gets not working? - Vlad from Moscow
  • @Fania By the way, I very much doubt that this is the code from Schildt’s book for a carbon copy. :) At least there are hardly two headings <stdio.h> and <cstdio> in the source code :) - Vlad from Moscow
  • @avp thank you for the advice :) - Fania

1 answer 1

Try the following similar program

 #include <iostream> #include <clocale> #include <cstring> #include <cstdio> int main() { std::setlocale( LC_ALL,"Russian" ); const size_t N = 80; char s[N]; do { std::cout << "Введите строчку: "; if ( !std::fgets( s, N, stdin ) ) break; char *p = s; while ( *p ) std::cout << static_cast<int>( *p++ ) << ' '; std::cout << std::endl; } while ( std::strncmp( s, "end", 3 ) != 0 ); } 

If you enter

 Привет, Fania end 

then the output of the program will be as follows

 Введите строчку: Привет, Fania -48 -97 -47 -128 -48 -72 -48 -78 -48 -75 -47 -126 44 32 70 97 110 105 97 10 Введите строчку: end 101 110 100 10 

The gets function is no longer supported by the C standard, since it is unsafe. As for the gets_s function, before including the <stdio.h> file, you must declare a constant

 #define __STDC_WANT_LIB_EXT1__ 1 

However, in C ++ this may not work.

Therefore, in the above program I replaced the gets_s call with the fgets call. The difference between the functions is that the latter also reads the newline character generated by pressing the Enter key. Therefore in the condition of the cycle I use the following comparison

 std::strncmp( s, "end", 3 ) != 0 

to exclude from comparing strings this newline character (as can be seen from the program output, its code is 10), which is present in the variable s .

Probably later in the book you will get acquainted with the input functions for strings available in C ++, such as get or getline , which should be used instead of C.

  • Thank you for the informative help :) - Fania