cout<<"\n Current items in Consoles: \n";
viewConsoles();
cout<<"\n \t Enter details of product you want to add :";
cout<<"\n \n \t Code : ";
gets(f.pcode);
cout<<"\n \n \t Brand : ";
gets(f.brand);
cout<<"\n \n \t Model : ";
gets(f.model);
cout<<"\n \n \t Cost : ";
cin>>f.cost;
cout<<"\n \n \n Are you sure you want to add this product?(y/any other key)";
if(getch()=='y')
{
fout.open("consoles.dat",ios::app);
fout<<endl;
fout.write((char*)&f,sizeof(f));
fout.close();
cout<<"\n New prodcut added to store";
}
else
cout<<"\n No changes have been made";
cout<<"\n Press any key to continue...";
getch();
Im using codeblocks 12.11.
The problem i have is that it just skips the line gets(f.pcode);
also when it writes the cost into the file it gives some garbage value.
As a side, I put the new line at the end of the output rather than the beginning. It is always good to leave the file in a state that you can write to it without having to remember to print a new line first. Also, some computers have trouble with files that do not end in a new line.
Thanks man that fixed the junk values but getline isnt working
it says " no matching function for call to getline(std::istream, char[4])".
I replaced my character array with string and it compiled but i still had the same problem i had with gets(). I dont get a chance to enter a value
There is probably a newline character '\n' remaining in the input buffer after a previous cin >> operation. You need to clear this before the getline() statement. For example by using cin.ignore().