Read and write to from/to file

Hello,
I was given this question, in all honesty I didn't understand the question completely, but below is the code that I tried

Part 1: Create a text file by entering user input from the keyboard. the data for the file attached.
Each line contains the last and first name of a person, the letter S rn N and two integer numbers.
• Print a paper listing of the text file after it has been created,
• Turn this in along with the program that creates the file.
• The final text file should match exactly the listing on the paper provided

Part 2: -
Using functions: Menu for this test
1. Enter data from keyboard (to create the file)
2. Read data from file (send empty structure to the function) (fill it up from the file and send it back to main)
3. Print the report (send the structure with the data to the function to print
4. exit
Read the text from the data file into a structure and print it out, In the listing (print out), print the first name first and then the last name followed by the other data in the same record.


Please can someone explain what should be done !!
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
 
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{

	//system("color F0");
	string Last, First;
	char code,ch;
	int num1;
	ofstream data;
	ifstream data;
	data.open("data. txt");
	cout << "Enter Passengers names S(smoking) / N(nonsmoking) and number for data file"<<endl;
	cout << "Enter a 'c' to continue and an 'x' to exit" << endl;
	cin >> ch;

	while (ch != 'x')
	{
		cout << "Enter the last name: ";
		cin >> Last;
		cout << ("Enter the first name: ");
		cin >> First;
		cout << ("Enter S(smoking) or N(nonsmoking): ");
		cin >> code;
		cout << ("Enter two integers: ");
		cin >> num1 ;
		cout << ("Enter a 'x' to exit 'c' to continue");
		cin >> ch;
		//data >> Last >> "," >> First >> "," >> code >> "," >> num1;
	}
	data.close();

	return 0;



	cin >> Last;
	cout << ("Enter the first name: ");
	cin >> First;
	cout << ("Enter S(smoking) or N(nonsmoking): ");
	cin >> code;
	cout << ("Enter two integers: ");
	cin >> num1;
	cout << ("Enter a 'x' to exit 'c' to continue");
	cin >> ch;
	//data << Last << "," << First << "," << code << "," << num1 << num2 << endl;

	data.close();

	return 0;



}
The first part seems fairly straightforward and self-contained. I suggest you tackle that first before moving on to part 2 as a separate task.

The above code is heading in the right direction, but has a number of errors. It won't compile successfully because two different streams are declared with the same name data. For the first part only the ofstream is required.

It contains unreachable code - everything after the return statement at line 38 can never be executed. In this case that part can be deleted. Though line 51 (commented out) is needed instead of line 34 (commented out).

One minor point, the code here doesn't match the instructions, where is the second integer?
1
2
    cout << ("Enter two integers: ");
    cin >> num1 ;



Last edited on
Does the first Part mean I should create a .txt file contains smth like this or I should read from that certain file ?

1
2
3
4
Smith     John				N	3	2
Jobs      Stevens			S	9	3
Capone    Al				N	3	2
Jaskson   Michael			S	8	1


and for the second part do I have to do input to another .txt file ?

I don't know what meant by structure here is it"struct"
Does the first Part mean I should create a .txt file contains smth like this or I should read from that certain file ?

As per instructions:
"Create a text file by entering user input from the keyboard"

That clearly states you are to create a new file. However, "entering user input from the keyboard" is slightly ambiguous, it could mean type into a text editor. But no, it goes on to say, "Turn this in along with the program that creates the file." Hence you must write a program to do this task. It will accept user input from the keyboard and write the data to the new file.

Last edited on
The second part builds to some extent upon the first, but is enhanced in several ways such that it is in effect a completely separate task. I guess you should regard the experience gained in doing part one as practice for part two.

I would use a separate program as well as separate data file for this.

Additional features of part two:
• a menu offering multiple options to the user.
• the use of functions
• the use of a struct
• the use of both file input and output.

By using a structure, you can pass data composed of multiple individual items to and from functions with ease. For help on structures, see the tutorial:
http://www.cplusplus.com/doc/tutorial/structures/

Edit: To me the instructions for part two seem somewhat brief, you may need to ask whoever set the task for more detail.
Last edited on
Topic archived. No new replies allowed.