gpt4 book ai didi

c++ - 使用引用传递和值传递参数的麻烦执行程序

转载 作者:行者123 更新时间:2023-12-03 08:55:18 25 4
gpt4 key购买 nike

当我尝试执行以下程序时,我不断收到错误消息,我不知道我的功能之一或什么(我编写它的方式)是否有问题。我知道代码中的第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

最佳答案

我已经看过您的程序5分钟了,有很多错误,我什至不知道从哪里开始...

  • 第131和145行:您忘记了do-while条件。
  • 第141行:应该用“ans3”(顺便说一下变量的俗称)替换“ans2”
  • 第89行:未声明学期,似乎您应该改用“var1”,这是另一个可怕的名称。
  • 第12行:您声明了函数“other_student_services_fees”,但从未定义它

  • 那时我只是放弃了你。我们不是在这里为您做功课,这些是您可能会发现的基本错误。

    关于c++ - 使用引用传递和值传递参数的麻烦执行程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28143892/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com