if, else, switch

Im trying to create a program that takes two integers (pass-by-reference) and divides int1 by in2. The program needs to error check both integers and needs to make sure int2 does not equal 0 then displays the result of the calculation and the remainder.

Im unclear as to why the printDivison function is not working. Any help is 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
39
40
41
42
43
#include <cstdio>
#include <cmath>

bool getTwoInts(int &numerator, int &denominator);
void printDivision(int numerator, int denominator);
int main()
{
   int numerator, denominator;
   getTwoInts(numerator, denominator);
   printDivision(numerator, denominator);
   return 0;
}

bool getTwoInts(int &numerator, int &denominator))
{
   int num, den;
   int scanfResult;
   printf("Please enter an integer numerator and denominator\n");
   printf("e.g. 12 and 2\n");
   scanfResult = scanf("%d %d", &num, &den);
   if(scanResult == 0) {
      printf("The numerator was garbage, discarding\n");
      scanf("%*s");
      scanf("%*s");
      return false;
   } else if(scanfResult == 1) {
      printf("The denominator was garbage, discarding\n");
      scanf("%*s");
      return false;
   }
   numerator = num;
   denominator = den;
}

void printDivision(int numerator, int denominator)
{
   int result;
   int remainder;
   result = numerator/denominator;
   printf"%d divided by %d is %d\n", numerator, denominator, result);
   remainder = numerator%denominator;
   printf("The remainder is %d", remainder);
}
Are you complaining that it does not compile or that it does not display the correct output?
The ‘printDivision()’ does does not check that the denominator is non-zero before trying anything.


BTW, you are using C I/O in a C++ program. Don’t do that.
Likewise, you have functions doing multiple things. Try to break it down. (For example, write a function that gets a single integer from the user. Then use that function twice.

Finally, some language refinement is helpful. Don’t tell the user their input was “garbage”. Simply state what was required and that the input does not satisfy the requirement:

    That was not an integer. Please input an integer:

If the user fails after two or three tries, you can gracefully quit:

    Input failure. Aborting.

Hope this helps.
"Are you complaining that it does not compile or that it does not display the correct output?"

I am complaining that it does not compile

"BTW, you are using C I/O in a C++ program. Don’t do that."

It is required that I use C I/O for this program

"Likewise, you have functions doing multiple things. Try to break it down. (For example, write a function that gets a single integer from the user. Then use that function twice."

Ok, I will give that a try

"Simply state what was required and that the input does not satisfy the requirement"

Ok thank you




You are missing a open parenthesis on line 40.

When compiling, make sure to crank up the compiler warnings/errors and always look at the first one you see and fix it. Then try again until you get a clean (no warnings/errors) compile.

Hope this helps.
closed account (48T7M4Gy)
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
#include <cstdio>
#include <cmath>

bool getTwoInts(int &numerator, int &denominator);
void printDivision(int numerator, int denominator);

int main()
{
    int numerator, denominator;
    getTwoInts(numerator, denominator);
    printDivision(numerator, denominator);
    
    return 0;
}

bool getTwoInts(int &numerator, int &denominator)
{
    int num, den;
    int scanfResult;
    printf("Please enter an integer numerator and denominator\n");
    printf("e.g. 12 and 2\n");
    scanfResult = scanf("%d %d", &num, &den);
    if(scanfResult == 0) {
        printf("The numerator was garbage, discarding\n");
        scanf("%*s");
        scanf("%*s");
        return false;
    } else if(scanfResult == 1) { // <---
        printf("The denominator was garbage, discarding\n");
        scanf("%*s");
        return false;
    }
    numerator = num;
    denominator = den;
    
    return true; // <--- ????
}

void printDivision(int numerator, int denominator)
{
    int result;
    int remainder;
    result = numerator/denominator;
    printf("%d divided by %d is %d\n", numerator, denominator, result); // <---
    remainder = numerator%denominator;
    printf("The remainder is %d", remainder);
}


Please enter an integer numerator and denominator
e.g. 12 and 2
12 2
12 divided by 2 is 6
The remainder is 0Program ended with exit code: 0


Need a check for zero deminator ... :)
I have gotten the typos and if statement sorted out and it has compiled successfully. Thanks!
Topic archived. No new replies allowed.