Note that there are some explanatory texts on larger screens.

plurals
  1. POHaving trouble cout-ing returned functions
    text
    copied!<p>I am new to C++ and am having trouble passing string back to the main class of my code.</p> <p>My goal is to split the below code so that I have 2 functions other than the main class and at least one must return a value other than 0.</p> <p>Beginning code: </p> <pre><code>#include "stdafx.h" #include &lt;iostream&gt; #include &lt;iomanip&gt; #include &lt;string&gt; using namespace std; int _tmain(int argc, _TCHAR* argv[]) { cout.precision(2); cout.setf(ios::fixed,ios::floatfield); float speedLimit; float driversSpeed; float ticketAmount; float speedOver; string repeat; /*Use a symbolic constant for the base ticket fine rate ($50).*/ const float base = 50; start: /*Prompt the user for the speed limit and the speed of the driver.*/ cout &lt;&lt; "Enter the speed limit: "; cin &gt;&gt; speedLimit; cout &lt;&lt; "Enter the driver's speed: "; cin &gt;&gt; driversSpeed; cout &lt;&lt; "You were driving " &lt;&lt; driversSpeed &lt;&lt; " in a " &lt;&lt; speedLimit &lt;&lt; " mph zone.\n"; speedOver = driversSpeed - speedLimit; if (speedOver &lt;= 10 &amp;&amp; speedOver &gt;= 1) { ticketAmount = base; } else if (speedOver &lt;= 14 &amp;&amp; speedOver &gt;= 11) { ticketAmount = (base *.05) + base; } else if (speedOver &lt;= 19 &amp;&amp; speedOver &gt;= 15) { ticketAmount = (base *.1) + base; } else if (speedOver &lt;= 24 &amp;&amp; speedOver &gt;= 20) { ticketAmount = (base *.15) + base; } else if (speedOver &lt;= 29 &amp;&amp; speedOver &gt;= 25) { ticketAmount = (base *.2) + base; } else if (speedOver &gt;= 30) { ticketAmount = (base *.25) + base; } else { ticketAmount = 0; } cout &lt;&lt; "Your fine is $" &lt;&lt; ticketAmount; cout &lt;&lt; "\nEnter Y to continue. Anything else to stop: "; cin &gt;&gt; repeat; if (repeat == "Y" || "y") goto start; else exit(0); return 0; } </code></pre> <p>and here what I have done so far:</p> <pre><code>#include "stdafx.h" #include &lt;iostream&gt; #include &lt;iomanip&gt; #include &lt;string&gt; using namespace std; const float base = 50; float speedLimit; float driversSpeed; float ticketAmount; float speedOver; int _tmain(int argc, _TCHAR* argv[]) { cout.precision(2); cout.setf(ios::fixed,ios::floatfield); string repeat; /*Use a symbolic constant for the base ticket fine rate ($50).*/ start: /*Prompt the user for the speed limit and the speed of the driver.*/ cout &lt;&lt; "Enter the speed limit: "; cin &gt;&gt; speedLimit; cout &lt;&lt; "Enter the driver's speed: "; cin &gt;&gt; driversSpeed; /*Display to the user the values which were input (speed limit and driver's speed) and the calculated ticket fine amount. Print 2 numbers after the decimal point for the fine amount. Make sure your output format matches the sample format.*/ cout &lt;&lt; "You were driving " &lt;&lt; driversSpeed &lt;&lt; " in a " &lt;&lt; speedLimit &lt;&lt; " mph zone.\n"; speedOver = driversSpeed - speedLimit; cout &lt;&lt; string(finalOutput); /*After the fine is printed for the first speeding violation, prompt the user to see if he/she wants to enter another speeding violation. If so, prompt again for the speed limit and driver's speed. Repeat the calculation and print the fine. Repeat this process until the user indicates he/she wants to stop. The user can enter either an uppercase or lowercase letter Y to continue with the program.*/ cout &lt;&lt; "\nEnter Y to continue. Anything else to stop: "; cin &gt;&gt; string(repeat); if (repeat == "Y" || "y") goto start; else exit(0); } float ticketAmountFunc(float ticketAmount) { /*Calculate the ticket cost as $50 (the base fine rate) plus: 0% additional if the driver's speed was 10 or less miles per hour above the speed limit. 5% additional if driver's speed was more than 10 miles per hour above the speed limit. 10% additional if driver's speed was more than 15 miles per hour above the speed limit 15% additional if driver's speed was more than 20 miles per hour above the speed limit. 20% additional if driver's speed was more than 25 miles per hour above the speed limit. 25% additional if driver's speed was 30 or more miles per hour above the speed limit. Do not charge a fine if the driver wasn't speeding.*/ if (speedOver &lt;= 10 &amp;&amp; speedOver &gt;= 1) { ticketAmount = base; } else if (speedOver &lt;= 14 &amp;&amp; speedOver &gt;= 11) { ticketAmount = (base *.05) + base; } else if (speedOver &lt;= 19 &amp;&amp; speedOver &gt;= 15) { ticketAmount = (base *.1) + base; } else if (speedOver &lt;= 24 &amp;&amp; speedOver &gt;= 20) { ticketAmount = (base *.15) + base; } else if (speedOver &lt;= 29 &amp;&amp; speedOver &gt;= 25) { ticketAmount = (base *.2) + base; } else if (speedOver &gt;= 30) { ticketAmount = (base *.25) + base; } else { ticketAmount = 0; } return ticketAmount; } string finalOutput(string tix) { string words = "Your fine is $"; //tix = words + ticketAmountFunc; tix += string(words) + string(ticketAmountFunc); return tix; } </code></pre> <p>VS is returning 2 errors:</p> <pre><code>Error 1 error C2065: 'finalOutput' : undeclared identifier Error 7 error C2440: '&lt;function-style-cast&gt;' : cannot convert from 'float (__cdecl *)(f </code></pre> <p>loat)' to 'std::string' </p> <p>Could someone please poing me in the direction of my error?</p> <p>Thank you.</p> <p>EDIT: Thank you Ben. I moved my main method and tried moving variables around to declare them as strings and still have the undeclared identifier issue but now twice.</p> <p>Here is my updated code:</p> <pre><code>#include "stdafx.h" #include &lt;iostream&gt; #include &lt;iomanip&gt; #include &lt;string&gt; using namespace std; const float base = 50; float speedLimit; float driversSpeed; float ticketAmount; float speedOver; string ticketAmountFunc(string r) { string ticketAmount; if (speedOver &lt;= 10 &amp;&amp; speedOver &gt;= 1) { ticketAmount = base; } else if (speedOver &lt;= 14 &amp;&amp; speedOver &gt;= 11) { ticketAmount = (base *.05) + base; } else if (speedOver &lt;= 19 &amp;&amp; speedOver &gt;= 15) { ticketAmount = (base *.1) + base; } else if (speedOver &lt;= 24 &amp;&amp; speedOver &gt;= 20) { ticketAmount = (base *.15) + base; } else if (speedOver &lt;= 29 &amp;&amp; speedOver &gt;= 25) { ticketAmount = (base *.2) + base; } else if (speedOver &gt;= 30) { ticketAmount = (base *.25) + base; } else { ticketAmount = "0"; } std::string s = ticketAmount; r = s; return r; } string finalOutput(string tix) { string words = "Your fine is $"; //tix = words + ticketAmountFunc; tix = string() + words + ticketAmountFunc(r); return tix; } int _tmain(int argc, _TCHAR* argv[]) { cout.precision(2); cout.setf(ios::fixed,ios::floatfield); string repeat; /*Use a symbolic constant for the base ticket fine rate ($50).*/ start: /*Prompt the user for the speed limit and the speed of the driver.*/ cout &lt;&lt; "Enter the speed limit: "; cin &gt;&gt; speedLimit; cout &lt;&lt; "Enter the driver's speed: "; cin &gt;&gt; driversSpeed; cout &lt;&lt; "You were driving " &lt;&lt; driversSpeed &lt;&lt; " in a " &lt;&lt; speedLimit &lt;&lt; " mph zone.\n"; speedOver = driversSpeed - speedLimit; cout &lt;&lt; string(finalOutput(tix)); cout &lt;&lt; "\nEnter Y to continue. Anything else to stop: "; cin &gt;&gt; string(repeat); if (repeat == "Y" || "y") goto start; else exit(0); } </code></pre> <p>and my errors are:</p> <p>Error 7 error C2065: 'r' : undeclared identifier<br> Error 8 error C2065: 'tix' : undeclared identifier </p>
 

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