简体   繁体   English

使用按引用传递和按值传递参数的故障执行程序

[英]Trouble executing program that uses both pass by reference and pass by value parameter

I keep getting errors when I try to execute the following program, and I do not know if something is wrong with one of my functions or what(the way I wrote it). 当我尝试执行以下程序时,我不断收到错误消息,我不知道我的函数之一或内容(编写方式)是否有问题。 I know something is wrong with line 129 in my code, but whenever I try to make changes other syntax errors show up. 我知道代码中的第129行有问题,但是每当我尝试进行更改时,都会出现其他语法错误。 Basically there is something wrong with my syntax and since I'm learning debugging I'm having a hard time with this. 基本上,我的语法有问题,并且由于我正在学习调试,因此很难。

#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;
}

Output should look like this: 输出应如下所示:

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

I've looked at your program for 5 min and there are so many errors I'm not even sure where to begin... 我已经看了您的程序5分钟,有很多错误,我什至不知道从哪里开始...

  1. Line 131 and 145: You forgot the do-while condition. 131行和145行:您忘记了do-while条件。
  2. Line 141: Should probably replace "ans2" by "ans3" (shitty names for variables by the way) 第141行:应该用“ ans3”替换“ ans2”(顺便说一下变量的俗名)
  3. Line 89: Semester is undeclared, seems like you were supposed to use "var1" instead, which is another horrible name. 89行:未声明学期,似乎您应该改用“ var1”,这是另一个可怕的名称。
  4. Line 12: You declare the function "other_student_services_fees" yet you never define it 第12行:您声明了函数“ other_student_services_fees”,但从未定义过

At that point I just gave up on you. 那时我只是放弃了你。 We are not here to do your homework for you, those are basics errors you could have found yourself. 我们不是在这里为您做功课,这些是您可能会发现的基本错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM