gpt4 book ai didi

c++ - 开关和功能

转载 作者:行者123 更新时间:2023-11-28 08:15:36 26 4
gpt4 key购买 nike

当我运行程序并选择 premium 时,它会运行它,然后它还会运行常规服务的功能。问题出在 int main 上。我做错了什么? IT 说我没有足够清楚地解释问题,不知道为什么,所以这里有更多的文字来解决这个问题。

premiumserv = premserv(daymin, nightmin); 
regularserv = regserv ();

完整代码如下

/************************************************/
/* Author: Sam LaManna */
/* Course: CSC 135 Lisa Frye */
/* Assignment: Program 3 Cell Phone Bill */
/* Due Date: 10/20/2011 */
/* Filename: program3.cpp */
/* Purpose: This program calculates and */
/* prints the bill for a cellular */
/* telephone company */
/************************************************/



//TODO:
//Fix account number issue: large numbers cause intfinte loop. Maybe use string?
//Complete premium and regular service calculation functions
//Output function - Figure out how to print



#include <iostream> //Basic input/output
#include <iomanip> //Manipulators

using namespace std;

void instruct(); //Function declaration for printing instructions
int accnum (); //Function declaration for account number
char servcode (); //Function declaration for service code
float regserv (); //Function declaration for regular service
float premserv (int& daymin, int& nightmin); //Function declaration for premium service
void output (float premiumserv, float regularserv); //Function declaration for printing results

int main()
{
int accountnum = 0; //Var for account number
char servicecode = 'a'; //Var for service code
float premiumserv = 0; //Var for premium service total
float regularserv = 0; //Var for regular service total
int daymin = 0;
int nightmin = 0;

instruct(); //Function call to print instructions

accountnum = accnum(); //Function call to input account number

servicecode = servcode(); //Function call to get service code

switch(servicecode) //Switch to determine which calc to use
{
case 'r':
break;
case 'R':
regserv ();
break;
case 'p':
break;
case 'P':
premserv (daymin, nightmin);
break;
}

premiumserv = premserv ( daymin, nightmin); //Function call for regular service

regularserv = regserv (); //Function call for premium service

cout <<"total" << premiumserv <<"day" << daymin << "night " << nightmin;

return 0;
}



/************************************************/
/* Name: instruct */
/* Description: Prints instructions to user */
/* Parameters: N/A */
/* Return Value: N/A */
/************************************************/

void instruct()
{
cout << "\n" << "This program will calculate the cellular telephone bill for a given customer when " << "\n"
<< "given the correct inputs. ";
//Prints instructions to user

return;
}

/************************************************/
/* Name: accnum */
/* Description: Get user account number */
/* Parameters: N/A */
/* Return Value: accnum */
/************************************************/

int accnum()
{
int accnum = 0; //Define var for account number
char accyn = 'n'; //Define char for user verify
int i = 1; //Define var for sentinel loop


do {
cout << "\n" << "\n" << "Please enter your account number: ";
cin >> accnum;
while ((accnum < 0) && (i<3))
{
cout << "\n" << "Error - invalid number, please try again.";
cout << "\n" << "Please enter your account number: ";
cin >> accnum;
i++;
if (i == 3)
{
cout << "\n" << "Too many invalid attempts, program terminating." << "\n"
<< "Have a nice day. " << "\n" << "\n";
exit (0);
} //end if
} //end while
cout << "\n";
cout << "Your account number is: " << accnum << "\n"
<< "Is this correct? [y/n] ";
cin >> accyn;
} while (accyn != 'y'); //end do/while loop


return accnum;
}

/************************************************/
/* Name: servcode */
/* Description: Get service type */
/* Parameters: N/A */
/* Return Value: servcode */
/************************************************/

char servcode()
{
char servcode = 'a'; //Define var for service code
char serviceyn = 'n'; //Define var for user verify
int i = 1; //Define var for sentinel loop

do {
cout << "\n" << "Please enter your service code, [R]egular or [P]remium:" << "\n";
cin >> servcode;
while ((servcode != 'R' && servcode != 'P' && servcode != 'r' && servcode != 'p' ) && (i < 3))
{
cout << "\n" << "Error - invalid service code, please try again. ";
cout << "\n" << "Please enter your service code: ";
cin >> servcode;
i++;
if (i == 3)
{
cout << "\n" << "Too many invalid attempts, program terminating." << "\n"
<< "Have a nice day. " << "\n" << "\n";
exit (0);
} //end if
} //end while
cout << "\n" << "You entered: " << servcode << "\n"
<< "Is that correct? [y,n]";
cin >> serviceyn;
} while (serviceyn != 'y'); // end do/while loop

return servcode;
}

/************************************************/
/* Name: regserv */
/* Description: Calculations for regular serv */
/* Parameters: N/A */
/* Return Value: regserv */
/************************************************/

float regserv ()
{
int regmin = 0; //Var for number of minutes used
float regserv = 0; //Var for cost for regular service


cout << "\n" "Please enter the minutes used: " << "\n";
cin >> regmin;
regserv = (regmin - 50) * 0.2 + 10;

return regserv;

}

/************************************************/
/* Name: premserv */
/* Description: Calculations for premium serv */
/* Parameters: N/A */
/* Return Value: premserv */
/************************************************/

float premserv (int& daymin, int& nightmin)
{
float daytotal = 0; //Var for day total
float nighttotal = 0; //Var for night total
float premserv = 0; //Var for premium service cost

cout << "\n" << "Please enter the number of minutes used durring the day (6AM - 6PM): " << "\n";
cin >> daymin;
cout << "\n" << "Please enter the number of minutes used durring the night (6PM - 6AM): " << "\n";
cin >> nightmin;

daytotal = (daymin - 75) * 0.1;
nighttotal = (nightmin - 100) * 0.05;
premserv = 25 + daytotal + nighttotal;

return premserv;

}

最佳答案

您在 switch 语句中调用了 regserv()premserv(),但是您在切换后立即无条件地调用了它们。

关于 switch 的评论表明您误解了 switch 的语义。它不会“选择”接下来要发生的事情,它实际上是在开关内部运行代码,因此您不需要在开关之后调用它们。但是,不要忘记分配 premiumservregularserv 变量并在 switch 语句中执行输出,例如:

...
case 'R':
regularserv = regserv();
cout << ... regularserv ...
break;
...

关于c++ - 开关和功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7828790/

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