This program will make you enter a number. If that number is even, it'll print it is even and then either multiply, add, subtract or divide a number (randomly) from it. If it's odd, it'll print it is odd and add one to make it even. Then as before, multiply, add, subtract or divide a number (randomly).
#include <iostream>
#include <ctime>
usingnamespace std;
int main()
{
while (true) {
system ("CLS");
cin.clear();
int input;
int a, b, c, d, e;
int multiply, add, subtract, divide;
srand(time(0));
a = rand()%4;
b = rand()%5;
c = rand()%6;
d = rand()%7;
e = rand()%8;
cout << "Enter a number.";
cin >> input;
if (input % 2 == 0) {
cout << input << " is an even number." << endl;
}
elseif (input % 2 != 0) {
cout << input << " is an odd number." << endl;
cout << "Changing it into an even number..." << endl;
cout << input << " has now changed into " << input + 1 << " ." << endl;
cout << input + 1 << " is now an even number." << endl;
multiply = input * b;
add = input + c;
subtract = input - d;
divide = input / e;
if (a + 1 == 1) {
cout << input << " will now be multiplied by a random number of times." << endl;
cout << multiply << " is the new number.";
}
elseif (a + 1 == 2) {
cout << input << " will now be divided by a random number of times." << endl;
cout << divide << " is the new number.";
}
elseif (a + 1 == 3) {
cout << input << " will now be increased by a random number." << endl;
cout << add << " is the new number.";
}
elseif (a + 1 == 4) {
cout << input << " will now be decreased by a random number." << endl;
cout << subtract << " is the new number.";
}
system("PAUSE");
return (0);
}
}
}
I try running it, but it always does a == 1 only (multiply) and not the others. It also can only multiply by 1, instead of 1 to 4.
2. If you tell the user that you are changing input into an even number, you ought to actually do it.
1 2 3 4 5
cout << "Changing it into an even number..." << endl;
cout << input;
input++;
cout << " has now changed into " << input << " ." <<endl;
cout << input << " is now an even number." << endl;
3. Operator precedence rules will bite you. I always avoid the problem by using parentheses liberally.
i think you should use <time.h> instead of <ctime>.
And i m not sure but i think there is another problem. If number is even nothing is happened. If it is a problem it is because of all the process is in the borders of first "else if" which is about the odd numbers. And if number is even the the program goes to the startpoint. If you want to change this you should cut the "}" sign at 57th line and paste it to the 32th.
Hello. Thanks for your help Duoas and anyone else! =)
Duoas, how do I make the randoms something like 1-7 instead of 0-7? And do you mind explaining how Switch works? I've read it in the tutorial, but I don't really get it.
Thanks!