开关和功能

时间:2011-10-19 22:04:31

标签: c++

当我运行程序并选择premium时,它运行它然后它还运行常规服务的功能。问题出在线上。我做错了什么? 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;

}

3 个答案:

答案 0 :(得分:1)

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

关于交换机的评论表明你误解了交换机的语义。它没有“选择”接下来会发生什么,它实际上在交换机内运行代码,所以你不需要在切换后调用它们。但是,不要忘记分配premiumservregularserv变量并在switch语句中执行输出,例如:

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

答案 1 :(得分:0)

看起来你的问题可能出现在这部分代码中:

  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;

在这里,您将打开服务代码并调用regserv和premserv。然后你调用这些函数,但是在regserv的情况下,你将返回值赋给一个永远不会再引用的变量。

答案 2 :(得分:0)

嗯,我不是100%确定你的程序应该做什么,但是从用户看来,当用户输入'R'或'r'并且在用户输入时保留它时,你想要调用regserve函数'P'或'p'。但是在你的switch语句中,当你收到'p'或'r'时,你会立即打电话给break。什么中断将在switch语句结束后继续执行,而不是落入'P'或'R'的情况。

解决这个问题的最佳方法是为每个案例添加一个显式调用regserve或premserve(你可以通过不包括中断来让它掉头但是通过明确地说明动作来更容易阅读每个案例)。