如何解决DevCC编译器错误:[错误]在'if'之前出现预期的unqualified-id?

时间:2019-05-24 04:08:23

标签: c++ compiler-errors

我正在正确处理家庭作业问题,需要一些帮助来弄清楚如何解决编译器错误:[错误]在'if'之前应为unqualified-id。

我尝试将这些函数包含在int main函数中,但这会导致更多错误。我曾尝试寻找缺少的花括号,但似乎找不到任何花括号。

“ if(surge!='Y')”是我在[if]之前得到[错误]预期的不合格ID的地方。

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

double calcFare (double perMinute, double lengthMinutes, double minuteFare, double perMile, double distanceMiles, double mileFare, double baseFare, double totalFare, char carClass);
double calcFareSurge (double surgeFare, double perMinute, double lengthMinutes, double minuteFare, double perMile, double distanceMiles, double mileFare, double baseFare, double totalFare, double surgeMultiplier, char carClass);
double test;
void output (string name, double totalFare);

int main()
{
    string name;
    double surgeFare, perMinute, lengthMinutes, minuteFare, perMile, distanceMiles, mileFare, surgeMultiplier, baseFare, totalFare;
    char surge, carClass;

    cout << "Enter name: " << endl;
    getline(cin, name);
    cout << "Enter time in minutes: " << endl;
    cin >> lengthMinutes;
    cout << "Enter distance in miles: " << endl;
    cin >> distanceMiles;
    cout << "Enter 'X' for uberX or 'S' for SUV or 'L' for luxury: " << endl;
    cin >> carClass;
    carClass = (char) toupper(carClass);
    cout << "Enter 'Y' if surge or 'N' if not: " << endl;
    cin >> surge;
    surge = (char) toupper(surge);

    if (surge == 'Y')
    {
        cout << "Enter surge multiplier: " << endl;
        cin >> surgeMultiplier;
    }

    totalFare = calcFare(perMinute, lengthMinutes, minuteFare, perMile, distanceMiles, mileFare, baseFare, totalFare, carClass);

    output(name, totalFare);

    system ("pause");
    return 0;
}

    if (surge != 'Y')
    {
        double calcFare (double perMinute, double lengthMinutes, double minuteFare, double perMile, double distanceMiles, double mileFare, double baseFare, double totalFare, char carClass)
        {       
            if (carClass == 'X')
            {       
                baseFare = 2.00;
                perMinute = 0.22;
                perMile = 1.15;
                minuteFare = perMinute * lengthMinutes;
                mileFare = perMile * distanceMiles;
                totalFare = minuteFare + mileFare + baseFare;
            }
            else if (carClass == 'S')
            {   
                baseFare = 15.00;
                perMinute = 0.90;
                perMile = 3.75;
                minuteFare = perMinute * lengthMinutes;
                mileFare = perMile * distanceMiles;
                totalFare = minuteFare + mileFare + baseFare;   
            }
            else if (carClass == 'L')
            {
                baseFare = 5.00;
                perMinute = 0.50;
                perMile = 2.75;
                minuteFare = perMinute * lengthMinutes;
                mileFare = perMile * distanceMiles;
                totalFare = minuteFare + mileFare + baseFare;
            }   
        }
    else
    {
        double calcFareSurge (double surgeFare, double perMinute, double lengthMinutes, double minuteFare, double perMile, double distanceMiles, double mileFare, double baseFare, double totalFare, double surgeMultiplier, char carClass)
        {
            if (carClass == 'X')
            {   
                baseFare = 2.00;
                perMinute = 0.22;
                perMile = 1.15;
                surgeFare = baseFare * surgeMultiplier;
                minuteFare = perMinute * lengthMinutes;
                mileFare = perMile * distanceMiles;
                totalFare = minuteFare + mileFare + baseFare;
            }
            else if (carClass == 'S')
            {   
                baseFare = 15.00;
                perMinute = 0.90;
                perMile = 3.75;
                surgeFare = baseFare * surgeMultiplier;
                minuteFare = perMinute * lengthMinutes;
                mileFare = perMile * distanceMiles;
                totalFare = minuteFare + mileFare + baseFare;
            }
            else if (carClass == 'L')
            {
                baseFare = 5.00;
                perMinute = 0.50;
                perMile = 2.75;
                surgeFare = baseFare * surgeMultiplier;
                minuteFare = perMinute * lengthMinutes;
                mileFare = perMile * distanceMiles;
                totalFare = minuteFare + mileFare + baseFare;
            }
        }
    }

    void output(string name, double totalFare)     
    {
        cout << setprecision(2) << fixed;
        cout << "Rider's name: " << name << endl;
        cout << "Total: $ " << totalFare << endl;
        return;
    }

1 个答案:

答案 0 :(得分:0)

if (surge != 'Y')
    {.........
     .......
}

上述代码段需要放在单独的函数中或main()中。

(a)如果将其移到主函数内部,则在定义double calcFare()时,在if和else内都仍然会出现另一个错误,您只能在if-内部进行函数调用- else,不是函数定义,在这两种情况下,您都必须在if和else之外定义函数。

(b)您可以创建一个新函数并相应地处理案例。


建议阅读

(-)函数调用与函数定义之间的区别。

(-)从函数调用返回值

希望这会有所帮助