c++ - Having trouble cout-ing returned functions -
i new c++ , having trouble passing string main class of code.
my goal split below code have 2 functions other main class , @ least 1 must return value other 0.
beginning code:
#include "stdafx.h" #include <iostream> #include <iomanip> #include <string> 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 symbolic constant base ticket fine rate ($50).*/ const float base = 50; start: /*prompt user speed limit , speed of driver.*/ cout << "enter speed limit: "; cin >> speedlimit; cout << "enter driver's speed: "; cin >> driversspeed; cout << "you driving " << driversspeed << " in " << speedlimit << " mph zone.\n"; speedover = driversspeed - speedlimit; if (speedover <= 10 && speedover >= 1) { ticketamount = base; } else if (speedover <= 14 && speedover >= 11) { ticketamount = (base *.05) + base; } else if (speedover <= 19 && speedover >= 15) { ticketamount = (base *.1) + base; } else if (speedover <= 24 && speedover >= 20) { ticketamount = (base *.15) + base; } else if (speedover <= 29 && speedover >= 25) { ticketamount = (base *.2) + base; } else if (speedover >= 30) { ticketamount = (base *.25) + base; } else { ticketamount = 0; } cout << "your fine $" << ticketamount; cout << "\nenter y continue. else stop: "; cin >> repeat; if (repeat == "y" || "y") goto start; else exit(0); return 0; }
and here have done far:
#include "stdafx.h" #include <iostream> #include <iomanip> #include <string> 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 symbolic constant base ticket fine rate ($50).*/ start: /*prompt user speed limit , speed of driver.*/ cout << "enter speed limit: "; cin >> speedlimit; cout << "enter driver's speed: "; cin >> driversspeed; /*display user values input (speed limit , driver's speed) , calculated ticket fine amount. print 2 numbers after decimal point fine amount. make sure output format matches sample format.*/ cout << "you driving " << driversspeed << " in " << speedlimit << " mph zone.\n"; speedover = driversspeed - speedlimit; cout << string(finaloutput); /*after fine printed first speeding violation, prompt user see if he/she wants enter speeding violation. if so, prompt again speed limit , driver's speed. repeat calculation , print fine. repeat process until user indicates he/she wants stop. user can enter either uppercase or lowercase letter y continue program.*/ cout << "\nenter y continue. else stop: "; cin >> string(repeat); if (repeat == "y" || "y") goto start; else exit(0); } float ticketamountfunc(float ticketamount) { /*calculate ticket cost $50 (the base fine rate) plus: 0% additional if driver's speed 10 or less miles per hour above speed limit. 5% additional if driver's speed more 10 miles per hour above speed limit. 10% additional if driver's speed more 15 miles per hour above speed limit 15% additional if driver's speed more 20 miles per hour above speed limit. 20% additional if driver's speed more 25 miles per hour above speed limit. 25% additional if driver's speed 30 or more miles per hour above speed limit. not charge fine if driver wasn't speeding.*/ if (speedover <= 10 && speedover >= 1) { ticketamount = base; } else if (speedover <= 14 && speedover >= 11) { ticketamount = (base *.05) + base; } else if (speedover <= 19 && speedover >= 15) { ticketamount = (base *.1) + base; } else if (speedover <= 24 && speedover >= 20) { ticketamount = (base *.15) + base; } else if (speedover <= 29 && speedover >= 25) { ticketamount = (base *.2) + base; } else if (speedover >= 30) { ticketamount = (base *.25) + base; } else { ticketamount = 0; } return ticketamount; } string finaloutput(string tix) { string words = "your fine $"; //tix = words + ticketamountfunc; tix += string(words) + string(ticketamountfunc); return tix; }
vs returning 2 errors:
error 1 error c2065: 'finaloutput' : undeclared identifier error 7 error c2440: '<function-style-cast>' : cannot convert 'float (__cdecl *)(f
loat)' 'std::string'
could please poing me in direction of error?
thank you.
edit: thank ben. moved main method , tried moving variables around declare them strings , still have undeclared identifier issue twice.
here updated code:
#include "stdafx.h" #include <iostream> #include <iomanip> #include <string> using namespace std; const float base = 50; float speedlimit; float driversspeed; float ticketamount; float speedover; string ticketamountfunc(string r) { string ticketamount; if (speedover <= 10 && speedover >= 1) { ticketamount = base; } else if (speedover <= 14 && speedover >= 11) { ticketamount = (base *.05) + base; } else if (speedover <= 19 && speedover >= 15) { ticketamount = (base *.1) + base; } else if (speedover <= 24 && speedover >= 20) { ticketamount = (base *.15) + base; } else if (speedover <= 29 && speedover >= 25) { ticketamount = (base *.2) + base; } else if (speedover >= 30) { ticketamount = (base *.25) + base; } else { ticketamount = "0"; } std::string s = ticketamount; r = s; return r; } string finaloutput(string tix) { string words = "your fine $"; //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 symbolic constant base ticket fine rate ($50).*/ start: /*prompt user speed limit , speed of driver.*/ cout << "enter speed limit: "; cin >> speedlimit; cout << "enter driver's speed: "; cin >> driversspeed; cout << "you driving " << driversspeed << " in " << speedlimit << " mph zone.\n"; speedover = driversspeed - speedlimit; cout << string(finaloutput(tix)); cout << "\nenter y continue. else stop: "; cin >> string(repeat); if (repeat == "y" || "y") goto start; else exit(0); }
and errors are:
error 7 error c2065: 'r' : undeclared identifier
error 8 error c2065: 'tix' : undeclared identifier
you're trying use functions before "point of declaration". there 2 simple solutions (pick one):
- add forward declaration aka prototype before use it.
- move caller (
main()
) below functions uses.
also, when calling function, need provide arguments inside parentheses. need ticketamountfunc(ticketamount)
, not ticketamountfunc
.
in addition these problems, seem defining function parameters value function generates, instead of data uses. that's not useful. when use functions well, won't need global variables.
Comments
Post a Comment