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.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
usingnamespace 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 < unsignedint >(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;
}
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.
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main ()
{
srand(static_cast < unsignedint >(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;
}