I 'm not quite sure if i did the right way but i just couldn't get the right value for my codes....can anyone figure out the problem?

for(int x = 0; x<myvector.size();x++)
if(number.compare(0,2,myvector[x],0,2)==0) //compare number(user's input) to the line read from getline
		checknumber = true;		
		cout<<"passing compare++"<<endl;//if same number is found, then 	
                             cout<<myvector[x]<<"value of myvector"<<endl;		

Edited 6 Years Ago by Bigbrain99: n/a

yea so why it isn't working?


and should i use ?

if( number.substr(0,2) == myvector[x].substr(0,2))

Edited 6 Years Ago by Bigbrain99: n/a

>>yea so why it isn't working?

It does work. Since you didn't post the contents of that vector we have no idea what your program is comparing. compare() is case sensitive -- "Now" is not the same thing as "now" or "NOW".

Myself I would use the substr() approach, but compare() is ok too. So it boiles down to personal preference.

int main()
    vector<string> myvector;
    myvector.push_back("Now is the time");
    myvector.push_back("for all good men");
    myvector.push_back("to come to the aid");

    string number = "for";
    int x = 1;
     if( number.compare(0,3,myvector[x],0,3) == 0 )
         cout << "Yes\n";
         cout << "No\n";


Edited 6 Years Ago by Ancient Dragon: n/a

This question has already been answered. Start a new discussion instead.