1
#include <iostream>

using namespace std;

int main()
{
    int N;
    int T;
    cin>>N;
    struct key { 
        std::string namek;
        int szam;
        int szat;
        std::string kol;
    };

    key kert[N];
    for (int i=0; i<N; i++) {    
        int k=i;
        cin.ignore(100, ' ');
        getline(cin, kert[k].namek);
        cin>>kert[k].szam;
        cin.ignore(100,' ');
        cin>>kert[k].szat;
        cin.ignore(100, ' ');
        getline(cin, kert[i].kol);
   }
   for (int i=0; i<N; i++) {
        int k=i;
        cout<<kert[i].namek;
   }

I want to read into the the 'kert' struct but my program only reads into kert[0] and then returns. I don't know where I messed up because I used getline for strings and cin for ints. I know cin leaves the '\n' in the buffer and tried reading dummy chars after cin , I also tried cin.ignore (as in the code) but nothing helps. Where did I mess up?

trolbtrobl
  • 13
  • 4
  • you are ignoring spaces, not new lines. `key kert[N];` variable length arrays are not supported in standard c++(AFAIK) and what do you add 4 to `N` in your first loop? – Biruk Abebe May 26 '16 at 16:44
  • @bkVnet The variable array lengths is supported. – pingul May 26 '16 at 16:46
  • @pingul as a GCC extension, yes – Rakete1111 May 26 '16 at 16:47
  • @pingul Since which version of C++? My compiler supports C++11 and this code will not compile. Or are you saying supported as a compiler extension. – Biruk Abebe May 26 '16 at 16:48
  • @bkVnet at first I thought it was because N was too small but, I will edit it out – trolbtrobl May 26 '16 at 16:51
  • @bkVnet I did some looking up after my statement, and I think my initial statement was somewhat wrong (or inaccurate at least) as Rakete1111 points out. There's a bunch of posts discussing it, like http://stackoverflow.com/questions/1887097/variable-length-arrays-in-c. – pingul May 26 '16 at 16:52
  • You should replace `cin.ignore(100, ' ');` with something like `cin.ignore(INT_MAX, '\n');`,you don't need the `ignore` statement between the two `cin`s and move the first `ignore` statement out of the loop. – Biruk Abebe May 26 '16 at 17:02
  • Most likely a dupe of: http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction – NathanOliver May 26 '16 at 17:29

1 Answers1

1

I did a bit of playing and changing the ignores allowed me to output all the names in kert. Now the ignores look for the endline character and are only called to counteract the cin's.

#include <iostream>

using namespace std;

int main()
{
    int N;
    cin>>N;
    struct key { 
        std::string namek;
        int szam;
        int szat;
        std::string kol;
    };

    key kert[N];
    cin.ignore(100, '\n');
    for (int i=0; i<N; i++) {    
        getline(cin, kert[i].namek);
        cin>>kert[i].szam;
        cin>>kert[i].szat;
        cin.ignore(100, '\n');
        getline(cin, kert[i].kol);
   }
   for (int i=0; i<N; i++) {
        cout<<endl<<kert[i].namek<<endl;
   }
   return 0;
}
Russ
  • 81
  • 1
  • 6