I am supposed to write a program that asks for the coefficients a, b, and c of a quadratic equation ax2+bx+c=0. It needs to display to the screen one of the following:
• two distinct real numbers (when b2-4ac > 0),
• two distinct complex numbers (when b2-4ac < 0), or
• one real number (when b2-4ac = 0)

I have worked the following program and it doesn't work when set up this way:

#include <iostream>
using namespace std;

int main()
{
	cout << "Enter three integers and the product will be displayed: ";
    cin >> a >> b >> c;
	
	if (((b * b) - (4 * a * c)) > 0) 
	   cout << "The result of " << a << "," << b << ", and\n "
       << c << " has two distinct real numbers.";
	
    else if (((b * b) - (4 * a * c)) < 0)
	   cout << "The result of " << a << "," << b << ", and\n "
	   << c << " has two distinct complex numbers.";
	  
	else (((b * b) - (4 * a * c)) == 0)
         cout << "The result of " << a << "," << b << ", and\n "
         << c << " has one real number.";
             
	system ("pause");

	return 0;
}

If I change the last statement to 'if else' the program works, but I don't think that would be correct. Please help.

Dani AI

Generated

Good instincts, . The specific bug is exactly what pointed out: the last arm must be a plain else. After you test > 0 and < 0, the only remaining case is == 0, so attaching a condition to else is a syntax error. Also, make sure the coefficients are declared before you read them, as noted.

Here is a compact pattern that compiles, validates input, handles the a == 0 edge case (not quadratic), computes the discriminant once, and classifies the roots. It also allows non-integer input and uses a small tolerance to treat near-zero discriminants as one real root.

#include <iostream>
#include <cmath>

int main() {
    double a = 0.0, b = 0.0, c = 0.0;
    std::cout << "Enter coefficients a b c: ";
    if (!(std::cin >> a >> b >> c)) {
        std::cerr << "Invalid input\n";
        return 1;
    }
    if (a == 0.0) {
        std::cout << "Not a quadratic (a == 0)\n";
        return 0;
    }

    const double D = b*b - 4.0*a*c;
    const double tol = 1e-12 * (std::fabs(b*b) + std::fabs(4.0*a*c) + 1.0);

    if (D > tol) {
        std::cout << "two distinct real numbers\n";
    } else if (D < -tol) {
        std::cout << "two distinct complex numbers\n";
    } else {
        std::cout << "one real number\n";
    }
}

Tips:

  • Prefer double for a, b, c; using int can overflow b*b or 4ac and misclassify the result.
  • Keep braces on each branch now; it prevents surprises if you expand the output later.
  • Skip system("pause"); it is non-portable. If you need a pause, run from a terminal or wait for an extra newline.
  • If you decide to print the actual roots, use std::complex<double> and take the square root of D in the complex domain.

Recommended Answers

All 4 Replies

I am supposed to write a program that asks for the coefficients a, b, and c of a quadratic equation ax2+bx+c=0. It needs to display to the screen one of the following:
• two distinct real numbers (when b2-4ac > 0),
• two distinct complex numbers (when b2-4ac < 0), or
• one real number (when b2-4ac = 0)

I have worked the following program and it doesn't work when set up this way:

#include <iostream>
using namespace std;

int main()
{
	cout << "Enter three integers and the product will be displayed: ";
    cin >> a >> b >> c;
	
	if (((b * b) - (4 * a * c)) > 0) 
	   cout << "The result of " << a << "," << b << ", and\n "
       << c << " has two distinct real numbers.";
	
    else if (((b * b) - (4 * a * c)) < 0)
	   cout << "The result of " << a << "," << b << ", and\n "
	   << c << " has two distinct complex numbers.";
	  
	else (((b * b) - (4 * a * c)) == 0)
         cout << "The result of " << a << "," << b << ", and\n "
         << c << " has one real number.";
             
	system ("pause");

	return 0;
}

If I change the last statement to 'if else' the program works, but I don't think that would be correct. Please help.

It's a syntax error as written, and won't compile, so it can't be run. Why would you think this is more correct?

You're also not declaring a, b, and c. Please post actual code and wrap it in code tags.

It would be less redundant to use a temporary, say

double det = (b * b) - (4 * a * c);

instead of recalculating it several times. It is also cleaner, clearer, and easier to read.

It's a syntax error as written, and won't compile, so it can't be run. Why would you think this is more correct?

You're also not declaring a, b, and c. Please post actual code and wrap it in code tags.

It would be less redundant to use a temporary, say

double det = (b * b) - (4 * a * c);

instead of recalculating it several times. It is also cleaner, clearer, and easier to read.

Okay, I understand the declaring the equation once, the det = the equation. I'm not sure I follow about not declaring a,b,c. There is a cin part of the program for the user to input a,b, and c. By the way what do you mean by post actual code and wrap tit in code tags? Sorry, I'm new at this.

I'm not sure I follow about not declaring a,b,c. There is a cin part of the program for the user to input a,b, and c.

What are a, b, c? The compiler does not automagically "do what I mean". You have to declare variables before you can try to obtain values for them using user input.

By the way what do you mean by post actual code and wrap tit in code tags?

Look up. Notice the differences. Click the button for help. [edit]The "Help with Code Tags" link.

And by actual code, I mean the code that you actually have. What you posted does not compile. When you talk about it "working", I assume you mean running, which implies that it made it past compiling.

The cin part enables the user to input values which can then be stored in the variables a, b, c.
Think of a,b, c as 3 containers. When you declare them

int a,b,c;

You are telling the computer that at some point some one will store a value in this container. When you use cin statements, the users provides the values which are then stored in the containers.

I hope this makes it clearer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.