why does the following code gets stuck in an infinite loop in the second pass through the while loop?? I know its got something to do with the input buffer, cause it works when I use fflush(stdin) but I can't understand what's wrong. Also I need to know why std::cout.flush() does not seem to be working..
Code:#include <iostream> #include <fstream> int main() { std::ofstream file; file.open("file.dat",std::ios::trunc|std::ios::binary); if(!file) std::cout<<"unable to open for output"; struct record { char code[6]; char name[20]; int i; }; record r; int a = 0; while(1) { std::cout<<"Record " << a + 1 << std::endl; std::cout<<"Enter character code, name and an int \n"; std::cin.getline(r.code,6); std::cout.flush(); std::cin.getline(r.name,20); std::cout.flush(); std::cin>>r.i; std::cout.flush(); file.write((char *)&r,sizeof(r)); std::cout<<"\nAdd another (y\\n) : "; char c; std::cin>>c; std::cout.flush(); if(c == 'n') break; a++; std::cout<<'\n'<<'\n'; } file.close(); }



LinkBack URL
About LinkBacks



