File Handling

Write a program that asks the user to input his/ her name, age and address then appends them on a text file named “employee.txt”.

Validation:

(First run)
Enter your name: James Bugrot
Enter your age: 16
Enter your address: Purok Tumpok Sitio Pahu, La Carlota City

Content of employee.txt
James Bugrot^16^Purok Tumpok Sitio Pahu, La Carlota City
------------------------------------------------------------------------------------------------------

(Second run)
Enter your name: Juan Dela Ugly
Enter your age: 18
Enter your address: Bangga Mayor, San Enrique

Content of employee.txt
James Bugrot^16^Purok Tumpok Sitio Pahu, La Carlota City
Juan Dela Ugly^18^ Bangga Mayor, San Enrique
------------------------------------------------------------------------------------------------------

(Third run)
Enter your name: Mr. Yuso
Enter your age: 20
Enter your address: Unknown

Content of employee.txt
James Bugrot^16^Purok Tumpok Sitio Pahu, La Carlota City
Juan Dela Ugly^18^ Bangga Mayor, San Enrique
Mr. Yuso^20^Unknown
Last edited on
Is this a homework assignment? We can help you, but we definitely wont do the whole thing for you.
Well said. If you have an error somewhere in your program, and you post that, people can help you. The way you have it written now makes it sound like you want someone to write a program for you, not give you pointers. Having someone else write code for you won't get you very far in anything.
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
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile;
  string fname, lname;
  int age,i;
  string address;


  myfile.open ("example.txt");
  cout<<"Enter your  first name: ";
  cin>>fname;

  cout<<"Enter your last name: ";
  cin>>lname;
  myfile << "Name: "<< fname<<" "<< lname<<endl;

  cout<<"Enter your age: ";
  cin>>age;
  myfile << "Age: "<<age<<endl;

  cout<<"Enter your address: ";
  cin>>address;
  myfile << "Address: "<<address<<endl;


  myfile << "";
  myfile.close();




  return 0;
}



this is the code, it runs but if I put more than 1 word it only print the first one
Last edited on
Topic archived. No new replies allowed.