What's wrong with this code?

I need the program to display the number of names entered and display the names entered. I can't get this to work properly? Here is my code:


#include <string>
#include <cstdlib>
#include <iostream>
using namespace 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 = i + 1;
}
if(xOption == 'L' && xOption != 'A' && xOption != 'E')
{
cout << "Number of students enrolled:" << i << endl;
cout << "Class roster:" << endl;
for(int i = 0; i < 10; i = i + 1)
{
cout << cRoster[i] << endl;
}
}
}
system ("pause");
return 0;
}
I'm not sure what you are trying to do here

what is toupper suppose to be? You already had the cin for xOption so I'm confused why you would change it right after.

 
xOption = toupper(xOption);
Last edited on
Why is it not working? You'll get a more accurate solution if you give a more accurate description of the problem.
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 . . .
Seems to work okay for me.

I'll format it so it's a bit easier to read here.

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
#include <string>
#include <cstdlib>
#include <iostream>
using namespace 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++;
        }
        else if(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++)
Okay, with this I get the names to list properly, but the "number of students enrolled" << i << disappears.

if(xOption == 'L' )
{
cout << "Number of students enrolled:" << i << endl;
cout << "Class roster:" << endl;
}
for (int i = 0; i < 10; i = i + 1)
{
cout << cRoster[i] << endl;
}
Last edited on
I solved my problem. I put

cout << "Number of students enrolled:" << i << endl;
cout << "Class roster:" << endl;

between if and else if and everything seems to be working fine. Thanks for your assistance, I really appreciate it. Good day.
Topic archived. No new replies allowed.