I need to give a condition to a string but can't.

i'm working on a uni project that asks me to create a game, i chose to create an rpg game with heros and each hero has three type of weapon and each type had three specific weapons to use. i've done everything correctly except to limit the selection of the specific weapons since i don't want the user to input numbers, instead i asked him to write the name of weapon, but when i write down if(stringname != "weaponname" || stringname != "weaponname2") it doesn't work. can anyone please help me through this ? and when i ask the user to input his choice (ex : cin >> weappick;) it gives an error: no operator ">>" matches these operands.
1
2
3
4
5
6
7
8
9
do{
      cin >> weappick; 
		  if(weappick != "excalibur" || weappick != "Excalibur" || 
                    weappick != "Sanje"  || weappick != "sanje" )
                         cout << "Please make a valid selection";
   } while(weappick != "excalibur" ||  weappick != "Excalibur" || 
                    weappick != "Sanje"  || weappick != "sanje" );

 
and it also gives me an error:these operands are incompatible (std::string *'' and const char *" how can i possibly solve this im brainwrecked)
Last edited on
I solved the part where it says no operator ">>" matches these operands. but i still can't figure out error:these operands are incompatible (std::string *'' and const char *") a little advice would be appreciated, thanks.
You didn't give enough information to tell what the problem is.
But how about:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool in(string str, vector<string>& lst) {
    for (const auto& s: lst) {
        size_t i = 0;
        for (char c: s)
            if (c != tolower(str[i++])) // case-insensitive compare
                break;
        if (i == str.size())
            return true;
    }
    return false;
}

vector<string> weaplist{"excalibur", "sanje"};

while (cin >> weappick) {
    if (in(weappick, weaplist))
        break;
    cout << "Please make a valid selection\n";
}

Hello CharbelHaddad,

When I look at the bit of code you posted in the if condition and the while condition you are using the logical or. When each side is based on a "!=" the logical and tends to work better. Sometimes the logical or will work, but from what I have seen this is rare.

these operands are incompatible (std::string *'' and const char *")
.
With out knowing how the variables are defined the error message has little meaning other than what it says. Unless the compiler is calling the first parameter a "std::string *" I would guess that you are trying to compare or deal with a "std::string" and either a C style character array or a pointer to a C string. If you could post the code that this error is about and the exact compiler message that would help.

Hope that helps,

Andy

Hey andy,

Well My coding is almost 160 lines long and to be honest I don't think you'd want to see that :P, I think i am using a c style character array. to let the user define the string i want, i used
1
2
for(i=0;i<1;i++)
        cin >> weappick;

and that worked fine when asking for a name and it worked too on inputting the weapon name, but i can't make a condition to force the user to enter only the weapon's actual name.

Last edited on
Hello CharbelHaddad,

160 lines is not as much as some I have seen and worked on.

The problem here is you are asking to fix a problem in one place that may actually start somewhere else.

In your last example cin >> weappick; I have no idea how "weappick" is defined.

And sometimes it is easier for me to load the program and follow it thorough to see what is happening.

Andy
Hi,

Instead of all the drama with trying to match strings, consider using a switch statement with a single char or a number as cases. Also consider the use of enums with the switch as well.

Google :+)
Well My coding is almost 160 lines long and to be honest I don't think you'd want to see that :P

I am afraid without seeing the whole picture we can't help much.

Topic archived. No new replies allowed.