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.

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.