programming class

I'm having an error in my C++ class. I can't seem to get the division to work properly. I keep getting strange errors.
1
2
3
4
5
6
7
8
9
10
11
12
else if (oper == '/' || oper == '%')
    {
        if (bb==0)
            cout << "\n undefined";

        else
            aa=a;
            bb=b;
            c = a /b;
            cout << "\n division" << c << setprecision (3);
            cout << "\n remainder" << a % b;
    }


I already declared the int and double. I also have a set precision section to prevent infinite decimals. I keep getting crazy inaccurate answers for division like "-99972"". I also want to remove the scientific notions
Not enough information.

You haven't shown the declarations of any of your variables.
If a and b are ints, then you're doing integer division and you;re not going to get the right answer.

BTW, only aa=a; is within the scope of the else.

Last edited on
here you go, I wanted to avoid posting the whole thing so my professor doesn't think I'm plagiarizing but whatever. Either she won't see this or she will and I'll explain it.


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
#include <iostream>   // input and output
#include <iomanip>    // manipulator that allows the user to use setprecision
using namespace std;

int main ()
{
int a;      //declaration of 2 integers
int b;
double aa;  //declaration of two double values. They can hold both integers and floats
double bb;  //used two aa's as an easy way to remember double=two letters
double c;
char oper;

cout << "\nEnter first number" <<endl ;      /* prompt to enter first and second numbers */
cin >> aa ;
cout << "\nEnter second number" <<endl ;
cin >> bb;
cout << "\nChoose a mathematical operator";     //prompt for operator
cin >> oper;

    if (oper == '*' || oper == 'x')       // multiple options for operators
        {
            cout << "\nThe product of the two numbers is\t\n" ;
            cout << aa*bb;
        }
    else if (oper == '-')
        {
            cout << "\nThe difference of the two numbers is" ;
            cout << aa-bb;
        }
    else if (oper == '+')
        {
            cout << "\nThe sum of the two numbers is" ;
            cout << aa+bb ;
        }

    else if (oper == '/' || oper == '%')
    {
        if (bb==0)
            cout << "\n undefined";

        else
            aa=a;
            bb=b;
            c = a /b;
            cout << setprecision (5) << "\n division" << c;
            cout << "\n remainder" << a % b;
    }

    else
        cout << "error exiting program";


        return 0;
}
When you get to line 43, what do you think the values of a and b are?
Hint: they're uninitialized variables and contain garbage.

Line 45: As i suspected, you're doing integer division. i.e. 3/4 = 0. Do your division with aa and bb.

Line 43: As I pointed out before, only line 43 is within the scope of the else at line 42. You need to enclose 43-47 in {}.
thanks for the help another way i wrote is was like. However I'm gonna re write my original since I like it more. Let me test it to make sure it works


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
#include <iostream>   // input and output
#include <iomanip>    // manipulator that allows the user to use setprecision
using namespace std;

int main ()
{
int a;      //declaration of 2 integers
int b;
double aa;  //declaration of two double values. They can hold both integers and floats
double bb;  //used two aa's as an easy way to remember double=two letters
double c;
char oper;

cout << "\nEnter first number" <<endl ;      /* prompt to enter first and second numbers */
cin >> aa ;
cout << "\nEnter second number" <<endl ;
cin >> bb;
cout << "\nChoose a mathematical operator";     //prompt for operator
cin >> oper;

    if (oper == '*' || oper == 'x')       // multiple options for operators
        {
            cout << "\nThe product of the two numbers is\t\n" ;
            cout << aa*bb;
        }
    else if (oper == '-')                                       //continuing pattern for several lines
        {
            cout << "\nThe difference of the two numbers is" ;
            cout << aa-bb;
        }
    else if (oper == '+')
        {
            cout << "\nThe sum of the two numbers is" ;
            cout << aa+bb ;
        }

     else if (oper == '/') //division by zero is not possible. If else inside statement to separate the two outcomes
        {
            if (bb==0)     //checks value
                cout << "\nUndefined" ;
            else

                cout << "\n("<< aa << "/" << bb << ")"<< "=" << setprecision(5)<< aa/bb; // decimal place cannot exceed 5. Also it looks fancy

        }
    else if (oper == '%')  //have to use variables for this
        {
            a=aa;         //declaration of variables
            b=bb;           

            if (b==0)     // once again zero is not an acceptable value
                cout << "Error" ;
            else
                cout << a % a ;      answer
        }
    else
        cout << "error exiting program";       //in case user tries to type nonsense 


        return 0;
}

it works

FINAL RESULT

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
#include <iostream>   // input and output
#include <iomanip>    // manipulator that allows the user to use setprecision
using namespace std;

int main ()
{
int a;      //declaration of 2 integers
int b;
double aa;  //declaration of two double values. They can hold both integers and floats
double bb;  //used two aa's as an easy way to remember double=two letters
char oper;

cout << "\nEnter first number" <<endl ;      /* prompt to enter first and second numbers */
cin >> aa ;
cout << "\nEnter second number" <<endl ;
cin >> bb;
cout << "\nChoose a mathematical operator";     //prompt for operator
cin >> oper;

    if (oper == '*' || oper == 'x')       // multiple options for operators
        {
            cout << "\nThe product is\n" ;
            cout << aa*bb;
        }
    else if (oper == '-')                                       //continuing pattern for several lines
        {
            cout << "\nThe difference is" ;
            cout << aa-bb;
        }
    else if (oper == '+')
        {
            cout << "\nThe sum is" ;
            cout << aa+bb ;
        }

     else if (oper == '/' || oper == '%')   // I combined the division and modus operators.
                                            //this makes it easier to use
    {
        if (bb==0)  //If zero is enter the answer is undefined
        {
            cout << "\n undefined";
        }
        else
        {
            a=aa;
            b=bb;
            c = aa /bb;
            cout << setprecision (5) << "\n division" << c;    //stop decimals after 5 spaces
            cout << "\n remainder" << a % b;
        }
    }
    else
        cout << "error exiting program";       //in case user tries to type nonsense


        return 0;
}
Topic archived. No new replies allowed.