gets() and writing into file

Ive got a problem in the following code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 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();
            


This is the file structure
1
2
3
4
5
6
7
struct fileStruct
{
    char pcode[4];
    char brand[15];
    char model[15];
    float cost;
}f;

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.
The problem i have is that it just skips the line gets(f.pcode);

C++ Doesn't "just skip" lines. It is undoubtedly executing that line, it's just that it must be doing something different from what you expect.

If you step through your code with a debugger, you'll probably see what's going wrong.
Your mixing C io with C++ io. I don't know, but that's probably an issue. Replace gets( string ) with getline( cin, string)

As for the junk value, this is because you used write(), and write() does not format data. Try:
1
2
3
4
5
6
7
8
9
if(getch()=='y')
{
  fout.open("consoles.dat",ios::app);

  fout << f.pcode << " " << f.brand << " " << f.model << " " << f.cost << '\n';

  fout.close();
  cout<<"\n New prodcut added to store";
}


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
Last edited on
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().
There are two getlines, one for c-strings and one for std::string:

c-string:
http://www.cplusplus.com/reference/istream/basic_istream/getline/

std::string:
http://www.cplusplus.com/reference/string/string/getline/
Topic archived. No new replies allowed.