无法执行使用传递引用和传递值参数的程序

时间:2015-01-26 02:11:02

标签: c++ syntax parameters error-handling

当我尝试执行以下程序时,我不断收到错误,我不知道我的某个函数或者是什么(我编写它的方式)有问题。我知道我的代码中第129行出了问题,但每当我尝试进行更改时,其他语法错误都会显示出来。基本上我的语法有问题,因为我正在学习调试,所以我很难用这个。

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

using namespace std;

//declare global functions
void num_units( int& input1 );
void student_fees( float& num1, int& var1 );
void state_resident( bool& cond1 );
char parking_decal( char& ans1 );
void other_student_services_fees( char& ans2, char& ans3 );


int main ()
{
    bool not_a_resident = false;
    char want_as_sticker = 'x', want_parking_decal = 'y', want_id_card = 'z';
    string semester_session = "not enrolled yet";
    int units = 0, semester = 0;
    float price_per_unit = 0, unit_fees = 0, student_services_fee = 0,  parking_decal_fee = 0, total_fees = 0, as_sticker_fee = 19.50, id_card_fee = 13.00;


    num_units( units );
    student_fees( student_services_fee, semester );
    state_resident( not_a_resident );
    parking_decal( want_parking_decal );
    other_student_services_fees( want_as_sticker, want_id_card );

    //determine total student services fee depending on user's preferences
    if ( want_as_sticker == 'n' )
        student_services_fee = student_services_fee - as_sticker_fee;
    if ( want_parking_decal == 'n' )
        student_services_fee = student_services_fee - parking_decal_fee;
    if ( want_id_card == 'n' )
        student_services_fee = student_services_fee - id_card_fee;

    //determine price per unit
    if ( not_a_resident == false )
        price_per_unit = 325.00;
    else
        price_per_unit = 46.00;

    //determine unit_fees
    unit_fees = price_per_unit * units;

    //determine parking decal price
    if ( ( parking_decal( want_parking_decal ) == 'y' ) && ( (semester == 1) || (semester == 3) ))
        parking_decal_fee = 45.00;
    else
        parking_decal_fee = 85.00;

    //calculate total fees
    total_fees = unit_fees + student_services_fee;

    if ( semester == 0 )
        semester_session = "Fall";
    else if ( semester == 1)
        semester_session = "Winter";
    else if ( semester == 2)
        semester_session = "Spring";
    else
        semester_session = "Summer";

    cout << "For " << semester_session + " " << "your total fees are $ " << total_fees;

    return 0;
}


void num_units( int& input1 )
{
    cout << "SMC Fee Calculator" << endl;
    cout << "Enter number of units enrolled: ";
    cin >> input1;
    cout << endl;
}

//calculates the unit fees
void student_fees( float& num1, int& var1 )
{
    do
    {
        //determines semester unit price
        cout << "Is this Fall[0], Winter[1], Spring[2] or Summer[3] session: ";
        cin >> var1;
        if ( var1 == 0 || var1 == 2 )
            num1 = 50.50;
        else if ( semester == 1 || semester == 3 )
            num1 = 48.50;
        else
            cout << endl;
        cout << "I'm sorry. That's an improper selection. Please try again.\n";
    }while ( var1 <= 4 && var1 >= 0 );
}

void state_resident( bool& cond1 )
{
    cout << "Are you a state resident[0] or not[1]: ";
    cin >> cond1;
    cout << endl;
}

char parking_decal( char& ans1 )
{
    do
    {
        cout << "Want a parking decal? [y/n]: ";
        cin >> ans1;
        if ( (ans1 != 'y') || (ans1 != 'n') )
            cout << "I'm sorry. That's not a valid input. Please try again.\n";
        else
            break;
    }while ( (ans1 != 'y') || (ans1 != 'n') );
    cout << endl;
    return ans1;
}

void other_studentservices_fees( char& ans2, char& ans3 )
{
    //Ask if user wants an AS sticker.
    do
    {
        cout << "Want an AS sticker? [y/n]: ";
        cin >> ans2;
        //proof answer
        if ( (ans2 != 'y') || (ans2 != 'n') )
            cout << "I'm sorry. That's not a valid input. Please try again.\n";
        else
            break;
    }  
    cout << endl;

    //Ask if user wants an AS sticker.
    do
    {
        cout << endl << "Want an ID card? [y/n]: ";
        cin >>ans3;
        //proof answer
        if ( (ans2 != 'y') || (ans2 != 'n') )
            cout << "I'm sorry. That's not a valid input. Please try again.\n";
        else
            break;
    } 
    cout << endl;
}

输出应如下所示:

SMC Fee Calculator
Enter number of units enrolled: 18
Is this Fall[0], Winter[1], Spring[2] or Summer[3] session: 0
Are you a state resident[0] or not[1]: 0
Want a parking decal? [y/n]: n
Want an AS sticker? [y/n]: n
Want an ID card? [y/n]: n
For Fall semester, your total fees are $ 846.00

SMC Fee Calculator
Enter number of units enrolled: 6
Is this Fall[0], Winter[1], Spring[2] or Summer[3] session: 1
Are you a state resident[0] or not[1]: 1
Want a parking decal? [y/n]: y
Want an AS sticker? [y/n]: y
Want an ID card? [y/n]: y
For Winter semester, your total fees are $ 2043.50

1 个答案:

答案 0 :(得分:0)

我已经看了你的节目5分钟而且有很多错误,我甚至不确定从哪里开始...

  1. 第131和145行:您忘记了执行条件。
  2. 第141行:应该替换&#34; ans2&#34; by&#34; ans3&#34; (顺便提一下变量的名称)
  3. 第89行:学期未申报,似乎你应该使用&#34; var1&#34;相反,这是另一个可怕的名字。
  4. 第12行:您声明函数&#34; other_student_services_fees&#34;但你永远不会定义它
  5. 那时我就放弃了你。我们不是为你做功课,这些都是你自己找到的基本错误。