0

私は C++ を初めて使用し、文字列をコードのメイン クラスに戻すのに問題があります。

私の目標は、以下のコードを分割して、メイン クラス以外に 2 つの関数を持ち、少なくとも 1 つが 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' に

誰かが私のエラーの方向に私を向けてくれませんか?

ありがとうございました。

編集:ベン、ありがとう。メインメソッドを移動し、変数を移動して文字列として宣言しようとしましたが、まだ宣言されていない識別子の問題がありますが、現在は 2 回です。

これが私の更新されたコードです:

#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