我是 C++ 新手,无法将字符串传递回代码的主类。
我的目标是拆分下面的代码,以便我拥有除主类之外的 2 个函数,并且至少一个函数必须返回 0 以外的值。
开始代码:
#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 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 << "Enter the speed limit: ";
cin >> speedLimit;
cout << "Enter the driver's speed: ";
cin >> driversSpeed;
cout << "You were driving " << driversSpeed << " in a " << 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 is $" << ticketAmount;
cout << "\nEnter Y to continue. Anything else to stop: ";
cin >> repeat;
if (repeat == "Y" || "y")
goto start;
else
exit(0);
return 0;
}
以及到目前为止我所做的:
#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 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 << "Enter the speed limit: ";
cin >> speedLimit;
cout << "Enter the driver's speed: ";
cin >> 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 << "You were driving " << driversSpeed << " in a " << speedLimit << " mph zone.\n";
speedOver = driversSpeed - speedLimit;
cout << 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 << "\nEnter Y to continue. Anything else to stop: ";
cin >> 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 <= 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 is $";
//tix = words + ticketAmountFunc;
tix += string(words) + string(ticketAmountFunc);
return tix;
}
VS 返回 2 个错误:
Error 1 error C2065: 'finalOutput' : undeclared identifier
Error 7 error C2440: '<function-style-cast>' : cannot convert from 'float (__cdecl *)(f
loat)' 到 'std::string'
有人可以指点我的错误方向吗?
谢谢你。
编辑:谢谢本。我移动了我的主要方法并尝试移动变量以将它们声明为字符串,但仍然存在未声明的标识符问题,但现在两次。
这是我更新的代码:
#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 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 << "Enter the speed limit: ";
cin >> speedLimit;
cout << "Enter the driver's speed: ";
cin >> driversSpeed;
cout << "You were driving " << driversSpeed << " in a " << speedLimit << " mph zone.\n";
speedOver = driversSpeed - speedLimit;
cout << string(finalOutput(tix));
cout << "\nEnter Y to continue. Anything else to stop: ";
cin >> string(repeat);
if (repeat == "Y" || "y")
goto start;
else
exit(0);
}
我的错误是:
错误 7 错误 C2065:“r”:未声明的标识符
错误 8 错误 C2065:“tix”:未声明的标识符