How do I get the program to recognize that 10 students have signed up for the class and display "Sorry class is full" after the user tries to enter an eleventh student? Thanks. Here is my code:
#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.";
}
}
#include <string>
#include <cstdlib>
#include <iostream>
usingnamespace 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;
}
think what you want to do
1) get rid of that for loop on line
18 - 20
2) declare i where you declare name and options ect , so on line 11. Then initialize it to 0.
3) instead of inputting to the name string you can directly input to the array. cin >> cRoster[i]; then increment i by 1.
4) Your while loop should loop while option is not equal to 'E' and i is less than 10.
5) You should get rid of system pause and using namespace std; If you don't want to scope directly by typing std:: everywhere you can put using std::cout , using std::cin...
6) You shouldn't use magic numbers for constants like you do for your array assign the value to a constant variable then use that.