Array Help

Hello, I am having trouble with looping a string array to enter names into that array. I enter the number of names I want to enter and it does not stop the loop. After that it crashes. Any idea what I could do to fix this?

Note: I am not able to use Vectors or anything on this assignment.

Here is the code that I have so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
using namespace std;
int main() 
{
	int studentNum;
	int n = 0;

		cout << "How many students do you need to record information for?" << endl;
			cin >> studentNum;
	
	string studentName[studentNum];
	cout << "Enter the name for each student below. " << endl;
		for (string name; cin >> name; n++) {
		
			studentName[n] = name;
		}
			for (n = 0; n < studentNum; n++) {
				cout << studentName[n] << endl;
			}
		
}

Thanks for any help. I really appreciate it.
-Torm04
Last edited on
Anyone :3
closed account (28poGNh0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# include <iostream>
# include <string>
using namespace std;

int main()
{
	int studentNum;

    cout << "How many students do you need to record information for?" << endl;
    cin >> studentNum;
    cin.get();
	string studentName[studentNum];

	cout << "Enter the name for each student below. " << endl;
    for (int i=0;i<studentNum;i++)
    {
        cout << "Enter the name of the student nbr " << i+1 << " -> ";
        getline(cin,studentName[i]);
    }
    for (int i=0;i<studentNum;i++)
        cout << "The name of the student nbr " << i+1 << " : " << studentName[i] << endl;

    return 0;
}
Accidental Post
Last edited on
I have some new code written out and I am stumped again. Basically I am trying to enter 4 test scores for each student in an array but do not know how to do it. Here is my code so far:
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
# include <iostream>
# include <string>
using namespace std;

int main()
{
	int studentNum, id;
	int studentScores = 3; //The amount of scores to average together for each student

    cout << "How many students do you need to record information for?" << endl;
    cin >> studentNum;
    cin.get();
	//---------------------------------------------------------------------------- Arrays
	string studentName[studentNum]; //Array that holds each of the students names.
	int studentId[studentNum]; //Array that holds each of the students id numbers.
	int studentScore[studentNum]; //Array that holds each of the students three scores.
	//----------------------------------------------------------------------------End of Arrays
	cout << "Enter the name for each student below. " << endl;
    for (int i=0;i<studentNum;i++)
    {
        cout << "Enter the name of the student " << i+1 << ". -> ";
        getline(cin,studentName[i]);
    }
   
    for (int i = 0; i < studentNum; i++) //Gets the id number for each of the students.
    {
    	cout << "Enter the student id for " << studentName[i] << endl;
    		cin >> studentId[i];
    		
    }
    	cout << "Please enter the three scores for each of the students." << endl;
    for (int i = 0; i < studentScores; i++)
	{
		cout << studentName[i] << ": ";
			cin >> studentScore[i];
	} 
     
	    for (int i=0;i<studentNum;i++) //Displays the name an id number of each student.
    {
      cout << "Student Name: " << studentName[i] << endl << "ID: " << studentId[i] << endl << "Students Scores: " << studentScore[i] << endl;
      
    }
    return 0;
}

Thanks for any help you can give.
Bump :3
Topic archived. No new replies allowed.