在返回函数时遇到问题

时间:2013-05-13 19:30:42

标签: c++ visual-c++

我是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)'to'std :: string'

有人可能会指责我的错误方向吗?

谢谢。

编辑:谢谢Ben。我移动了我的main方法并尝试移动变量以将它们声明为字符串并且仍然存在未声明的标识符问题但现在两次。

这是我更新的代码:

#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':未声明的标识符

2 个答案:

答案 0 :(得分:0)

你试图在他们的“声明点”之前使用这些功能。有两个简单的解决方案(选择一个):

  1. 在使用前添加前向声明aka prototype
  2. 将来电者(main())移到其使用的功能下方。
  3. 此外,在调用函数时,需要在括号内提供其参数。所以你需要说ticketAmountFunc(ticketAmount)而不只是ticketAmountFunc

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

答案 1 :(得分: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,而我有时会混合使用这两种想法。

希望一切顺利。

相关问题