- October 10, 2007 at 9:00 pm #2017saranyabalajiParticipant
using namespace std;
int count = 0;
int menuoption = 0;
float a = 0.0;
float b = 0.0;
float c = 0.0;
float x1 = 0.0;
float x2 = 0.0;
cout << "ntt
Quadratic Calc 1.0
cout << "1. Enter Coefficientsn";
cout << "2. Calculation solutionsn";
cout << "3. Quit Programn";
cout << "Enter choice: ";
if (!(cin >> choice)) // if the user enters a non-numerical number, this is stored into a badChars string and prints out non-valid statement.
while (choice < 1 || choice > 3) // if the user enters a choice that is greater than 3 or less that 1, the following statement is printed in again.
cout << "Please enter 1, 2, or 3: ";
cin >> choice;
if(choice == 1) // if the user selects choice 1, then they will be asked to enter a value for the coefficients a, b and c.
menuoption = 1;
cout << "Enter the coefficients a , b , c for the quadratic equation ax^2 + bx + c = 0:n";
cout << "Enter value for a:n";
cin >> a;
cout << "Enter value for b:n";
cin >> b;
cout << "Enter value for c:n";
cin >> c;
if(choice == 2) // when the user selects choice 2.
if (menuoption == 1) // if the menu option is chosen to be 1, then choice 2 works as menuoption = 0 is declared in the beginning. In order for choice 2 to work, choice 1 must first be selected.
menuoption = 0;
if(cin.fail()) // if the user enters a non-numerical value.
cout << "ERROR!! Input Failure!n";
else if(a == 0) // is a = 0, then a run-time error has occurred.
cout << "ERROR!! Unable to solve the problem.n";
else if(((pow(b,2)-(4 * a * c))) < 0) // if there are negative solutions under a square root, then it is non-real.
cout << "Non-real results! ";
cout << "Thank you for using the program. You didn't calculate solutions." "n";
else // if the coefficients fulfill the requirements, then 2 solutions are calculated.
x1 = (-b + (sqrt(pow(b,2)-(4 * a * c))))/(2 * a);
x2 = (-b - (sqrt(pow(b,2)-(4 * a * c))))/(2 * a);
cout << "The roots are: " "n"
<< "x1 = "<< x1 << " , " << "x2 = " << x2 << "n";
} while (choice != 3); // when choice doesn't equal three, then the following statement is printed out.
cout << "Thank you for using the program. You calculated solutions for " << count << " quadratic equations." "n";
Modify this lab by implementing functions to perform most of the tasks in the main body.
Your new program must utilize at least the following functions:
1. A function, called getDouble() that returns a double after reacting to any possible input failure.
2. A function, called getInt() that returns an int after reacting to any possible input failure.
3. A function that accepts three doubles (for the three quadratic coefficients) and returns:
– If the coefficients match the criteria under Lab 4.
– If the coefficients result in a non-quadratic equation.
– If the coefficients result in an equation with non-real roots
4. Two functions that each accept the three quadratic coefficients and each return their respective roots of the equation.
5. A function that prints your menu.
6. A function, called processUserChoice. that accepts the user choice (calculate, enter coeffs, or quit) and the a, b, and c and processes the user choice. Your functions must “more-or-less” work with the following main program:
double a, b, c;
userChoice = getUserChoice();
processUserChoice(a, b, c, userChoice);
while(userChoice != x ); // x is the choice that ends the program
Note that doubles a,b, and c must be passed into ProcessUserChoice by reference so that that function may make changes to a, b, and c. Make sure you properly document your functions. Lastly, all of your functions must be included in a separate stand-alone library.
Can you aid me on where to start?
Thanks so much!
- The forum ‘C Programming’ is closed to new topics and replies.