Problem with string comparison

closed account (yh54izwU)
This is just a snippet of the code but I can't figure out why it is comparing the 2 strings in the vector. The code compiles it just skips the whole if statement. Is this logic or did I just do something wrong. Any help would be much appreciated. Thanks

for(int x=0;x<nameslong;x++){
int pullme=rand()%25;
pullednames.insert(pullednames.begin()+x, nameslist[pullme]);
int nameincrem=x+1;
cout<<pullednames[x]<<"\n";

do{
nameincrem++; //from the line below to...//
bool nametest= (pullednames[x]==pullednames[nameincrem]);

if(nametest==1){
pullme=0;
pullme=rand()%25;
pullednames.insert(pullednames.begin()+x, nameslist[pullme]);
cout<<"This is a if inside do test";
nameincrem=x+1;
} //here seems to be the problem//
}while(nameincrem<nameslong && pullednames[nameincrem]!=pullednames[x]);

pullme=0;
}
If nametest is a bool, why not just do if(nametest)
closed account (yh54izwU)
That didn't work either for some reason
Well in a do while loop you will always execute the do part at least once but your do section has another condition that needs to be met you have you made sure that your nametest is 1? If it as bool variable you should do what clanmjc said and compare it because it might not always return a 1 because it only needs to return a nonzero to be true, I believe.
If nameslong is equal to or greater than pullednames.size() you're going to have some out of bound subscript operations. It's not clear from your code where the mistake might be as we're missing a boatload of context, such as what the code is supposed to be doing.

If you're trying to build a list of 'random' unique names here, you're not there yet.


pullednames.insert(pullednames.begin()+x, nameslist[pullme]);

That's a rather odd way to use insert on a vector.


Topic archived. No new replies allowed.