I have been working on this project for a fair amount of time. And whenever I try to print out my variables so that the output looks right, the variable is "nan". I know that nan is basically the same thing as null. But I don't see why this is happening when I clearly am giving it a value. If anyone can see the problem the help would be greatly appreciated.
// Program to input two integers
// Calculate and print the sum and product of the integers
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
int A;
int B;
int C;
double solution1 = 0, solution2 = 0;
int num2Solutions = 0;
char answer = 'y';
cout<< fixed<< showpoint<< setprecision(3);
do{ //this line starts your loop off, this is not a while loop but a do while loop
// cout enter: a, b, c
// read in a b c like this --- cin >> a >> b >> c;
// end the line
// check to see if a = 0
// if so then print out the error message
// if a != 0, then use an else statement to keep going in the program
cout<< "Insert A, B, then C: "<<endl;
cin>> A >> B >> C;
cout<<endl;
solution1 = (((-1 * B) + sqrt(pow(2, B) - (4*A*C))) / (2*A));
solution2 = (((-1 * B) - sqrt(pow(2, B) - (4*A*C))) / (2*A));
//use the quadratic formula to find sol_1 and sol_2
//sol_1 would be when you do -b + .......
//sol_2 would be when you do -b - .......
if(A == 0)
{
cout<< "The equation is not quadractic because A = 0";//print out the error message saying that a cannot be zero here.
}
else
{
//rest of the program
//now you need to check to see if the equation has 2 roots or 1 root
//the way that you would do this is to use an if else statement like this
if(solution1 == solution2) /*sol_1 and sol_2 are the same*/
{
cout<< "There is only one root! ";
cout<< "Root: " << solution1 << endl; //thenn print out that there is one root and print out either sol_1 or sol_2
}
else
{
cout<< "Root 1 = " << solution1 << endl;
cout<< "Root 2 = " << solution2 << endl;
cout<< endl;
//state that there are 2 different solutions and print out
//sol_1 and sol_2 with their respective labeling
//also, increment the counter that keeps track of how manyt
//equations input had 2 real solutions
//that counter is called "num2Solutions"
}
cout<<"Another equation? Enter Y or N: "<<endl;
cin>>answer; // here is where you will ask the user to enter 'y' or 'n' if he would like
// to enter another equation
// read in the input using the cin function
// the name of the variable that tracks this is "answer"
}
}while(answer == 'y'); // this line check to make sure that you still want to continue with
// the program. If you type in 'y' when it asks if you would like to
// do another then it loops through the loop and until you and with
// an 'n' or some letter that it not 'y'
cout<< "There were " << num2Solutions << " equations with 2 real roots." <<endl; // this is where you will print out the number of equations that your entered
// that had 2 real solutions
return 0;
}
Two things I noticed are that you calculate solution1 and solution2 before you check whether A==0 and that the quadratic formula admits for complex solutions, but your program does not.
To fix the first problem, test for the condition A==0 before you do the calculations.
For the second, test whether (B2-4AC) is positive or negative. If it's negative, you'll need to use complex numbers.
I think it's in <complex>, but I've never used it, so double check that in your book or Google it.