0

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 	
				break;
				}
				else 							
				no++;				           
                             cout<<myvector[x]<<"value of myvector"<<endl;		
				}

Edited by Bigbrain99: n/a

2
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by Bigbrain99
0

yea so why it isn't working?

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

and should i use ?

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

Edited by Bigbrain99: n/a

0

>>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";
     else
         cout << "No\n";

}

Edited by Ancient Dragon: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.