Personal project: else/if statement problem

Hello, This is just a personal project and the problem is when ever "rtd" equals to 10 I get both of the if and else statement text is put onto the display, I just want to know the solution to have either the Else or the If be displayed when they are called on.

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
41
42
43
44
45
46
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

	int main()
	{
		int age = 0;
		string name;
		char wait; 
		cout << "type name of person\n";
		cin >> name;
		cout << "characetr is called " << name << endl;

		//adding the age up to 10
		while (age < 10)
		{
			int add = age += 1;
			cout << name << " is now " << age << " \n";
		}

		//generate random int
		srand(static_cast < unsigned int >(time(0)));
		int rnd = rand();
		int rtd = (rnd % 10) + 1;
		if (age = 10)

		{
			cout << name << " is now 10 years old\n";
			int rtd = (rnd % 10) + 1;
			cout << rtd; 
			if (rtd == 10)
			{
				cout << name << " has died";
			}
			else (rtd <= 9);
			{
				cout << name << " has not died";
			}

		}
		cin >> wait;
	return 0;
	}
Line 28, you are constantly setting age to 10, not checking if it is equal to 10. The if will always be true because of that.

if (age == 10), not if (age = 10)

You need to seed the C random number generator only once, move line 25 out of your while loop. A good place is right after you enter main() (line 10).
Thank you So very much, I would have not thought of that but now I know what to look out for. But it Is still giving out both out the if (line 33) and else (line 38) out puts like thus "has not diedhas died" when they are executed. It is only when the top one namely if ( rtd < 9) then both of them trigger but if the "if" statement is not triggered it works fine.

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
41
42
43
44
45
46
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;

int main ()
{
                srand(static_cast < unsigned int >(time(0)));
		int age = 0;
		string name;
		char wait; 
		cout << "type name of person\n";
		cin >> name;
		cout << "characetr is called " << name << endl;

		//adding the age up to 10
		while (age < 10)
		{
			int add = age += 1;
			cout << name << " is now " << age << " \n";
		}

		//generate random int
		int rnd = rand();
		int rtd = (rnd % 10) + 1;
		if (age == 10)

		{
			cout << name << " is now 10 years old\n";
			int rtd =(rnd % 10) + 1;  
			cout << rtd; 
			if (rtd < 9)
			{
				cout << name << "has not died";
			
			}
			else(rtd = 10);
			{
			    cout << name << "has died";
			
			}
		}

  return 0;
}
Last edited on
Look at line 38. What's wrong? Multiple problems with it.
Topic archived. No new replies allowed.