Hello all,

I want to change the following code to have the user enter ANY NAME to see if the person is present in the array. I'v been trying for a couple hours now but keep getting errors. There's more to this problem but I have to get past this hurdle... Please help. Thank You.

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

int main()
{

string myStudents[3];

//this first part works. It stores names. I will change the loop later to do 100..
for(int counter = 0; counter <3; counter++)
{
    cout <<"Enter student number " << counter+1 << " name: ";
    cin>>myStudents[counter];
    cout << endl;
}
// I use a specific name here. I need to change this so that I can cin any name for search.
bool isAndy = false;

for(int counter = 0; counter <3; counter++)
{
    if (myStudents[counter] == "Andy")
    {
        isAndy = true;
    }
}
if(isAndy == true)
    {
        cout<<"Yes, Andy is in Class"<<endl;
    }
else
    {
        cout<< "No, Andy is not in Class" <<endl;
    }
system ("pause");
return 0;
}
Re: Array to store names and later enter any name to see if person is present... 80 80

Please use [code] //code goes here [/code] (code tags) to maintain the formatting of your code when you post it.

What are the errors that you are getting?

Try to consolidate your code inside your for loop down to just a couple of lines. Hint: myStudents[counter] == "Andy" resolves to either true or false.

Re: Array to store names and later enter any name to see if person is present... 80 80

I will. Thanks.

I want to do away with andy so that I can cin for example "userChoice" (which takes in any name) it should checks the array to see if userChoice is true or false and return the following:

{
cout<<"Yes, "<< userChoice << " is in Class"<<endl;
}
else
{
cout<< "No, "<< userChoice << " is not in Class" <<endl;
}

So I will have to do something like this prior to the cout code above. Any suggestions:

{ cout >> "Please enter student name: ";
  cin >> myStudents[userChoice] >> endl;
} 
for(int counter = 0; counter <3; counter++)

bool isuserChoice = false;
if (myStudents[counter] == userChoise{
isuserChoise = true;
}
}
if(isuserChoise == true)
//then it will cout the result (which i placed above as an example).
cout<<"Yes, "<< userChoice << " is in Class"<<endl;
}
else
{
cout<< "No, "<< userChoice << " is not in Class" <<endl;
}
Re: Array to store names and later enter any name to see if person is present... 80 80

You're missing a couple of () but yeah that's the general idea.

To consolidate it further:

if(myStudents[counter] == userChoice) //statement in parens evaluates to true or false
{
     cout<<"Yes,"<<endl;  //etc.
}
else
{
     cout<<"No,"<<endl //etc.
}

Someone is bound to point out that you don't need the braces, but it helps to put them in initially to tell where everything belongs.

So, look at the statement that I made and trace it though to see why it's equivalent to your statements (with a lot less code). Hint, saying anything == true is redundant.

Re: Array to store names and later enter any name to see if person is present... 80 80

Ok, here's what I got.
Error: No operator found which takes a left hand operand of type.
Error 2: identifier "isuserChoice" is indefined. Here I travel down the road of no return.

Can you take a look at the very first code up top and show me where i'm going wrong. This is kicking my butt. I know it's not that difficult but I can't figure it out.

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

int main()
{

string myStudents[3];
int userChoice;

for(int counter = 0; counter <3; counter++)
{
	cout <<"Enter song #"<< counter+1 << " to your music library: ";
	cin>>myStudents[counter];
}
	bool isuserChoice = false;
	{
		cout << "Please enter student name: ";
		cin >> myStudents[userChoice] >> endl;
	}

for(int counter = 0; counter <3; counter++)

if(isuserChoise == true)
	
		{
		cout<<"Yes, "<< userChoice << " is in Class"<<endl;
	}
else
	{
		cout<< "No, "<< userChoice << " is not in Class" <<endl;
	}


system ("pause");
return 0;
}
Re: Array to store names and later enter any name to see if person is present... 80 80

Compare lines 17 and 25: Look carefully at your variable name.

Also, look at line 20: Can you assign an input value to an endl?

On line 25, see what happens when you remove the ==true

Re: Array to store names and later enter any name to see if person is present... 80 80

OK I give up... Thanks for your assistance jonsca. I'll have to find someone at school who's good at C++. Thanks again.

Re: Array to store names and later enter any name to see if person is present... 80 80

Well, that's fine too. I was trying to point out it was a misspelling of the variable name (spelling counts, ya know).

Hint: at this stage of the game, put in a few lines of code, compile them with no errors. Add in a few more lines. Compile again. That way you'll be able to keep track of exactly which line messed things up.

Re: Array to store names and later enter any name to see if person is present... 80 80

Things are looking up. My classmate and I figured it out.

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

int main ()
{

        string myStudents[3];
        
        for (int counter = 0; counter < 3; counter ++)
        {
                cout<<"Enter the name of student "<< counter+1 <<": ";
                getline(cin, myStudents[counter]);
                
        }
        
        string request = "";
        cout<<"Which student are you searching for? ";
    getline(cin,request);
        bool containsStudent = false;

        for(int counter = 0; counter<3; counter++)
        {
                if (myStudents[counter] == request)
                { containsStudent = true;}
        }

        if (containsStudent == true)
        {cout<<"Yes, the student you are looking for is in class."<<endl;}
        else
        {cout<<"No, the student you are looking for is not in class."<<endl;}

        


		system ("pause");
        return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.