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.
#include <iostream>
#include <string>
usingnamespace 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
# include <iostream>
# include <string>
usingnamespace 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;
}
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:
# include <iostream>
# include <string>
usingnamespace 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;
}