Using Arrays in More Complex Problems

I need to create an array that can store 10 strings. This array represents the class roster. The following menu is displayed on the screen :

Enter A for adding to the class
Enter E to exit
Choice:

If the user chooses A check to see whether the class is full, if it is display the following message:

Sorry, the class is already full.

If the class is not full, ask the user to enter the last name of the student who wants to enroll in the class. Add the student to the class.

Display the menu again until user chooses E to exit.

I can't seem to figure it out. I know I'm supposed to add a counter and copy the string variable "name" to the array cRoster[10], I just don't know where or how (I keep getting error messages). Any advice is greatly appreciated. Thank you. Here is my code so far:


#include <cstdlib>
#include <iostream>
using namespace std;

int main ()
{
char xOption = ' ';
string cRoster [10];
string name = "";

while (xOption != 'E')
{
cout << "Enter A for adding to the class." << endl;
cout << "Enter E to exit." << endl;
cout << "Choice:";
cin >> xOption;
xOption = toupper(xOption);
if (xOption == 'A')
{
cout << "Enter student's last name:";
cin >> name;
}
}
system ("pause");
return 0;
}
Well, does your program even compile? It looks like you're missing the include for the string class:

#include <string> .

As for advice, use a single variable to keep track of the class size and where to add new names to the array. increment the variable at the end of your while loop, and when the user uses option A, just check if the variable is equal to the class size.
Last edited on
This is what I have now. If more than 10 names are entered, "Sorry, class is full has to appear. I'm having trouble figuring that part out. Thanks.


#include <string>
#include <cstdlib>
#include <iostream>
using namespace std;

int main ()
{
char xOption = ' ';
string cRoster [10];
string name = "";

while (xOption != 'E')
{
cout << "Enter A for adding to the class." << endl;
cout << "Enter E to exit." << endl;
cout << "Choice:";
cin >> xOption;
xOption = toupper(xOption);
for (int i = 0; i < 10; i = i + 1)
{
cRoster[i] = name ;
}
if (xOption == 'A')
{
cout << "Enter student's last name:";
cin >> name;
}
else
{
cout << "Sorry, the class is already full.";
}
}

system ("pause");
return 0;
}
Topic archived. No new replies allowed.