code transferred to mobile device behaves differently.

hi i wrote this on my pc but when i transferred it to my phone and compiled it on c4droid the modulo function hangs. can anybody explain why it works fine on the pc. thanks.

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  #include <iostream>
#include <cmath>

using namespace std;

enum BOOL { FALSE,TRUE };

int Modulo();
int Add();
int Exponent();

int main()
{

BOOL exit = FALSE;  // important

for (;;)            // important

{
int choice;

cout << "Choose (1) Modulo (2) Add (3) Exponent (4) Quit.\n";

cin >> choice;

switch(choice)
{
case (1):
Modulo();
break;
case (2):
Add();
break;
case (3):
Exponent();
break;
case (4):
exit = TRUE;          // important
break;
default : cout << "Please enter another number \n";
}
if (exit)
break;                // important
}
cout << "The program has now ended\n.";
}

int Modulo()
{

int a,b,c;

cout << "What times table would you like\n";

cin >> b;

while (a < b * 10)       // important
{

++a;

if (a % b == 0)          // important
{

cout << a << "\n";

}

}

}


int Add()
{
int d,e,f;

cout << "Enter a number to add\n";

cin >> d;

cout << "Enter another number to add to that other number \n";

cin >> e;

f = d+e;

cout << "The answer is " << f << " \n";
}

int Exponent()
{
int g,h,i;

cout << "Enter the digit to be exponentiated.\n";

cin >> g;

cout << "Enter the exponent\n";

cin >> h;

i=std::pow(g,h);             // important

cout << "The answer is " << i << endl;




}
while (a < b * 10) // important
a is not initialized and might a bigger than b * 10 resulting in an endless loop.
Some tips:

You don't have to case (1): do that. You can simply write case 1:.

You have written your functions like int Modulo() { but never returned any value. If returning a value is not needed, change their function types to void.

You have never used variable c inside Modulo().

Variable a is not initialized and due to that it could be any value. Let's say that 9999 and this can potentially cause some problems.

You can join assignments and declarations. Instead of:

1
2
int i;
i = std::pow(g, h);


You can simply write const int i = pow(g,h); (since you have written using namespace std; you don't have to type std:: before pow(g,h) again.) (I've also written const there because your answer is a constant value.)

Instead of for (;;) you can type while(true). That's just a preference.

Here is a prettified and tiny little bit improved version of your code:

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <cmath>
using namespace std;

enum BOOL { FALSE, TRUE };

void modulo();
void add();
void exponent();

int main() {
  BOOL exit = FALSE;  // important

  while(true)  // important
  {
    int choice;

    cout << "(1) Modulo, (2) Add, (3) Exponent, (4) Quit.\nChoose: ";
    cin >> choice;

    switch (choice) {
      case 1:
        modulo();
        break;
      case 2:
        add();
        break;
      case 3:
        exponent();
        break;
      case 4:
        exit = TRUE;  // important
        break;
      default:
        cout << "Please enter another number \n";
    }
    if (exit) break;  // important
  }
  cout << "The program has now ended\n.";
}

void modulo() {
  int a = 0, b;

  cout << "What times table would you like\n";
  cin >> b;

  while (a < b * 10)  // important
  {
    ++a;

    if (a % b == 0)  // important
    {
      cout << a << "\n\n";
    }
  }
}

void add() {
  int d, e;

  cout << "Enter a number to add\n";
  cin >> d;

  cout << "Enter another number to add to that other number \n";
  cin >> e;

  const int f = d + e;
  cout << "The answer is " << f << " \n\n";
}

void exponent() {
  int g, h;

  cout << "Enter the digit to be exponentiated.\n";
  cin >> g;

  cout << "Enter the exponent\n";
  cin >> h;

  const int i = pow(g, h);  // important
  cout << "The answer is " << i << "\n\n";
}


If you have any further questions, do ask, don't hesitate.
Last edited on
Topic archived. No new replies allowed.