0

我是 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”:未声明的标识符

4

2 回答 2

0

您正在尝试在“声明点”之前使用这些函数。有两个简单的解决方案(选择一个):

  1. 在使用前添加一个前向声明,即原型
  2. 将调用者 ( main()) 移到它使用的函数下方。

此外,在调用函数时,您需要在括号内提供其参数。所以你需要说ticketAmountFunc(ticketAmount),而不仅仅是ticketAmountFunc

除了这些问题之外,您似乎将函数参数定义为函数生成的值,而不是它使用的数据。那没用。当你很好地使用函数时,你将不需要全局变量。

于 2013-05-13T19:34:15.363 回答
0

如果我可以补充的话,请尽量避免在你职业生涯的这个阶段使用 goto 语句——改用适当的循环和函数。

这不是一揽子规则,但除非有非常具体和正当的理由,否则可以避免 goto。

你可以使用这样的循环:

bool Quit = false;

while(!Quit) {


  // user wants to quit
     Quit = true; // execution continues after end of while loop
}

还可以使用 toupper 函数,谷歌“C++ toupper”,这样您就不必对 char 的值进行 2 次测试。

如果可以,请避免using namespace std,因为它会污染全局命名空间,这可能会导致与函数和变量名称发生冲突。例如,STL 中包含各种具有特殊含义的常用词,如距离、左右等。因此,要么放在std::每个 std 事物之前,要么对经常使用的事物执行此操作:

using std::cout;
using std::cin;
using std::endl;
using std::string;

有些人使用std::exclusivley,而我有时会混合使用这两种想法。

希望一切顺利。

于 2013-05-13T20:29:54.040 回答