Arrays

I am very new to C++, I am in highschool and am taking an intro to C++ class, except it's an independent study so I don't get any help. Anyways, I'm having trouble with a program that I am currently working on. It is a GPA program and what i am trying to do is get the user to enter three things: 1. the name of their class (multiple) 2. The level of their class (0,1,2,3; 0 being for AP) and 3. Their current grade in the class. I am trying to array all of these things and put them into a cin >> so I can store them and have the console application return them at the end of the program and calculate the GPA. However, the second time the program asks for the name of a class (their second class) an error message pops up saying GPAProgram.exe has encountered an error and needs to close. If anyone knows anything about how to work arrays please let me know! Thanks!
Hi Cody.

Could you kindly post your code. Make sure to remember to put it under code tags.

Carlo
I'm terribly sorry, here is the code:
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <cstdio>
#include <cstdlib>
#include <iostream>
#define NULLPTR 0

using namespace std;
int readArray(int integerArray[], int maxNumElements);

int main()

{
    char *name[6];
    short clevel[6];
    char grade[6];
    // input number of classes

    int numclasses;
    cout << "\nEnter the number of classes you are currently enrolled in: ";
    cin >> numclasses;

    // input name of first class


    cout << "\nEnter in the name of your first class: ";
    cin >> name[0];

    // input the class level


    cout << "\nEnter the level of the class i.e: 0,1,2,3 (0 is for AP) : ";
    cin >> clevel[0];

    //input grade


    cout << "\nEnter your current grade in the class: ";
    cin >> grade[0];




    cout << "\nEnter the name of your second class: ";
    cin >> name[1];



    cout << "\nEnter the level of the class i.e: 0,1,2,3 (0 is for AP) : ";
    cin >> clevel[1];


    cout << "\nEnter your current grade in the class: ";
    cin >> grade[1];




    cout << "\nEnter the name of your third class: ";
    cin >> name[2];


    cout << "\nEnter the level of the class i.e: 0,1,2,3 (0 is for AP) : ";
    cin >> clevel[2];

    cout << "\nEnter your current grade in the class: ";
    cin >> grade[2];





    cout << "\nEnter the name of your fourth class: ";
    cin >> name[3];


    cout << "\nEnter the level of the class i.e: 0,1,2,3 (0 is for AP) : ";
    cin >> clevel[3];


    cout << "\nEnter your current grade in the class: ";
    cin >> grade[3];




    cout << "\nEnter the name of your fifth class: ";
    cin >> name[4];


    cout << "\nEnter the level of the class i.e: 0,1,2,3 (0 is for AP) : ";
    cin >> clevel[4];


    cout << "\nEnter your current grade in the class: ";
    cin >> grade[4];




    cout << "\nEnter the name of your sixth class: ";
    cin >> name[5];


    cout << "\nEnter the level of the class i.e: 0,1,2,3 (0 is for AP) : ";
    cin >> clevel[5];


    cout << "\nEnter your current grade in the class: ";
    cin >> grade[5];



    cout << "\nEnter the name of your seventh class: ";
    cin >> name[6];


    cout << "\nEnter the level of the class i.e: 0,1,2,3 (0 is for AP) : ";
    cin >> clevel[6];


    cout << "\nEnter your current grade in the class: ";
    cin >> grade[6];





    system("PAUSE");
    return 0;
Your arrays need to be (the number of classes) long (so in your declarations, replace the sixes with sevens).

It seems counter-intuitive, but...

-Albatross
We tried that but the same error message keeps popping up.
Does the error say anything more, like Segmentation Fault?

-Albatross
Nah, it's just the "GPAProgram.exe has encountered an error and needs to close" error message. I don't know if its anything from the source code or if its just the program itself. I'm using Code::Blocks if that helps at all. Standard C++ program at our school.
And there is no debugger that comes with MinGW?

-Albatross
The problem is the way you are using arrays.
When you do
 
cin>> name[0];

It will write all into that array. Not at position 0 of the array. To do that you'd need a vector. I recommend using vector and string anyway.
1
2
3
std::vector<string> name

cin >> name[0];
While RedX has a point about vectors and strings, [] performs no out of bounds checking, and the vector does not resize itself via that method (AFAIK). I recommend using push_back().

1
2
3
string temp;
getline(cin, temp);
name.push_back(temp); //This resizes your vector and adds the data onto the end. 


Alternatively, you could allocate the amount of space you need ahead of time, but...

-Albatross
Last edited on
could you please show me where I would put the code?
That little snippet I gave should be used in a loop, and the final result of that should replace lines 24-121.

On loops:
http://cplusplus.com/doc/tutorial/control/

-Albatross
Topic archived. No new replies allowed.