Number Changer

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).
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <ctime>
using namespace 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;
       }
    else if (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.";
          }
    else if (a + 1 == 2) {
          cout << input << " will now be divided by a random number of times." << endl;
          cout << divide << " is the new number.";
          }
    else if (a + 1 == 3) {
          cout << input << " will now be increased by a random number." << endl;
          cout << add << " is the new number.";
          }
    else if (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.
Last edited on
1. #include <cstdlib>

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.
1
2
3
4
5
    if ((a + 1) == 1) {
        ...
        }
    else if ((a + 1) == 2) {
        ...
(You could have done that with a switch statement.)

4. Lines 54..60 should be something like:
1
2
3
4
5
        }
    system("PAUSE");
    }
return (0);
}
BTW. Watch your identations. You goofed here because your main while loop's contents are not properly indented.

Hope this helps.

[edit] BTW, nice start!
Last edited on
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.

Sorry about poor English. :)
Last edited on
In a C++ program, use <ctime> not <time.h>

I'm posting again because I forgot to mention:

5. Lines 19 and 36: e can be zero, which causes a divide-by-zero fault.
Duoas: Doesn't == have a lower precedence than +?

bluezor: Wouldn't it be easier to add 1 to 'a' in the initialization? Or compare it to 0, 1, 2, and 3?
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!
@bluezor
Something like
rand_int=rand()%7+1
should do it.

a switch-case statement is like this:
switch(some_variable)
{

case someconstant:
statement1;
break;

case someotherconstant:
statement2;
break;

.....

default:
statement;
break;
}
this is the same as
if(some_variable==someconstant)
{
statement1;
}

else if(some_variable==someotherconstant)
{
statement2;
}

......

else
{
statement;
}

easy enough and some times helpful, but not really necessary.
Last edited on
Doesn't == have a lower precedence than +?

Yoinks! So it does.
Topic archived. No new replies allowed.