-4

This is my code, the error is as displayed in the title. And in the compiler:

1>c:\users\ägaren\documents\visual studio 2010\projects\yt tutorial hd\yt tutorial hd\main.cpp(26): error C2446: '==' : no conversion from 'const char *' to 'int'

And:

1>c:\users\ägaren\documents\visual studio 2010\projects\yt tutorial   hd\yt tutorial hd\main.cpp(26): error C2040: '==' : 'int' differs in levels of       indirection from 'const char [4]'
#include <iostream>
#include <windows.h>
#include <string> 
using namespace std;


int main()
{
   int gold=600, level=1, exp, damage=10, health=100, mana=100, manaLevel=1;          // Main Vars      
   string name;
   cout << "Hello, what is your name?: ";
   cin >> name; 
   cout << "Welcome " << name << " to the field of war,\nYou will stumble upon many other brave warriors.\nTake care and good luck.\n";
   Sleep(500);

   cout << "You're first task is to eliminate the Orc leader.\n";
   int orc=15, troll=25, mage=50; // Enemy Vars
   int healthpotion=25, manaPotion=25; // Potions vars
   int ifAttack; // if the user chooses to attack or not.
   Sleep(500);
   cout << "Tossan: " << name << " I hereby order you to attack the orc, he is up north.\nThis message will delete in 2.5 seconds.";
   Sleep(2500);
   system("cls"); // 1st screen clear.
   cout << "After heading north you stumble upon the Orc, will you attack him? [y/n]\n";
   cin >> ifAttack;
   if (ifAttack == "yes")

}

the if (ifAttack == "yes") is what is giving me the erros, == is underlined but i cant find how to fix it. c++

Biffen
  • 6,249
  • 6
  • 28
  • 36
Yamiez
  • 39
  • 1
  • 9
  • 8
    `ifAttack` is meant to be the answer to a yes-or-no question. Why did you define it as an `int`, and what do you expect the user to enter at the `[y/n]` prompt? Even if your code worked, you'd reject a response of `y` because it doesn't match `"yes"`. – Keith Thompson Feb 12 '15 at 19:46
  • 1
    ah yes, ifAttack is suppose to be a string right? I remade the code abit at the end it was if (ifAttack == "y") but i changed it to clarify the code abit. Thx :) – Yamiez Feb 12 '15 at 20:00
  • Change `int ifAttack;` to `string ifAttack;`. – R Sahu Feb 12 '15 at 20:00

1 Answers1

1

Declare ifAttack not as an int but like the:

string ifAttack;

You can then read it in just as you were doing with: cin >> ifAttack;

But since you're only really concerned with whether the user entered a 'y' for the first character change your if statement to:

if(ifAttack.front() == 'y' || ifAttack.front() == 'Y')

EDIT:

Filtering user input can be a difficult task. But if you decide that you want to take that on you can compare the whole string rather than just the first character. That way "yellow elephant" does not mean "yes":

if(_stricmp(ifAttack.c_str(), "yes") || _stricmp(ifAttack.c_str(), "y"))

_stricmp is a Microsoft only function: https://msdn.microsoft.com/en-us/library/k59z8dwe.aspx strcasecmp is the Linux equivalent: http://pubs.opengroup.org/onlinepubs/009695399/functions/strcasecmp.html

I would suggest you save filtering your input till the end of your project. It can be tedious. For example the string comparisons above don't match "YES!".

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • I mean as in, erm it will run the code under even if only 1 is true? – Yamiez Feb 12 '15 at 20:04
  • @Yamiez No. `||` means `or`. I'm using it to check case. Remember that 'y' is not the same character as 'Y'. So I'm going to check if the user entered either one in the `if`statement. – Jonathan Mee Feb 12 '15 at 20:05
  • 1
    @JonathanMee That's awesome. Now the user can enter "yellow elephant" and still have their answer taken as a yes. – Emil Laine Feb 12 '15 at 20:08
  • @Yamiez The `else if` won't help you here. If the user entered "yello elephant" it would meet the `if` condition and never make it to the `else if`. I've added an edit to help you match the entire `string` rather than just the first letter. Also if `||` is confusing to you, you might check out this: http://stackoverflow.com/questions/555505/c-alternative-tokens#comment43978668_555524 – Jonathan Mee Feb 12 '15 at 20:28
  • @JonathanMee but it is working for me now, i've added in if else if and else. And if i type yellow elephant i get the cout i made in else :) – Yamiez Feb 12 '15 at 20:48
  • @Yamiez Sounds like this is the solution? If so I'd appreciate an accept. If you had to do a bunch of work past what I have mentioned here, I'd recommend that you post your own answer explaining what you did for posterity. – Jonathan Mee Feb 12 '15 at 22:07
  • @JonathanMee im a bit of a noob on this website, what do you mean by "an accept" :P I already knew how to make yellow elephant or any other sort of words not register as "yes" or "no". It was just a big newbie mistake by me, not using a string! :D – Yamiez Feb 12 '15 at 22:59
  • @Yamiez Just click the empty check box to the left of this answer. That helps flag anyone who stumbles on your answer with a similar problem that this answer fixed your issue. – Jonathan Mee Feb 12 '15 at 23:02
  • [_"Filtering user input can be a difficult task. But in our future, Microsoft and I fight to make it safe. I am Jonathan Mee, author of this answer, and these are my suggestions."_](https://www.youtube.com/watch?v=jPYvwPN1eU8) – Lightness Races in Orbit Feb 12 '15 at 23:13
  • @LightnessRacesinOrbit Don't know how much sarcasm that was laced with but it gave me a good laugh when I clicked on the link :J – Jonathan Mee Feb 12 '15 at 23:16
  • 1
    @JonathanMee: No sarcasm, only playfulness. That first sentence just reminded me of it and I couldn't help myself. – Lightness Races in Orbit Feb 12 '15 at 23:16
  • 1
    @JonathanMee No sorry, didn't even notice I unccepted? – Yamiez Oct 27 '15 at 12:08