0

I'm trying to learn C++. Sometimes I get confused by C style strings and its functions. I've been using

char var[1];
fflush(stdin);
gets(var);

to write a string into a char array. I don't know if thats the most efficient way but thats how I've been taught.

Now, I'm making a console program in which I read some variables that way and make things with them. It's all working fine but I have a char array, estudios[1] and I have to compare it with strcmp (I'm not talking about the strcmp(estudios, "N") != 0 I wrote below) to a specific value and i found that te result of the comparation was always the same no matter the value estudios had. I realized that after this chunk of code:

if (strcmp(estudios, "N") != 0){
    cout << "Estudios completos o incompletos?" << endl;
    fflush(stdin);
    gets(indicador);
}

Let's say that the value of estudios is "P". Before the code i showed the value of estudios is "P" but after it it changes it value to "". To be more precise it changes after the gets(indicator); Why does that happen? Is it supposed to do that? Sorry for such a newbie question

GZanotto
  • 31
  • 3
  • 2
    `char var[1];` can store strings with at most 0 characters. Do not use `gets()`, which has unavoidable risk of buffer overrun, deprecated in C99 and removed from C11. You should use [`std::getline()`](https://en.cppreference.com/w/cpp/string/basic_string/getline) and [`std::string`](https://en.cppreference.com/w/cpp/string/basic_string) when working with C++. – MikeCAT Jun 25 '21 at 23:16
  • 1
    Also, never use `fflush(stdin);`. It may appear to work on some systems but it is undefined behaviour. The `fflush` function is designed for *output* streams. – Adrian Mole Jun 25 '21 at 23:16
  • 1
    "thats how I've been taught." Taught by whom? Fire the bad teacher! – MikeCAT Jun 25 '21 at 23:17
  • 1
    [The man page for `gets`](https://man7.org/linux/man-pages/man3/gets.3.html) literally says "Never use gets()." If the documentation telling you how to use something says never use it and has said that for the past couple decades you really have to wonder when the professor was taught and how carefully they've been keeping up. – user4581301 Jun 25 '21 at 23:23
  • haha now i dont know what to do, thats what my professor wants us to use :/ i guess he does not care at all because he also told us to use getch() and ive read that it shouldn't be used neither. – GZanotto Jun 25 '21 at 23:56
  • *Sometimes I get confused by C style strings and its functions.* Don't use them. Use the C++ alternatives instead. – Eljay Jun 25 '21 at 23:57
  • what alternatives do you mean? string variables? my teacher told us exactly not to use it because it causes memory problems, should i use them anyways? – GZanotto Jun 26 '21 at 00:03
  • Do what you have to do to pass the class, but see if you can at least substitute [`fgets`](https://man7.org/linux/man-pages/man3/fgetc.3.html) in. The big thing is covered by eeorika's (no you freaking spell-checker I don't mean erotica) answer: make the array you're reading into big enough to get all the data. – user4581301 Jun 26 '21 at 00:04
  • Ask your instructor to expand on "memory problems". In some circumstances there ARE problems with dynamically resizing `string`s, the largest probably being [memory fragmentation](https://stackoverflow.com/questions/3770457/what-is-memory-fragmentation) on an embedded system. Whether or not these are bigger problems than reading into a fixed-size buffer and missing part of the input because you stopped before overflowing the buffer or corrupting memory because you overflowed the buffer, that's a case-by-case call. – user4581301 Jun 26 '21 at 00:07
  • okay ill try to avoid gets, thank you – GZanotto Jun 26 '21 at 00:12
  • @user4581301 im still a beginner, i almost didnt understand a word you said haha but ill ask my teacher about that, thanks! – GZanotto Jun 26 '21 at 00:14
  • The general direction I'm trying to push you is don't take "It's Bad! Never Do It!" without a good reason. Even the dreaded `goto` has valid uses. There may be uses for `gets`. But in both cases there are many safer, and often simpler alternatives. An instructor's time is limited. They can't get into all the details and sometimes they have to just say, "Don't do it. It's bad." but if you can corner the instructor after class with a few well-thought out and researched questions, not only are you likely to get good answers, but you may find yourself on a path to funding future education. – user4581301 Jun 26 '21 at 00:20
  • i get (no pun intended) where you're going. thank you very much for all the advices – GZanotto Jun 26 '21 at 01:05

1 Answers1

1

Don't use gets. It is dangerous. It shouldn't be used at all. It has been removed from both C and C++ standards. Don't use gets.


I have a char array, estudios[1]

strcmp(estudios, "N") != 0

A character array of length 1 can only contain the null terminated string of length 0. The string "N" contains two characters: 'N' and '\0' which is the null termination character.

If estudios[0] is anything other than the null termination character, then it doesn't contain a null terminated string, and passing it to strcmp will violate the pre-conditions of the function and the behaviour of the program will be undefined.

Why does that happen?

The behaviour of the program is undefined.

Is it supposed to do that?

You aren't supposed to pass non-null-terminated strings into strcmp.


Here is a fixed program that probably does what you're trying to do (your example is incomplete, so I'm guessing):

std::string indicator;
char c;
std::cin >> c;
if (c != 'N') {
    cout << "Estudios completos o incompletos?" << endl;
    std::cin >> indicador;
}
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • now i understand what was my problem. thanks a lot!! i've just changed the definition of estudios[1] to estudios[2] – GZanotto Jun 26 '21 at 00:00
  • im still using gets(), im afraid that my professor doesnt accept it other way – GZanotto Jun 26 '21 at 00:00
  • Do what the professor requires of you to pass the course, but I strongly recommend supplementing your education with some up-to-date programming references if you plan on making a career of programming. Your entry into the work force will be a lot smoother and you won't have to play catch up. You always have to play a little catch-up because the idioms, best practices and even the language change as we find approaches that work better. – user4581301 Jun 26 '21 at 00:12
  • fine, thank you, i will start learning actual ways so i dont fall behind. is there any other specific thing beginners do i should avoid like this? – GZanotto Jun 26 '21 at 00:21