Home c++ C++ - Gets_s () and signposts on the symbol array

C++ – Gets_s () and signposts on the symbol array

Author

Date

Category

Recently began to study C++ on the book of Shildt “C++ Basic Course” and studying pointers and their interaction with symbolic arrays, faced a wilderness. Whether the code in the book is outdated, whether VS 2012 fails, but I have complete disagreements between gets () and any character arrays and a big problem is a complete failure of gets_s () (which works with symbolic arrays and is a faithful function) read data into a pointer Symbolic array. By Shieldt, the code is correct, the header files are indicated all that you need. What could be the problem? In general, the whole essence of the problem is that GETS () does not work at all and that gets_s () does not fulfill all the necessary functions. Below code from Shildt’s book under the car.

# include & lt; iostream & gt;
#Include & lt; CString & GT;
#Include & lt; Cstdio & gt;
#Include & lt; stdio.h & gt;
Using Namespace STD;
INT MAIN () {
SETLOCALE (LC_ALL, "RUSSIAN");
Char s [80];
int i = 0;
char * p;
do {
  p = s;
  COUT & LT; & LT; "Enter the line:";
  gets (p);
  While (* p) {cout & lt; & lt; (int) * P ++ & lt; & lt; ''; I ++;}
  COUT & LT; & LT; '\ n';
} While (STRCMP (S, "END));
}

Answer 1, Authority 100%

Try the following similar program

# include & lt; iostream & gt;
#Include & lt; Clocale & gt;
#Include & lt; CString & GT;
#Include & lt; Cstdio & gt;
INT MAIN ()
{
  STD :: SETLOCALE (LC_ALL, "RUSSIAN");
  const size_t n = 80;
  char s [n];
  do.
  {
    STD :: COUT & LT; & LT; "Enter the line:";
    If (! STD :: FGETS (S, N, STDIN)) Break;
    char * p = s;
    while (* p) STD :: COUT & LT; & LT; static_cast & lt; int & gt; (* p ++) & lt; & lt; '';
    STD :: COUT & LT; & LT; STD :: ENDL;
  } While (STD :: STRNCMP (S, "END", 3)! = 0);
}

If you enter

hello, fania
End.

That the output of the program will look like this

Enter the line: Hi, Fania
-48 -97 -47 -128 -48 -72 -48 -78 -48 -75 -47 -126 44 32 70 97 110 105 97 10
Enter the line: End
101 110 100 10

The GETS function is no longer supported by the standard C, as it is unsafe. As for the function gets_s , before turning on the & lt file; stdio.h & gt; you need to declare a constant

# define __stdc_want_lib_ext1__ 1

Nevertheless, in C++ it may not work.

Therefore, in the above program, I replaced the gets_s_s_s call to FGETS . The difference between functions is that the latter also reads the symbol of a new string generated by pressing the Enter key. Therefore, in terms of the cycle, I use a comparison of the following type

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

To exclude from the row comparison, this symbol of a new line (as can be seen from the output of the program, its code is 10) present in the variable s .

Probably Next in the book you will get acquainted with input functions for rows available in the C++ language, such as get or GetLine , which should be used instead of the functions of C.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions