Rounding to two digits and stop program from following two if statements

So it's been a while since I've done some coding and I've just got back to it after 4 years or so and I don't know what I am doing wrong. For starters, I am trying to say in my code that if both integers are zero to output indeterminate and if a is zero to output undefined. However, even if those two statements aren't true the program is outputting indeterminate and undefined with every output.

I also don't know how to round these numbers. Could anyone help with rounding them to two digits?? Thanks so much in advance

#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;

int main() {
// ax+b=0
//ax=-b
//x=-b/a
double a,b;
cin>>a;
cin>>b;
if ( b != 0 ); {
if ( a != 0 );{
if (a < 0) {
if (b > 0); {
cout<<b/a;
}
}
if (a < 0) {
if (b < 0); {
cout<<"-"<<b/a;
}
}
if (a > 0) {
if (b > 0); {
cout<<"-"<<b/a;
}
}
if (a < 0) {
if (b > 0); {
cout<<b/a;
}
}
}
}
if ( a == 0 and b != 0 );{
cout<<"undefined";
}
if ( b == 0 ); {
if ( a == 0 ); {
cout<<"indeterminate";
}
}
return 0;
}
You have a ; after your if statements if ( a == 0 ); , if ( b == 0 ); . Also if ( a == 0 and b != 0 ); should use the && operator (i.e.) if ( a == 0 && b != 0 ).

Please, take a look at this for a refresher http://www.cplusplus.com/doc/tutorial/control/ and the logical operator section of http://www.cplusplus.com/doc/tutorial/operators/
Last edited on
Hi maroon419

Please always use code tags (the <> icon on the right of the text box), it makes it a lot easier for people to read your code.

A couple of things,
_Your code doesn't compile, you put semicolons after your if statements, that's not how they work, check the following link for more info: https://en.cppreference.com/w/cpp/language/if
_You have too many if statements, and can easily group them (see my code below for example of that)
_Your mathematical logic is wrong, for multiplications and divisions:
*negative with negative gives positive
*positive with positive gives positive
*negative with positive gives negative
_To round your numbers, check the function setprecision() for which you included the right header iomanip: http://www.cplusplus.com/reference/iomanip/setprecision/

Here is how you could have written 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
#include <iostream>
#include <math.h>
#include <iomanip>

using namespace std;

int main()
{
    double a,b;
    
    cout << " Enter a: ";
    cin >> a;
    
    cout << " Enter b: ";
    cin >> b;
    
    cout << showpoint <<  setprecision(2) << fixed << "\n ";
    
    if ( a != 0 && b != 0)
    {
        if (a > 0 && b > 0)
            cout << b/a;   
            
        else if (a < 0 && b > 0)
            cout << "-" << b/a;
            
	else if (a > 0 && b < 0)
            cout << "-" << b/a;
            
        else if (a < 0 && b < 0)
            cout << b/a;    
    } 
    else if ( a == 0 && b != 0 )
        cout << "undefined";
    else if ( a == 0 && b == 0)
        cout << "indeterminate";
    
    cout << "\n\n";
    system("pause");
    
    return 0;
}


Hope this helps you,

Regards,

Hugo.
Last edited on
chicofeo wrote:
Also if ( a == 0 and b != 0 ); should use the && operator (i.e.) if ( a == 0 && b != 0 ).

You should make it clear that that is just your opinion. He can use "and" if he wants, although pretty much everybody uses && so it is probably a better choice.

H00G0 wrote:
Your code doesn't compile,

Yes it does.

H00G0 wrote:
To round your numbers, check the function setprecision()

I see you fixed that part. But your code gives no output if b is zero and a is not.

@maroon419, I'm not sure what distinction you are making between undefined and indeterminate. The only special case with your equation is if a is 0. And I can't comprehend what you are trying to do with negative values. It seems like it could be simplified to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <iomanip>

int main() {
    // x = -b / a
    std::cout << std::fixed << std::setprecision(2);
    double a, b;
    std::cout << "Enter a and b: ";
    std::cin >> a >> b;
    std::cout << "x is ";
    if (a != 0)
        std::cout << -b / a << '\n';
    else
        std::cout << "undefined\n";
}

EDIT: BTW, if you want to avoid the somewhat strange -0.00 output that can occur, you can change the if (a != 0) part to this:

1
2
3
4
5
    if (a != 0) {
        double ans = -b / a;
        if (ans == 0.0) ans = 0.0; // get rid of -0.0
        std::cout << ans << '\n';
    }

if (ans == 0.0) ans = 0.0 might look a little strange, but -0.0 and 0.0 are equal, so it makes no difference if you say ans == 0.0 or ans == -0.0.
Last edited on
I see you fixed that part. But your code gives no output if b is zero and a is not.

Yes I know, I simply rewrote OP's code changing his if statements and adding rounding to his outputs, this is now how I would have written it if I had done it from scratch.

You're right though, one may add
1
2
else if (b == 0 && a != 0)
    cout << b / a; //which is 0 anyway 


But yes the snipped you provided is much better
Last edited on
@ dutch , You do know the difference between "should" (opinion) and "must" (fact), right? I assume you do know the many different ways to solve this problem, correct?

Good! now that we got it out of the way, I do not have a picture of you in my wallet, you do not pay my bills, you are not my supervisor; therefore, your demands to me means as much as the fecal matter that comes out of your mouth.

@chicofeo, Obviously English is not your first language. You've misunderstood the phrase "You should make it clear that that is just your opinion". You should have made it clear. That's my opinion.

And in the future, keep your braindead subhuman comments to yourself!
Topic archived. No new replies allowed.