#include "stdafx.h"
#include<iostream>
#include<ctime> //for time functions
#include<cstdlib> //for srand and rand
usingnamespace std;
int _tmain(int argc, _TCHAR* argv[])
{
srand(time(0));
int a;
int b;
int c;
int d;
int e = (rand() %10); // yields random # 0 - 9
int f = 0;
cout << "Welcome to create that phone number" << endl;
cout << "Please enter a number 1-9 that is the first number" << endl;
cin >> a;
cout << "Please enter a number 1-9 that is the second number" << endl;
cin >> b;
cout << "Please enter a number 1-9 that is the third number" << endl;
cin >> c;
cout << "Please enter a number 1-9 that is the fourth number" << endl;
cin >> d;
if ((e == 1) || (e == 3) || (e == 5) || (e == 7) || (e == 9))
{
int f = 2 * (rand() %5);
}
else ((e == 0) || (e == 2)|| (e == 4) || (e == 6) || (e == 8));
{
int f = ((rand() % 5) * 2) + 1;
}
{
cout << "Phone number is" << a << b << c << d << e << f << endl;
}
system("pause");
{
return 0;
}
}
Btw to check to see if a number is even or odd you can do:
1 2 3
if( e % 2 != 0) // number is odd since there is a remainder from dividing by 2
if(e % 2 == 0) // number is even since dividing by 2 yields no remainder
Also the reason f is always 0 is that you are redeclaring f inside of your if else statements which exist only in that block. So f never gets changed from 0.
1 2 3 4 5 6 7 8
if (e % 2 != 0)
{
f = 2 * (rand() %5);
}
else
{
f = ((rand() % 5) * 2) + 1;
}
Zhuge.
I've tried your suggestion but that didn't work. I did change my int f to a global variable and that doesn't work either. I am always getting a zero for my last number.
Scorpic.
I changed my f to a global variable and thanks for the shortcuts. I knew something existed but I knew the above would work but there had to be something simpler.