why won't my if statements work?

I'm trying to make a chatbot using
if
else
printf
scanf

in theory I think it can be done, as I don't see why not. It's not meant to be amazing or anything, but I think it can be done.


For reasons I don't understand, the if statements don't seem to work. If I type the phrases "hi" nothing happens.

if I type anything else, the else statement kicks in (as it should)

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
31
 

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

int main(void) {

	printf("Hi,I am a chatbot.  I am a prototype, type words properly:\n");

	if(scanf("hi\n")){
	printf("hello\n");
	}
	if(scanf("how's it going?\n")){
	printf("pretty good I suppose.Just chilling out\n");

	}
	if(scanf("what's up?\n")){
	printf("nothing really, just bored\n");


	}
	if(scanf("are you intelligent?\n")){
	printf("Yes, why would I not be?\n");

	}
	else(printf("I don't understand what you mean i'm a prototype"));{

	}

}
Hi,

I think you need to review what the scanf function actually does.

http://www.cplusplus.com/reference/cstdio/scanf/
Scanf returns the number of items successfully extracted. Since you aren't extracting any items (with a % format specifier), it always returns 0.

I think what you should do is read an entire line of input and then compare it to the strings.

You're using the C library here. Can you use C++ streams and strings instead? It will make the code easier and safer (no worry about buffer overruns).
Looks like scanf is a function of type int. So I played around with it a little. Don't ask me why this works, but try this out:

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
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
#include <stdlib.h>

int main(void) {

//Note: you must enter whatever is in the first scanf's string first before doing anything else.
//First, just hit enter.  THEN submit "hi\n", etc.  Have fun with whatever information
//this gives you!!

	printf("Hi,I am a chatbot.  I am a prototype, type words properly:\n");

	if(!scanf("\n")){  // note I use !scanf here.
	printf("hello\n");
	}

	if(!scanf("hi\n")){
	
	printf("pretty good I suppose.Just chilling out\n");

	}

	if(!scanf("how's it going?\n")){
	
	printf("nothing really, just bored\n");	

	}

	if(!scanf("what's up?\n")){
	
	printf("Yes, why would I not be?\n");

	}
	else(printf("I don't understand what you mean i'm a prototype"));{

	}
	if(!scanf("are you intelligent?\n")){};

	system("pause");

}


What I CAN tell you is that scanf is supposed to take 2 arguments at least, the first is the format you are accepting (as a string) coming from the input stream, and the second is the variablename or address where that data will be stored. Not sure what scanf is returning (again, return type of the function is int), but it may just return if it was successful at doing it's job?

It is interesting how the program above seems to function: you enter what is in the next statement, then it seems to backtrack to the inside of the previous if statement's output. Crazy.

P.S.
I am fully aware that system ("pause") should not be used in a professional environment, but it is recognized with no further libraries added.
Just wondering whether this is a troll topic?

Why else bother writing such ridiculous code?
Why else bother writing such ridiculous code?

Because this is a beginners forum? People come here for help, not insults.
closed account (i23CM4Gy)
Below is a simple program that takes user input and compares it to something, then says "yes" if it matches. Adapt it to your own use if you want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
#include <iostream>

int main()
{
    std::string input;
    std::string hello = "hello";

    std::cout << "Input: ";
    std::cin >> input;

    if (input == hello)
    {
        std::cout << "yes" << "\n";
    }
    else
    {
        std::cout << "no" << "\n";
    }

    return 0;
}


To turn it in to a chatbot-type thing, you're going to need a loop.
Last edited on
I would like to thank you all for your helpful suggestions, I am not trolling but I just wanted to see if a basic chatbot could be done. Nothing serious, but like a few sentences and replies, using if statements to prepare for each condition appropriately.

Special thanks to alsp and nickerromo , what both of you posted are both very useful in coding this in a way that it actually works. I may mess around with this further, but you've all given me somewhere to start from. Thank you!
dhayden wrote:
People come here for help, not insults.


Hi,

Unfortunately that is not always the case, I have come across a number of situations where people are trolling and are not genuinely looking for information at all.

The code by nickerromo IMO, was not helpful at all and completely misses the point, as are number of other posts by this user.

So that's it from me, hopefully this topic won't turn into pages & pages of nonsense.
The code by nickerromo IMO, was not helpful at all and completely misses the point, as are number of other posts by this user.


Buddy, since I got thanked, you are the one missing the point. The original post had a very specific question, and I can see where the user was coming from - experimenting with the scanf function mainly. Here it is again in case you missed it:

1
2
3
4
5
6
7
I'm trying to make a chatbot using 
if
else
printf
scanf

in theory I think it can be done, as I don't see why not.


Looking at the code, I knew immediately what was being attempted, and gave it a real effort in Visual Studio on my own, as my example showed. We are beginners. We don't have to make sense.
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
(sigh) OK, the last post I will make on this topic.

I ASSUME that everyone who has EVER seen scanf in any program ever will know how it is really intended to be used - the proper format etc. If I was wrong, I am sorry. I also assumed that if the user is using C functions in a C++ language, he further knows the proper way to use scanf. I also assumed (and no I don't need any crass comment about what assuming does) that the original user was going for the format "if this, do this", plain and simple.

Mine works in a simple way. Works as in "produces the desired result (using the format presented)" in a very roundabout way. Since it only sort of works, I was intending the user to further investigate why my version worked. Would anybody care to dissect how my sort-of-solution does what it does? I think that would be most helpful.
Topic archived. No new replies allowed.