Reputation: 51
When I run the program and answer "yes", it tells me that I am wrong. Does anybody know a way to execute this type of program?
#include <string.h>
#include <stdio.h>
int strcmp(const char *str1,const char *str2 );
// I am trying to make a program that asks the user to input an answer
int main()
{
char answer[20];
printf("Can birds fly?\n");
scanf("%s", &answer[20]);
if(strcmp(answer, "yes") == 0)
{
printf("You are right");
}else
{
printf("You are worng");
}
return 0;
}
Upvotes: 2
Views: 214
Reputation: 67090
Change this line:
scanf("%s", &answer[20]);
to:
scanf("%s", answer);
You have to pass to scanf
the address where you want to put the string, with answer[20]
you pick the value of the 21th character in the string (undefined because the string is 20 characters only) and then take a pointer to it (garbage, you may even get an access violation).
Upvotes: 5