Displaying a file

In this program I am suspose to be able to edit my grades.txt file. But I'm stuck and don't know how to display my file. Any help would be nice.

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int display(int number, char itemDescription[], int points[], int score[],
                 int average, char letter, char symbol);

/**********************************************************************
* Reads the file.
***********************************************************************/
int readFile(int number, char itemDescription[], int points[], int score[])
{
  ifstream input;
  input.open("grades.txt");

  input >> number;

  for (int count = 0; count < number; count++)
      input >> itemDescription[count] >> points[count] >> score[count];

  //display(number, itemDescription, points, score);

  input.close();
}

/**********************************************************************
* Creates a new file.
***********************************************************************/
int newFile(int number, char itemDescription[], int points[], int score[])
{
   cout << "How many records are in the file? \n";
   cin >> number;

   cout << "Description for #1: \n";
   cin >> itemDescription[0];
   cout << "Description for #2: \n";
   cin >> itemDescription[1];
   cout << "Description for #3: \n";
   cin >> itemDescription[2];
   cout << "Description for #4: \n";
   cin >> itemDescription[3];

   ifstream input;

   for (int count = 0; count < number; count++)
     input >> itemDescription[count] >> points[count] >> score[count];

   display(number, itemDescription, points, score);

  return 0;
}

/**********************************************************************
* Write the file to grades.txt.
***********************************************************************/
int writeFile(int number, char itemDescription[], int points[], int score[])
{
   ofstream output;
   output.open("grades.txt");

   output << number;

   for (int count = 0; count < number; count++)
   output << itemDescription[count] << points[count] << score[count] << endl;

   output.close();
}

/**********************************************************************
 * Displays the table.
 ***********************************************************************/
int display(int number, char itemDescription[], int points[], int score[])
{

  cout << "Number" << setw(16) << "Name" << " Points" << " Score" << endl;
  cout << "====== =============== ====== =====\n";

  writeFile(number, itemDescription, points, score);
  readFile(number, itemDescription, points, score);

   for (int count = 0; count < number; count++)
    {
      cout << number;                                                  //I think this is where I'm going 
      cout << itemDescription[count];                          //wrong, I don't know how to make
      cout << points[count];                                         //it display my table.
      cout << score[count];
    }

  cout << "====== =============== ====== =====\n";
  cout << setw(12) << "Total" << setw(22) << "--" << endl;

  return 0;
}

/**********************************************************************
* Add text here to describe what the function "main" does. Also don't forget
* to fill this out with meaningful text or YOU WILL LOOSE POINTS.
***********************************************************************/
int main(int file, int records, int d1, int d2, int d3, int d4)
{
   int number;
   char itemDescription[50];
   int points[100];
   int score[100];
   int option;

   readFile(number, itemDescription, points, score);
   newFile(number, itemDescription, points, score);
   writeFile(number, itemDescription, points, score);

   return 0;
}



Ok I added what I had on to I just forgot to include it, my main problem is that I don't know what to put in the display function in order for it to display my file.
Last edited on
Here's some advice: try asking a question. All I see up there is declarative statements.

Anyway I tried compiling your code and got several syntax errors: the problem you're having is that you haven't #included anything (you need fstream, iostream and iomanip I think) and you will also get syntax errors after that: you haven't told the compiler where to find things.

In C++ in the STL (standard template library) there are namespaces. One such namespace, which pretty much covers everything, is called std (standard I think). "In" that namespace is definitions for alot of what you're using.

You can either type "using namespace std;" at the top (which I don't recommend) or put std:: in front of everything you used that's from the std namespace (which I do recommend) or you can put using std::<function> at the top of the file.

I could go into more depth but it would take too long. If you have any specific questions ask them, I'll answer them.
Ok I added what I had on to I just forgot to include it, my main problem is that I don't know what to put in the display function in order for it to display my file.
You can use a file stream to get the data from the file then cout it.
Whats a file stream?
A data stream is a continuous flow of data. If you stream a video, for example, one computer sends a continuous flow of data to yours (usually via the internet) and your computer reads and processes it in order. Otherwise videos wouldn't work -- you'd get the data in chunks and it would be unsuitable.

A file stream is where you open a file, and read the data (or stream the data) in order. That way you get the data in a continuous flow and don't have to check that it's correct, because the data is already in order.
Can I use this if throughout my program I am going to be editing my file? If so what do i have to change for it to work.
Errr... you're using ifstream right?
fstream = file stream.
You're using filestreams already. He just means just fstream to get the data; then print it with cout.
haha sorry I'm so confusing but I just really just don't know what I'm doing. I am using ifstream, how do I use ifstream to get the data and print it.
http://cplusplus.com/doc/tutorial/files/

Try to read/learn then ask a question about what you don't understand.
Last edited on
This should help:

http://www.cplusplus.com/doc/tutorial/files/

Edit: Hrm, beaten.
Last edited on
Topic archived. No new replies allowed.