array and string

#include <iostream>
#include<cstring>
#include <string>

using namespace std;

int main()
{
int i=0;

string t;
int a;
cout<<"plese enter the number of times u wan to key in"<<endl;
cin>>a;
int vector[a];

for(int i=0;i<a;i++)
{
cout<<"numbers["<<i<<"]="<<endl;
cin>>vector[i];
}

cout<<"Enter a name to search=>"<<endl;
cin>>t;

for(int i=0;i<a;i++)
{
if(t==vector[i])
{
cout<<"record found"<<endl;
}
else
{
cout<<"record not found"<<endl;
}
}


}

I got error when compile the code, can anyone help me to see through.The question is:Ask the user to input the number of times he want to key in the record.After he key in the record, insert one of the word to search whether the word is inside the record.
what does the error say?
PS: the code goes inside code tags
well for one thing you cant use the equality operator on items of different types. Here:

if (t==vector[i])

t is a char and vector is an array of ints,so you can't compare them.

But if you're going to make a post in future please also post what errors you get and on which lines they occur.
sorry,since i m new here, i wonder how to make my code show in the line like others post?my error is like wat quirkyusername said: if(t==vector[i])
so wat should i do to compare the two?
you can't, what exactly are you wanting your program to do? You haven't explained it.
yts wrote:
i wonder how to make my code show in the line like others post?


This is how:
[code]PUT YOUR CODE HERE[/code]
Basically my program output will be like this:
Enter the number of times you want to key in:3
numbers[0]=bird
numbers[1]=cat
numbers[2]=dog
Enter a name to search:elephant
the record not found
http://cplusplus.com/reference/algorithm/find/
^I'd do it using this.

As for your error... vector is an array of ints. You're comparing one of its elements to a string. That's not going to work properly unless you overload ==.

-Albatross
Last edited on
Topic archived. No new replies allowed.