0

I'm doing this school assignment and wrote this code, that creates a structure called "knjiga", that gets data about a book. But for some reason, the program lets me input only 2 full arrays of "knjige" (should be 15, not 2!).

Sorry if the variables are written in a foreign language, I just don't have time to change all of them, also, my school wants to use variables written in our language

#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;

struct knjiga{
    string avtor;
    string naslov_knjige;
    string zalozba;
    int letnica;
};

//input
void vnosKnjig(knjiga knjige[]){
    for(int i = 0;i<15;i++){
        getline(cin, knjige[i].avtor);
        getline(cin, knjige[i].naslov_knjige);
        getline(cin, knjige[i].zalozba);
        fflush(stdin);
        cin >> knjige[i].letnica;
        fflush(stdin);
    }
}

//output
void izpisKnjig(knjiga knjige[]){
    for(int i = 0;i<15;i++){
        cout << i+1 << ". Knjiga: " << knjige[i].avtor << " , " << knjige[i].naslov_knjige << " , " << knjige[i].zalozba << " , " << knjige[i].letnica << endl;
    }
}

int main() {
    struct knjiga knjige[15];
    vnosKnjig(knjige);
    izpisKnjig(knjige);
}

Here's something I've inputted, as a sample:

jkrowling
harry
mohorjeva
2004
rowling
potter
neki

After I entered "neki", the program already outputs, instead of going on. And here is the weirder output:

1. Knjiga: jkrowling , harry , mohorjeva , 2004
2. Knjiga:  , rowling , potter , 0
3. Knjiga:  ,  ,  , 4196163
4. Knjiga:  ,  ,  , -2085287456
5. Knjiga:  ,  ,  , -2087467936
6. Knjiga:  ,  ,  , -984944128
7. Knjiga:  ,  ,  , 6299720
8. Knjiga:  ,  ,  , 1
9. Knjiga:  ,  ,  , 0
10. Knjiga:  ,  ,  , -984943888
11. Knjiga:  ,  ,  , 6
12. Knjiga:  ,  ,  , -2096610263
13. Knjiga:  ,  ,  , 4198293
14. Knjiga:  ,  ,  , 4198315
15. Knjiga:  ,  ,  , 0
  • "_I just don't have time to change all of them_" - Search/replace? – Ted Lyngmo Jul 01 '21 at 17:38
  • 3
    [Don't `fflush(stdin)`](https://stackoverflow.com/questions/2979209/using-fflushstdin). What did you intend to accomplish by doing so? – Nate Eldredge Jul 01 '21 at 17:48
  • 1
    Maybe you were intending to work around this: [https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – drescherjm Jul 01 '21 at 17:50
  • You should read through https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction to learn how to correctly mix `getline` and `>>`. I would guess the stream may be going bad because `cin >> knjige[i].letnica;` fails, so all of the other input is skipped. You should add code to detect if reads are successful or not. – Retired Ninja Jul 01 '21 at 17:50
  • One of my teachers told us to use fflush(stdin) if we have a switch between getline() and cin scenario, as there is something that will happen and ruin the whole thing,...if that makes sense. @NateEldredge – Tim Hajdinjak Jul 01 '21 at 17:51
  • 2
    ***One of my teachers told us to use fflush(stdin)*** That is not the correct solution. I believe that is undefined behavior. – drescherjm Jul 01 '21 at 17:52
  • Flush makes sure data in buffers is written to the underlying media. It's not that useful when data is being read. I've seen some implementations that empty out the read buffer, but is not something you can count on. Even if this is implemented, more data could arrive at any time, making flushing only marginally useful. Instead have a robust communication protocol that makes it easy to separate messages. If you don't want a message, read and discard it. – user4581301 Jul 01 '21 at 18:28
  • 1
    @drescherjm I believe that it's UB too, except in MSVC, where I think it's defined to do _nothing_ - so, clearly pointless and potentially destructive. – Ted Lyngmo Jul 01 '21 at 18:51

0 Answers0