-2
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    int k, i = 0;
    vector<char[30]> student;
    while (true) {
        cin >> k;
        cin.ignore();
        if (k == 1) {
            cout << "add student:";
            fgets(student[i], 30, stdin);
            fflush(stdin);
        }
    }
}

I don't understand why when i type ''abc'' the loop exits.I'm new to C++ so I don't know.

drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • 1
    `abc` is not an `int`. The code is not verifying that `cin>>k;` succeeded, which it won't in this case. Odd that the loop exits. Best to use a debugger to determine what is happening. – Eljay Apr 25 '22 at 16:01
  • 3
    Can you mention which C++ textbook you are using, to learn C++? `vector`? Using `cin`, `fgets`, and `fflush` together? That's ...not a very good textbook. – Sam Varshavchik Apr 25 '22 at 16:17
  • 1
    @SamVarshavchik: Not to mention [`fflush(stdin)` is nonsensical undefined behavior](https://stackoverflow.com/q/62789938/364696). I suspect the entirety of this code is copy'n'paste cargo cult programming. – ShadowRanger Apr 25 '22 at 16:19
  • The code is mixing C I/O with C++ streams. To begin with, this doesn't seem to be a good choice. You can rewrite it to make it both less error prone and also more readable. – digito_evo Apr 25 '22 at 16:19
  • For the record, I can't reproduce *just* typing `abc`. [If I type `1`, *then* `abc` on a new line, it segfaults](https://tio.run/##XY@9bsMwDIR3PQWRLjLgFA6y1bZeI0OQQZVpmbAsGfpphyKvXtWOWyQNhxu@uyNBNc97rVTOL2SVSR1CQy5Ej3IS7M4@UEXnBUuBrAYrJwyzVAghdjUjG2GSZHnBvhgss4KxBIIWqvpGtn6jBunPx@oiluKy18bN/RzIIPDoExawrVhHkQUhYKwfyStp6zzy4k6pBz5C28LhsX3LuxShaWAnu@7v5Nuu/pfpNcbAf80zXUo4VuX6GNniKdmbFAb@bF3ZptecD0y@q2/VG6lD3p@kMYui987/AA "C++ (gcc) – Try It Online"), for [the reason given in bitmask's answer](https://stackoverflow.com/a/72002662/364696). – ShadowRanger Apr 25 '22 at 16:27

2 Answers2

1

Your student vector is empty. Doing anything with student[i] (regardless of i) will result in undefined behaviour, possibly crashing your program.

bitmask
  • 32,434
  • 14
  • 99
  • 159
  • Yes, the loop exists probably due to a seg fault or something similar. – digito_evo Apr 25 '22 at 16:20
  • While yes, if the line using `student[i]` is reached, then undefined behavior occurs, that line should never be reached. `k` should never be `1`; when the formatted input fails, `k` should be `0`, and since the OP is passing `"abc"`, it should definitely fail, so `k == 1` should never occur, and the undefined code should never be reached. Granted, *technically*, the compiler is allowed to assume undefined behavior never occurs, so it *could* choose to do arbitrary things to avoid ever reaching that line. – ShadowRanger Apr 25 '22 at 16:21
  • @ShadowRanger Fair point. However, if they enter a string that doesn't parse to int, and the condition is not satisfied, the loop should not terminate. – bitmask Apr 25 '22 at 16:25
  • @bitmask: Yar. [My assumption](https://stackoverflow.com/questions/72002396/clear-buffer-before-taking-new-line/72002662?noredirect=1#comment127228454_72002396) is they actually typed `1`, then `abc`, and neglected to inform us of this (nor of the fact that the terminated program almost certainly told them it died with a segfault). – ShadowRanger Apr 25 '22 at 16:29
  • @ShadowRanger Yes, that is the most plausible explanation. – bitmask Apr 25 '22 at 16:29
0

"abc" cannot be parsed as an int, so the input will fail. The stream will remain in failed state until you have acknowledge the failure by clearing the state using the clear member function. You never clear the failure, so the following extractions will fail before asking any input and the loop will never stop.

The language implementation is allowed to assume that any thread will eventually terminate, or make a call to a library I/O function (or do one of a few other things which this program doesn't do). Arguably, cin >> k; may be considered to be a "call to a library I/O function", but if the implementation doesn't consider that in the case of stream in a failure state, then it may just terminate the program.


If you did enter 1 and the if branch is entered, the behaviour of the program will be undefined because:

  1. This:

    fgets(student[i], 30, stdin);
    

will write outside the bounds of the student vector.

  1. :

    fflush(stdin);
    

Passing an input stream to fflush results in undefined behaviour.

eerorika
  • 232,697
  • 12
  • 197
  • 326