How to use the If Statement this way?

Write your question here.

I'm new to programming because I want to make a game myself. I was doing the "hello world!" beginner exercise, it the turned to a project the Ive been building up for. I was trying to make the program/AI to communicate in a way that when it ask something like "how are you?", and if i say "good", i would say "cool". I also added a random generater that works with ctime that pick a number 1 to 3 so that if i say "good", it will either reply with one of the three phrases.

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 <iostream> 
#include <string>
#include <random>
#include <ctime>


using namespace std;


void delay(long ms) // delay for (ms) milliseconds 
{ 
std::clock_t nclocks = (std::clock_t)(ms * 0.001 * CLOCKS_PER_SEC); 
std::clock_t t0 = std::clock(); 
while (nclocks > std::clock() - t0) 
{ /* do nothing */ } 
} 

int main()

{
	/* Setup */

	string Myname00;
	string Myage00;
	string Answer00;
	mt19937 randomgenerater00(time(0));
	uniform_int_distribution<int> diceRoll00(1-3);

	cout << "Hello" << endl;
	delay(5000); // delay 5 seconds
	
	if(diceRoll00 == 1) {
	std::cout << "What's up? Are you going ok?" << std::endl; 
	}
	
	cin >> Answer00;

	system("PAUSE");
	return 0;
Last edited on
What exactly is your problem?
closed account (48T7M4Gy)
http://www.cplusplus.com/reference/cstdlib/rand/

Try reading this for a start. There's a sample program you can run by pressing on the wheel at the top right. It incorporates some if not all of concepts you are chasing.
@ coder777

I kept getting errors
closed account (E0p9LyTq)
BadBadS wrote:
I kept getting errors

And the errors are?

Remember we are not in the room so can't see the output you get. Posting the errors here would help us be able to help you.

PLEASE, use code tags when you post code. It makes your source easier to read. You can go back and change your original post to include code tags.
http://www.cplusplus.com/articles/jEywvCM9/
What errors? Please post the exact text of your errors.

Please use code tags correctly. You're missing a closing code tag. Your code must be followed by
[/code]

Sorry for the inconvience, I hope this comment is more proper

I kept getting "";" is expected" on the cout in the If statements, and kept getting "no operator "==" matches these operands"

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

        string Myname00;
	string Myage00;
	string Answer00;
	mt19937 randomgenerater00(time(NULL));
	uniform_real_distribution<int> Diceroll00(1,3);

	cout << "Hello" << endl;
	delay(5000); // delay 5 seconds
	
	std::cout << "What's up? Are you going ok?" << std::endl; 
	
	if (Answer00 == "Good" || "I'm Good")
	{ Diceroll00 == 1 cout << "Great!" << endl; 
	}
	
	if (Answer00 == "Good" || "I'm Good")
	{ Diceroll00 == 2 cout << "That's good!" << endl; 
	}
	
	if (Answer00 == "Good" || "I'm Good")
	{ Diceroll00 == 3 cout << "Nice!" << endl; 
	}
	
	cin >> Answer00;

	system("PAUSE");
	return 0;

}
Line 13, 17, 21: C++ does not support implied left hand side in conditionals. You must fully specify the conditions.
Example:
if (ans == 'Y' || 'y') evaluates as if ((ans == 'Y') || ('y'))

('y') always evaluates to 1 (true), therefore the if statement is always true.

Line 14, 18, 22: What are these lines supposed to be doing?
Diceroll00 == 1 looks like a conditional, but it's not enclosed in (). Did you mean to use the assignment operator (=)?
13
14
15
16
  if (Answer00 == "Good" || Answer00 == "I'm Good")
    { number = Diceroll00(randomgenerator00);  // Corrected
        cout << "Great!" << endl; 
    }

edit: corrected Diceroll00 to pass randomgenerator00.
Last edited on

Line 14, 18, 22: What are these lines supposed to be doing?
Diceroll00 == 1 looks like a conditional, but it's not enclosed in (). Did you mean to use the assignment operator (=)?


I was trying to use the random number generator to make the AI respond with one of the choices out of the three phrases, I dont want the AI to respond with the same response everytime.
So your problem is solved now?
closed account 5a8Ym39o6 wrote:
So your problem is solved now?


Sorry i still didn't get it to work.
closed account (E0p9LyTq)
Line 25 (cin >> Answer00;) should come after line 11, not after all your if statements.
FurryGuy wrote:
Line 25 (cin >> Answer00;) should come after line 11, not after all your if statements.


Thanks, I missed that lol

but I'll just start over with a different stradegy, including trying out default_random_engine or the link kemort brought.

http://www.cplusplus.com/reference/cstdlib/rand/
closed account (E0p9LyTq)
I would recommend highly using the C++ <random> library instead of the C library rand() function. :) rand() has...."documented problems."

You are already using a good C++ random engine, mt1997. I'd say stick with that one. You might run into problems with default_random_engine, depending on what compiler you use. GCC has problems with that engine, even with the latest version (6.10). default_random_engine in GCC gives the same non-random series.
Ok, will do.
closed account (E0p9LyTq)
I noticed you were using <ctime>. Worth a look is the C++ <chrono> library instead. :)
Topic archived. No new replies allowed.