-7

I've checked all syntax.

#include <stdlib.h>
#include <stdio.h>

int main(){
  char * response;
  int anger = 0;
  int correct = 0;
  printf("type Dice");
  scanf("%s", response);
  if (response == "Dice"){
    printf("Good Job!");
    correct = 1;
  }
}

I an trying to make a response generator and the is statement isn't working. I tried setting the variable response to the correct answer and the if statement worked so I was thinking that maybe something was wrong with my scanf. I'm a beginner in C.

halfer
  • 19,824
  • 17
  • 99
  • 186
ConnorKC
  • 1
  • 4

2 Answers2

1

You can't use == to compare strings. Use strcmp() instead. If it returns 0, they're identical.

if (strcmp(response,"Dice")==0){
   ...
}
nageeb
  • 2,002
  • 1
  • 13
  • 25
  • Have you even read the comments? – Eugene Sh. May 03 '17 at 22:07
  • Not endorsing the rest of the code, but this was the problem that was preventing it from compiling. – nageeb May 03 '17 at 22:10
  • I don't think it was preventing the compiling. It should have given a bunch of warnings, yes. – Eugene Sh. May 03 '17 at 22:11
  • weird thing is it didn't give me any warnings? – ConnorKC May 03 '17 at 22:22
  • Also I put in the strain cap and I gave me non zero status here is my code#include #include #include int main(){ char * response; int anger = 0; int correct = 0; printf("type Dice"); scanf("%s", &response); int compare = strncmp(response, "Dice", 20); if (compare == 0) printf("Good Job!"); return 0; } – ConnorKC May 03 '17 at 22:24
1

You did char * response. This makes a pointer variable to a character. Right now it is not pointing to any memory(it is some garbage value). scanf stores user input in consecutive memory addresses starting from the one pointed by response. as response is uninitialised, the input may not necessarily be stored on the stack(Don't want that).

Now when you doresponse=="Dice" it doesn't mean anything at all.


Some pretty basic stuff on arrays and pointers and their comparison.

int arr[10];

now arr points to the first member of the array, arr+1 points to second, arr+2 to third and so on. arr[i] is a shorthand way of saying *(arr+i).


String is also an array of characters.

char *str1="Hello";
char *str2="Hello";
if(str1==str2){...}

What the if statement in line three here does is, it compares to pointers, ie it checks whether they both point to the same location. Since this is not true, execution won't go into the if block. What you want to do is compare the strings character by character. There is an inbuilt function in string.h called strcmp() which does that.

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
Parth K
  • 587
  • 5
  • 18
  • 1
    The funny thing that the last example can actually evaluate to `true`, since `str1` and `str2` are pointing to equal string literals and the compiler might very well to decide not to duplicate them and have only one `"Hello"` allocated. – Eugene Sh. May 03 '17 at 22:17
  • You must have got a string literal comparison warning. DO NOT IGNORE THE COMPILER WARNINGS. They are there for a reason – Parth K May 03 '17 at 22:18
  • @EugeneSh. Not sure the compiler would do that since compiler would want to leave scope for change later on in the code – Parth K May 03 '17 at 22:19
  • Yes it will: https://ideone.com/EbivBo .BTW, these should be pointers (fixed it for you..). Compiler doesn't care about future changes. It is optimizing here and now. – Eugene Sh. May 03 '17 at 22:20
  • Well in the program down the flow we are attempting to change any of the two strings. The compiler can possibly make `str1` and `str2` point to same location as otherwise change in one string will change the other – Parth K May 03 '17 at 22:26
  • No! String literals are read only. Attempting to change them is undefined behavior – Eugene Sh. May 03 '17 at 22:26
  • "now arr points to the first member of the array" - Definitively **not**. `arr` is an array, not a pointer! You might want to find out why `arr + 0` actually **does**. – too honest for this site May 03 '17 at 22:40