how to make this into a function

So I made this program to find remainders without using the % operator. I'm still pretty new to C++ and I'm wondering how to go about doing this using remainder as a seperate function instead?? I guess I'm asking how to get the program to pull the seperate function of remainder and running it along with the program. Any helps greatly appreciated! 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
#include <iostream>
#include <cstdlib>

using namespace std;

int main(int argc, char *argv[])
{

 int a; 
 int b; 
 int c; 

cout << "Enter two numbers, seperated by a space, you wish to divide." << endl;
 cout << "Example: (3 15) will return 3/15= and its remainder if there is one!" << endl;
 cout << endl;
 cin >> a >> b;
 c = a - ((a/b)*b);
 cout << endl;
 
if (c == 0)
{ 
cout << a << " / " << b << " = " << ( a / b ) << endl;
cout << "No Remainder" << endl;
}
 if (c != 0) 
{ 
cout << a << " / " << b << " = " << ( a / b ) << endl;
cout << "Remainder= " << c << endl;
} 
cout << endl; 
 cout << "Check my math with the equation below!!" << endl;
 cout << endl;
 cout << ( a / b ) << " * " << b << " + the remainder of " << c << "=" << ( a / b ) * b + c << endl;
 cout << endl;

 system("PAUSE"); // pause before exit
 return EXIT_SUCCESS; // hit any key to exit
} 
Last edited on
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
#include <iostream>
#include <cstdlib>

using namespace std;

void CalRemain(int a, int b, int& c);
void DoubleCheck(int a, int b, int c);

int main()
{
     int a; //num1
     int b; //num2
     int c; //remain
    
     cout << "Enter two numbers, seperated by a space, you wish to divide." 
          << endl
          << "Example: (3 15) will return 3/15= and its remainder if there is one!" 
          << endl;
          
     //take value of a and b 
     cin >> a >> b;
     
     //Call this function
     CalRemain(a, b, c);
     cout << c << endl;
     
     //Call this double check function
     DoubleCheck(a, b, c);
     
     cin.get();
     cin.get();
     return 0;
} 

//Calculate the remainder of the 2
void CalRemain(int a, int b, int& c)
{
    c = a - ((a/b)*b);
}

//Double check if the calculation was correct
void DoubleCheck(int a, int b, int c)
{
     if (c == 0)
     { 
        cout << a << " / " << b << " = " << ( a / b ) << endl;
        cout << "No Remainder" << endl;
     }
     
     if (c != 0) 
     { 
        cout << a << " / " << b << " = " << ( a / b ) << endl;
        cout << "Remainder= " << c << endl;
     } 
     
     cout << endl; 
     cout << "Check my math with the equation below!!" << endl;
     cout << endl;
     cout << ( a / b ) << " * " << b << " + the remainder of " << c << "=" << ( a / b ) * b + c << endl;
     cout << endl;
}



I was told by many others to not use
system("PAUSE");
because it is a bad habit in C++ programming.

Hope this helps!
Topic archived. No new replies allowed.