Below is my code.

My assignment is to write a code to ask the user to enter the amount of people in the class between 1 and 25. Then enter that many names and it will list the first and last name alphabetically. ex. jon matt kate = jon matt

I do not know much about writing code but the loop is giving me problems.

and also i have an error code: error '>>' no operator matches operand when i have cin >> nameArray.

#include <iostream>
#include <string>

using namespace std;
string nameArray[25];

int main()
{
int numStudents;


for(int x = 0; x < numStudents; x++)
{
cout << " Please enter name " << x+1 << endl;
cin >> nameArray;


for(int x = 0; x < numStudents; x++)
{
cout << " Name " << x+1 << x++ << endl;
}

system("pause");
return 0;
}

I changed the cin line (line 15).

You were also missing a } .

It compiles now, but there is more work to do. You will get a runtime error when you try to run because you have not initialized the numStudents variable.

#include <iostream>
#include <string>

using namespace std;
string nameArray[25];

int main()
{
int numStudents;


for(int x = 0; x < numStudents; x++)
{
cout << " Please enter name " << x+1 << endl;
cin >> nameArray[x];


	for(int x = 0; x < numStudents; x++)
	{
	cout << " Name " << x+1 << x++ << endl;
	}

}
system("pause");
return 0;
}

Edited 5 Years Ago by coolbeanbob: n/a

i tried it and it says numStudents is not a declared variable and then when i click continue, it doesnt give any response. just says press any key to exit..

i am completely lost with this program.

I added a few things and it works. it asks how many students but then it goes all the way to 25 instead of stopping at the given amount. and it also doesn't put it alphabetically.

how can i do these two things:

#include <iostream>
#include <string>


using namespace std;

const int minStudents = 1;
const int maxStudents = 25;
const int Size = 30;
string nameArray[25];

int main()
{

    int x;


    cout << " How many students are there? " << endl;
    cin >>  x;



for(int x = 0; x < 25; x++)
{
cout << " Please enter name " << x+1 << endl;
cin >> nameArray[x];
}

while(x <= 0  || x > 25)
    {
        cout << "Invalid entry. Enter a number between 1 and 25. " << endl;
    cin >> x;
}

system("pause");
    return 0;
}

Edited 3 Years Ago by pyTony: fixed formating

i tried it and it says numStudents is not a declared variable and then when i click continue, it doesnt give any response. just says press any key to exit..

i am completely lost with this program.

It's because you have not given numStudents a value. The for loop can not be executed until numStudents has a value.

This article has been dead for over six months. Start a new discussion instead.