please can anyone help me with this code.

: Write a program that will: 1) read an array of records from the keyboard, 2) store
this information to a binary file, 3) read from the binary file back to the array of records, 4) store
this information to a textfile. Left justify the information for each field. Each record will consist
of the following fields:
first name 15 characters
last name 15 characters
street address 30 characters
city 20 characters
state 5 characters
zip long integer
You may assume a maximum of 20 records.
Sample Run:Enter the following information
Person’s First Name: Billy
Person’s Last Name: Berry
Street: 205 Main Street
City: Cleveland
State: TX
Zip: 45679
Enter a Y if you would like to input more data
Y
Enter the following information
Person’s First Name: Sally
Person’s Last Name: Connely
Street: 348 Wiley Lane
City: San Francisco
State: Md
Zip: 54789
Enter a Y if you would like to input more data
n
That’s all the information
The output file contains the following:
First Name Last Name Street City State Zip Code
Billy Berry 205 Main Street Cleveland Tx 45679
Sally Connely 348 Wiley Lane San Francisco Md 54789



#include<iostream>
#include<cstring>
#include <fstream>
using namespace std;
struct personsinfo
{
char firstname[15];
char lastname [15];
char streetaddress[30];
char city [20];
char state[5];
int zipcode;
};

int main()
{
personsinfo pr;
fstream personsinfo("greacon.txt", ios::out|ios::binary);
cout<< " Enter the following information\n\n";
char response;
do
{
cout<< "Enter person's FirstName: \n";
cin.getline(pr.firstname, 15);
cout<<"Enter person's LastName: \n";
cin.getline(pr.lastname, 15);
cout<<"Enter Street Address: \n";
cin.getline(pr.streetaddress, 30);
cout<<"Enter City: \n";
cin.getline(pr.city, 20);
cout<<"Enter State: \n";
cin.getline(pr.state,5);
cout<<"Enter Zipcode: \n";
cin>>pr.zipcode;
personsinfo.write(reinterpret_cast<char *> (&pr), sizeof(pr));
cout<<"press Y if you like to input more data\n";
cin>>response;
}
while(response== 'Y'|response=='y');
personsinfo.close();
personsinfo.open("greacon.txt", ios::in|ios::binary);
personsinfo.read(reinterpret_cast<char *> (&pr), sizeof(pr));
cout<<"Thats all the information\n\n";
cout<<"The output file contains the following:\n";
while(!personsinfo.eof())
{
cout<< pr.firstname<< " ";
cout<< pr.lastname<< " ";
cout<< pr.streetaddress<<" ";
cout<< pr.city<< " ";
cout<< pr.state<< " ";
cout<< pr.zipcode;
cout<<endl;
cin.get();
personsinfo.read(reinterpret_cast<char *> (&pr), sizeof(pr));
}
personsinfo.close();
system("pause");
return 0;
}


i don't seem to know what the problem is. it allows me input the first set of data. after i hit 'Y' it goes on to the input last name instead of input first name. and the it only reads for two sets of data.



Last edited on
Your std::cin istream isn't flushed, so it processes the enter you pressed.
I use std::getchar() to get that char (which I believe is '\n') out of the stream.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
void fun()
{

    using namespace std;
    struct personsinfo
    {
        char firstname[15];
        char lastname [15];
        char streetaddress[30];
        char city [20];
        char state[5];
        int zipcode;
    };

    personsinfo pr;
    fstream personsinfo("greacon.txt", ios::out|ios::binary);
    cout<< " Enter the following information\n\n";
    char response;
    do
    {
        cout<< "Enter person's FirstName: \n";
        cin.getline(pr.firstname, 15);
        cout<<"Enter person's LastName: \n";
        cin.getline(pr.lastname, 15);
        cout<<"Enter Street Address: \n";
        cin.getline(pr.streetaddress, 30);
        cout<<"Enter City: \n";
        cin.getline(pr.city, 20);
        cout<<"Enter State: \n";
        cin.getline(pr.state, 5);
        cout<<"Enter Zipcode: \n";
        cin >> pr.zipcode;
        personsinfo.write(reinterpret_cast<char *> (&pr), sizeof(pr));
        cout << "press Y if you like to input more data\n";
        cin >> response;
        std::getchar();
    }
    while(response== 'Y' | response== 'y');
    personsinfo.close();
    personsinfo.open("greacon.txt", ios::in|ios::binary);
    personsinfo.read(reinterpret_cast<char *> (&pr), sizeof(pr));
    cout<<"Thats all the information\n\n";
    cout<<"The output file contains the following:\n";
    while(!personsinfo.eof())
    {
        cout<< pr.firstname<< " ";
        cout<< pr.lastname<< " ";
        cout<< pr.streetaddress<<" ";
        cout<< pr.city<< " ";
        cout<< pr.state<< " ";
        cout<< pr.zipcode;
        cout<<endl;
        cin.get();
        personsinfo.read(reinterpret_cast<char *> (&pr), sizeof(pr));
    }
    personsinfo.close();
    system("pause");
    return;

}
i used the std::getchar(); function and it worked for me. this was really helpful.
Topic archived. No new replies allowed.