Note that there are some explanatory texts on larger screens.

plurals
  1. POReturn text string and then call upon said string in later function
    primarykey
    data
    text
    <p>I'm almost done with all my homework, but the last part I have to do is make a program that will read some values from a text file called "quad.txt" and calculate the roots with the quadratic formula and then output the values. The first part of the lab was do do this all in one function, main. That works fine. However, now I am asked to write three separate functions: one which calculates the discriminant (b^2 -(4*a*c)) and returns a string value (positive, zero, or negative) based on the value of the discriminant, another which will calculate the actual roots and output based on the returned string value above, and finally the main function which will open the file and run the two other functions. See my code below, but where I'm stuck is that I can't figure out how to return a string from function disc(), and then get the function display() to call upon the returned string value and output the correct data. Here is my code so far:</p> <p>Here is the link to my quad.txt file <a href="http://www.mediafire.com/view/?z3k77ytz3i1ikla" rel="nofollow">quad.txt</a></p> <pre class="lang-cpp prettyprint-override"><code>//Brian Tucker //5.23.2012 //Lab 6 Part1 //Quadratic Formula from text file #include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;cmath&gt; #include &lt;iomanip&gt; #include &lt;cstdlib&gt; #include &lt;conio.h&gt; #include &lt;string&gt; using namespace std; int a, b, c; //sets up vars double r1, r2; string disc(){ if((pow(b,2) - (4*a*c) &gt; 0)){ //determines if there are two roots and outputs return positive; } else if((pow(b,2) - (4*a*c) == 0)){ //determines if there is a double root return zero; } else if((pow(b,2) - (4*a*c) &lt; 0)){ //determines if there are no roots return negative; } } void display(string data){ r1=((-b)+sqrt(pow(b, 2)-(4*a*c)))/(2*a); //quadratic formula r2=((-b)-sqrt(pow(b, 2)-(4*a*c)))/(2*a); if(positive){ cout&lt;&lt;setw(3)&lt;&lt;"a="&lt;&lt;a; //outputting a, b, c cout&lt;&lt;setw(3)&lt;&lt;"b="&lt;&lt;b; cout&lt;&lt;setw(3)&lt;&lt;"c="&lt;&lt;c; cout&lt;&lt;setw(7)&lt;&lt;"2 rts"; cout&lt;&lt;setw(5)&lt;&lt;"r1="&lt;&lt;r1; cout&lt;&lt;setw(5)&lt;&lt;"r2="&lt;&lt;r2; } else if(zero){ cout&lt;&lt;setw(3)&lt;&lt;"a="&lt;&lt;a; //outputting a, b, c cout&lt;&lt;setw(3)&lt;&lt;"b="&lt;&lt;b; cout&lt;&lt;setw(3)&lt;&lt;"c="&lt;&lt;c; cout&lt;&lt;setw(7)&lt;&lt;"Dbl rt"; cout&lt;&lt;setw(5)&lt;&lt;"r1="&lt;&lt;r1; } else if(negative){ cout&lt;&lt;setw(3)&lt;&lt;"a="&lt;&lt;a; //outputting a, b, c cout&lt;&lt;setw(3)&lt;&lt;"b="&lt;&lt;b; cout&lt;&lt;setw(3)&lt;&lt;"c="&lt;&lt;c; cout&lt;&lt;setw(7)&lt;&lt;"No rts"; } cout&lt;&lt;endl; } int main(){ ifstream numFile; //sets up the file numFile.open("quad.txt"); //opens the file while(numFile.good()){ //while there are still values in the file, perform the function numFile&gt;&gt;a&gt;&gt;b&gt;&gt;c; string result = disc(); display(result); } numFile.close(); getch(); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload