Garion, toupper is used to capitalize what is entered in xOption. This is helpful as you don't need to do, for example, while(xOption != 'E' && xOption != 'e').
Garion : toupper is if someone enters a instead of A (we have to include it)
iHutch105: What is happening is the number of students is displayed, and when The names are displayed "Class roster" is repeated 10 times. and adds 10 to each additional name entered after that. I need "Class roster" to display once, but list all the names entered. I'm guessing it has something to do with the for() loop, I just can't figure out what it is . . .
#include <string>
#include <cstdlib>
#include <iostream>
usingnamespace std;
int main ()
{
char xOption = ' ';
string cRoster [10];
int i = 0;
string name = "";
while (xOption != 'E')
{
cout << "Enter A for adding to the class." << endl;
cout << "Enter L for displaying roster." << endl;
cout << "Enter E to exit." << endl;
cout << "Choice:";
cin >> xOption;
xOption = toupper(xOption);
if (xOption == 'A')
{
cout << "Enter student's last name:";
cin >> cRoster[i];
i++;
}
elseif(xOption == 'L' )
{
cout << "Number of students enrolled:" << i << endl;
cout << "Class roster:" << endl;
for( i = 0; i < 10; i = i ++ )
{
cout << cRoster[i] << endl;
}
}
}
system ("pause");
return 0;
}
Note, I've changed that second if statement to an else-if. It's just a bit tidier. I also got rid of the redeclaration of i in the for loop. Some compilers will complain, so won't.
The reason I've indented it is because it sounds like one of the brackets might be misplaced in your local code, causing the loop issue. Apart from that, I can't see what would be causing it.
One general thought, though: have you used std::vectors before? They'd be much better suited than arrays here.
This does not work either. There is empty space where the repeated "Class rosters" were, and additional names are not being added. Also, the counter is not working properly, after I added a second name, the number of students was displayed as 11.
(Side note -I have not used std::vectors yet. After this intro class is over in a few weeks I am going to start fresh. What we are being taught seems different in some ways than the standard. ie i = i +1 instead of i++)