2011-10-19 43 views
0

當我運行程序並選擇溢價它運行它把它然後它也運行常規服務的功能。這個問題在整個問題上處於主要地位。我做錯了什麼? 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; 

} 
+5

IT是正確的...... – Nate

+0

怎麼這麼,我列出了問題,我認爲這個問題是。我還包括完整的代碼。那裏還有什麼? –

+0

作爲一個方面說明,我假設你想讓'P'和'p'運行高級服務,'R'和'r'運行常規服務。這是你想要的嗎?因爲現在如果你輸入'r'或'p'它不起作用,它會到達'break'並立即退出。你想要的是刪除該中斷,如下所示: case'r': case'R': regserv(); case'p': case'P': premserv(daymin,nightmin); –

回答

1

完整代碼調用switch語句regserv()premserv(),但你無條件的開關後,立即打電話給他們兩個。

對開關的評論表明您誤解了開關的語義。它不會「選擇」接下來會發生什麼,它實際上在交換機內部運行代碼,所以在交換機之後不需要調用它們。但是,不要忘了分配premiumservregularserv變量和switch語句進行輸出,如:

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

如何保存功能的輸出。多數民衆贊成什麼即時嘗試在switch語句 –

+0

後將功能的輸出分配給變量,就像你在switch語句之外做的那樣。 – ObscureRobot

+0

Im沒有在switch語句中分配功能的輸出。那就是我在交換機下面的那兩行代碼 –

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的情況下,您可以將返回值分配給一個變量,然後該變量不會再被引用。

0

嗯,我不是100%確定你的程序應該做什麼,但是從它的外觀來看,當用戶輸入'R'或'r'時用戶需要調用regserve功能,而當用戶輸入'P'或'p'。但是在您的轉換聲明中,當您收到「p」或「r」時,您立即致電休息。什麼突破將會在switch語句結束後繼續執行,而不是落入'P'或'R'的情況。

解決這個問題的最好方法是爲每個案例添加一個明確的調用來爲regserve或premserve添加顯式調用(您可以通過不包括中斷來讓它崩潰,但它通過顯式聲明操作更易於閱讀針對每種情況)。