### trying to create two consectutive #'s where one is either odd and the other even or vice versa

SO far what i've done is below and the else statement isn't working. It almost always gives me a 0.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````#include "stdafx.h" #include #include //for time functions #include //for srand and rand using namespace 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; } }``````
You have a semicolon after you else condition; get rid of it and it should work.

By the way, what is the purpose of the braces on lines 39/41, and 45/47?
Btw to check to see if a number is even or odd you can do:
 ``123`` ``````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.
 ``12345678`` ``````if (e % 2 != 0) { f = 2 * (rand() %5); } else { f = ((rand() % 5) * 2) + 1; }``````
your else statement on line 33
`else ((e == 0) || (e == 2)|| (e == 4) || (e == 6) || (e == 8));`

should be an else if statement
`else if ((e == 0) || (e == 2)|| (e == 4) || (e == 6) || (e == 8))`
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.
I made the changes suggested by the others and it appears to work for me.
 ``123456`` `````` if (e % 2) { f = 2 * (rand() %5); // e is odd } else { f = ((rand() % 5) * 2) + 1; // e is even }``````

Topic archived. No new replies allowed.