Another array problem

I'm trying to get a program that lets the user put in their own grades. I'm having trouble with naming the grades.

We haven't learned about strings or vectors yet. I need some hints on how to get this to work. It doesn't really explain this in the array tutorial.

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
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;


void getName(int records, char description[])
{
   for (int i = 1; i <= records; i++)
   {
      cout << "Description for #" << i << " ";
      cin >> description;
   }

   return;
}


void display(int records, char name[])
{
   cout << "\tNumber" << setw(16) << "Name" << setw(7) 
        << "Points" << setw(6) << "Score" << endl;
   
   cout << "\t======" << setw(16) << "===============" 
        << setw(7) << "======" << setw(6) << "=====" << endl;

   for (int i = 0; i < records; i++)
   {
      cout << setw(14) << i + 1 << setw(16) << name[i] 
           << setw(7) << "100" << setw (6) << "95%" << endl;
   }

   cout << "\t======" << setw(16) << "===============" 
        << setw(7) << "======" << setw(6) << "=====" << endl;

   cout << setw(20) << "Total" << setw(23) << "--" 
        << setw(3) << "B-" << endl;

   return;
}


int main()
{
   int records;
   cout << "How many records are in the file? ";
   cin >> records;

   char name[256];
   getName(records, name);

   display(records, name);
   return 0;
}


Here's the output for display. Everything except 'Number' and 'Name' are hard coded in. I haven't made it that far yet.

How many records are in the file? 4
Description for #1 Assign1
Description for #2 Assign2
Description for #3 Test1
Description for #4 Final
1
2
3
4
5
6
7
8
        Number            Name Points Score
        ====== =============== ====== =====
             1               F    100   95%
             2               i    100   95%
             3               n    100   95%
             4               a    100   95%
        ====== =============== ====== =====
               Total                     -- B-

name is a string, but what you want is an array of strings.
In getName, every time you do cin >> description; the value of description is overwritten. Every time you do cout << name[i]; you only get one character printed, as that is what operator [] does.
Make name in main() char name[256][80]; (256 strings 80 characters each), then change getName() and display() arguments from char name[] to char name[256][]. Then, change line 12 to cin >> description[i]; and I think that should work.
Also arrays start at 0 and
 
for (int i = 1; i <= records; i++)
is an off by 1 error.
Topic archived. No new replies allowed.