Home › Forums › C Programming › C++ help
- This topic has 0 replies, 1 voice, and was last updated 17 years, 2 months ago by saranyabalaji.
- AuthorPosts
- October 10, 2007 at 9:00 pm #2017saranyabalajiParticipant12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788#include <iostream><br />#include <math.h><br />#include <cstdlib><br />#include <string><br />using namespace std;<br /><br />int main(){<br />int choice;<br />int count = 0;<br />int menuoption = 0;<br />float a = 0.0;<br />float b = 0.0; <br />float c = 0.0;<br />float x1 = 0.0;<br />float x2 = 0.0;<br />string badChars;<br /><br />do<br />{ <br />cout << "ntt<hr class="bbcode_rule" />Quadratic Calc 1.0<hr class="bbcode_rule" />nn";<br />cout << "1. Enter Coefficientsn";<br />cout << "2. Calculation solutionsn";<br />cout << "3. Quit Programn";<br />cout << "Enter choice: ";<br />if (!(cin >> choice)) // if the user enters a non-numerical number, this is stored into a badChars string and prints out non-valid statement.<br />{<br />cin.clear();<br />cin>>badChars;<br />cout<<"""<<badChars<<"""<<" is not valid!!!!"<<endl;<br />continue;<br />}<br /><br />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.<br />{<br />cout << "Please enter 1, 2, or 3: ";<br />cin >> choice;<br /><br />}<br /><br />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.<br />{<br />menuoption = 1;<br />cout << "Enter the coefficients a , b , c for the quadratic equation ax^2 + bx + c = 0:n";<br />cout << "Enter value for a:n";<br />cin >> a;<br />cout << "Enter value for b:n";<br />cin >> b;<br />cout << "Enter value for c:n";<br />cin >> c;<br />}<br />if(choice == 2) // when the user selects choice 2.<br />{<br />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.<br />{<br />menuoption = 0;<br />}<br />if(cin.fail()) // if the user enters a non-numerical value.<br />{<br />cout << "ERROR!! Input Failure!n";<br />}<br />else if(a == 0) // is a = 0, then a run-time error has occurred.<br />{<br />cout << "ERROR!! Unable to solve the problem.n";<br />}<br />else if(((pow(b,2)-(4 * a * c))) < 0) // if there are negative solutions under a square root, then it is non-real.<br />{<br />cout << "Non-real results! ";<br />cout << "Thank you for using the program. You didn't calculate solutions." "n";<br /><br />}<br />else // if the coefficients fulfill the requirements, then 2 solutions are calculated.<br />{<br />x1 = (-b + (sqrt(pow(b,2)-(4 * a * c))))/(2 * a);<br />x2 = (-b - (sqrt(pow(b,2)-(4 * a * c))))/(2 * a);<br />cout << "The roots are: " "n"<br /><< "x1 = "<< x1 << " , " << "x2 = " << x2 << "n";<br />count=count+1;<br />}<br />}<br />} while (choice != 3); // when choice doesn't equal three, then the following statement is printed out.<br />cout << "Thank you for using the program. You calculated solutions for " << count << " quadratic equations." "n";<br />return 0;<br />}<br /></string></cstdlib></iostream>
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:123456789101112<br />int main(){<br />double a, b, c;<br />int userChoice;<br />do{ <br />printMenu(); <br />userChoice = getUserChoice(); <br />processUserChoice(a, b, c, userChoice); <br />}<br />while(userChoice != x ); // x is the choice that ends the program<br />return 0;<br />}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!
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.