-2

I've got this code:

char level;

printf("Please choose a level!\nEasy\nMedium\nHard\n\n");
scanf("%s", &level);
do{
    if((level=="easy") || (level=="Easy"))
    {
        printf("You have choosen the easy level, so you have 2 powerups per time. If you want to use them, please type P or p!\n");
        break;
    }

and this warning: 17 15 C:\Users\katerina\Desktop\levels.c [Warning] comparison between pointer and integer

What can I do?

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • 5
    This is a quite rudimentary mistake, especially when coming from other languages. Consider taking a decent C tutorial or course. – Bart Friederichs Mar 12 '16 at 09:39
  • Pro tip: see [this excellent list of recommended books on C](http://stackoverflow.com/q/562303/253056) and pick one. – Paul R Mar 12 '16 at 09:47

3 Answers3

2

In C, there is no string comparison operator ==. You have to use the strncmp function:

if (strncmp("easy", level, 4) == 0) {
}

Also, your variable level is incorrectly defined. It should be a char * (don't forget to allocate memory) or char[].

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
2

Two problems: firstly level needs to be a string, not a char. Secondly you need to use strcmp to compare strings, not ==.

Paul R
  • 208,748
  • 37
  • 389
  • 560
1

You cannot compare string in C using ==. Use strcmp:

if (strcmp ("easy", level) == 0)

Also, if you want your code to be safer, use strncmp as Bart mentioned.


Another thing you have to notice, is that declaring level like

char level

makes only space for one character to fit. ("easy" won't fit). I think you want to declare a string:

char level [length];

Just replace 'length' by the length of the string you want.

Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
  • Thanks Ashish!! I appreciate a lot :) – Katerina Kakari Mar 12 '16 at 09:47
  • @AshishAhujaツ: please don't beg for up-votes or answer acceptance - it's undignified, especially as your answer is is essentially the same as the first two answers. – Paul R Mar 12 '16 at 09:51
  • @PaulR, I'm not begging. I never do this, unless I see a new user saying that it worked. They do not know the process of acceptation on SO, and thus I am just informing them. I wrote: accept my answer **if** it solved everything. Pay attention to the if. Also, before the user said it worked, I never even asked it do like accpetion of the answer. – Box Box Box Box Mar 12 '16 at 09:54
  • @PaulR, also please define your wording of 'begging'. As per me, begging will be to ask a user to accept the answer, without any reason, whether it worked or not: _please accept my answer, I have put enough effort in it. It doesn't matter whether it works for you or not, it works for me_ or something like that. This is a correct answer, and it isn't wrong for asking for acception of the answer, if the user is coming and saying _it worked. thank you_. Also, if you see carefully, I was basically just informing the user of acception, and saying to accept the answer if it works. – Box Box Box Box Mar 12 '16 at 10:01
  • @PaulR, it's common sense that if my answer hadn't worked, the user wouldn't have accepted it, and accepted the answer that worked. – Box Box Box Box Mar 12 '16 at 10:06