The specific question is
why won't my if statements work? |
The answer is that they are working, the problem is much deeper. This just proves once again, that programmers are generally bad at guessing where the problems lie.
@nickeeromo, please don't take offence, I'm sure you're here to learn as well. It is often hard for a beginner to distinguish between good and bad answers.
Obito may have thought nickerromo's answer helpful, but TheIdeasMan is right, nickerromo's code is not helpful in the least. By his own admission, he doesn't know why it "works". In reality, it's no better than the original code, and doesn't actually work, just creates the illusion that it does. His explanation also shows he doesn't understand how scanf works.
Best advice is TheIdeasMan 's original reply, also Dhayden's.
The real issue for Obito can be illustrated thus -
Prompt user
Read user input
Process user input
Respond based on user input
A crude solution in C might follow the pattern below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
#include <stdio.h>
#include <stdlib.h>
int main()
{
const int MAX = 256;
char *in = calloc(sizeof(char), MAX); /* using calloc so string is initialised with zeros, hence null terminated */
if (NULL == in)
{
/* exception processing here */
}
/* prompt the user - printf does return a value, which we're ignoring */
printf("Hi, I am a chatbot. I am a prototype, type words properly:\n");
/* read the input - note that we're ignoring the return value of scanf, just as we did with printf */
scanf("%s", in); /* the user's input will be stored in "in" note that in is a pointer */
if (0 == strcmp(in, "hi")) /* Process the user's response, was it "hi" */
{
printf("Hello\n"); /* respond to user - user typed "hi" */
}
else
{
printf("The user typed %s\n", in); /* respond to user - user typed something other than "hi" */
}
free(in);
return 0;
}
|
C++ idea by (i23CM4Gy) is even better, but given this odd user-name and closed account, I am also forced to consider the likelihood there's some trolling going on