in our c++ clas we are writing a program that stores files in a sequential records. our teacher isnt the brightest and he doesn't help us. ive tried for about 2 weeks to figure it out, some please help.
Code:
//Wirte a program that writes a users name, address, and phone to a file.
//Then display a menu for the user to choose to add a record, veiw the record, or end.
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
main()
{
system("cls");
int choice = 0;
char name[26] = " ";
char address[30] = " ";
char phone[12] = " ";
int A = 0;
do
{
cout << "Main Menu" << '\n';
cout << "1. Add Record " << '\n' <<"2. Display File " << '\n' << "3. End " << '\n' << "Enter choice: ";
cin >> choice;
cout << '\n';
system("cls");
if(choice == 1)
{
ofstream outfile;
outfile.open("User.DAT", ios::out);
do
{
cout << "Enter your name: ";
cin >> name;
cin.ignore(80, '\n');
cout <<"Enter your address: ";
cin.get(address, 30);
cin.ignore(80, '\n');
cout << "Enter your phone number: ";
cin.get(phone, 12);
cin.ignore(80, '\n');
outfile << name;
outfile << address;
outfile << phone;
cout << '\n' << "Press 1 to add another file and 2 to exit to main menu: ";
cin >> A;
}while(A == 1);
outfile.close();
}
ifstream infile;
if(choice == 2)
{
infile.open("User.DAT",ios::in);
infile >> name;
infile >> address;
infile >> phone;
do
{
cout << "Name: " << name << endl;
cout << "Address: " << address << endl;
cout << "Phone: " << phone << endl;
}while(!infile.eof());
infile.close();
}
cout << '\n' << '\n';
}
while(choice !=3);
return 0;
}