Modulo

Hello, I would like some help with my code.
I'm fairly new to C++, so sorry if my code is a little messy/ confusing.
I'm basically trying to create a 'Modulo Calculator' which takes two numbers, orders them, counts down on screen and then displays the answer.

As you will see, I have tried to make the '...' appear at intervals which are not equal, so excuse that!!

Thank you for any help which is available!

Dowzer. :)

(It might help if I actually said what the problem was!! NOOB! So when i run the code, it gives me a random figure. It seems to be thinking that 'y' is equal to some other number. Sorry, it's just really hard to explain! I'm sure if you run the code in a compiler etc you will see what I mean!)

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
#include <iostream>
#include "lukeClearScreen"
using namespace std;

//{ INTS
int firstNum;
int secondNum;

int modulo;

int x;
int y;

int steps = 0;

int firmodsec; // = (firstNum - modulo) / secondNum
//}

//{ STRINGS
string resetChoice;
//}

int main()
{
do{
    clearScreen;


Just to note, 'clearScreen' is what I defined in a separate library, but all it is is: system("cls");

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
61
62
63
64
65
66
67
68
69
    cout << " Modulo Calculator. " << endl<<endl;

    cout << " Please enter two numbers. " << endl<<endl;
    cin >> firstNum;
    cin >> secondNum;

    if (firstNum < secondNum)
    {
      x = firstNum;
      y = secondNum;
    }
    if (firstNum > secondNum)
    {
      x = secondNum;
      y = firstNum;
    }

    modulo = y % x;

    clearScreen;

    cout << "Calculating";
    Sleep(850);
    cout << ".";
    Sleep(1000);
    cout << ".";
    Sleep(1015);
    cout << "." << endl;
    Sleep(1500);

    clearScreen;

    cout << "Your largest number is " << y << ". " << endl;
    Sleep(500);
    cout << "Your smallest number is " << x << ". " <<endl;

    do{
    Sleep(2000);
    cout << y << " - " << x << " = " << (y - x) << endl<<endl;
    y-=x;
    steps+=1;
    }while (y > x);
    Sleep(2000);
    cout << "So, " << x << " goes in to " << (y + x) << " " << steps;

    //{ DECISION ON WHETHER OR NOT TO WRITE 'time' OR 'times'.
    if (steps == 1)
    {
        cout << " time." << endl<<endl;
    }
    if (steps >= 2)
    {
        cout << " times." << endl<<endl;
    }
    //}
    Sleep(2000);
    cout << "This leaves " << modulo << " left over." << endl<<endl;
    Sleep(2000);
    cout << "So, the modulo is " << modulo << ". " << endl<<endl;

    cout << "Reset? (y/n)" << endl<<endl;
    cin >> resetChoice;

}while (resetChoice == "y");

cout << "\n Thank you for using the Modulo Calculator. " << endl;

    return 0;
}
Last edited on
One obvious problem: What happens when firstNum and secondNum are the same?

You're also missing #include<string>

Your program also gives the wrong answer if the numbers are 10 and 20.
i.e.


So, 10 goes in to 20 1 time.

So when i run the code, it gives me a random figure. Which of the many figures appearing on screen is random? A definite test case would be neat (like "input 2 and 3, got 21 here but it should be 10" )

Also I noticed you are never resetting steps in your loop, so it would keep incrementing.
Topic archived. No new replies allowed.