2015-10-09 44 views
0
#include <iostream> 
#include <iomanip> 
#include <cstdlib> 

using namespace std; 

int userInput(int& Length, char& AuthorLevel); 
int computePay(int& Length, char& AuthorLevel); 

int main(void) 
{ 
    int Length;  // The length of the story. 
    int counter = 0; 
    int numA = 0; 
    int numB = 0; 
    int numC = 0; 
    char AuthorLevel; // The level of the author. 
    float PayOut;  // The final payout. 
    float averagePayout = 0.0; 
    float highestPayout = 0.0; 

    userInput(Length, AuthorLevel); 
    computePay(Length, AuthorLevel); 

    system("pause"); 
    return 0; 
} //end main() 
//================================================================== 

int userInput(int& Length, char& AuthorLevel) 
{ 
    cout << "Please enter the word count of the story (-1 to stop): "; 
    cin >> Length; 
    if(Length != -1) 
    { 
     cout << "Now enter the author's level (A, B, or C): "; 
     cout << "Level: "; 
     cin >> AuthorLevel; 
     cout << endl; 
    } 
    else 
    { 
     system("pause"); 
     return 0; 
    } 
} 

//================================================================== 
int computePay(int& Length, char& AuthorLevel) 
{ 
    float PayOut;  // The final payout. 
    int numA = 0; // Number of A's that have been printed. 
    int numB = 0; // Total number of B's. 
    int numC = 0; // Number of C's. 
    float averagePayout = 0.0; 
    float highestPayout = 0.0; 
    int counter = 0; // The number of times the program has ran. 

    while(Length != -1) 
    { 
     if(Length < 7500 || Length != -1) 
     { 
     PayOut = 0.08 * Length; 
     } 
     else if(Length < 8000) 
     { 
     PayOut = 600; 
     } 
     else if(Length < 17500) 
     { 
     PayOut = 0.075 * Length; 
     } 
     else if(Length < 19000) 
     { 
     PayOut = 1313; 
     } 
     else if(Length >= 19000) 
     { 
     PayOut = 0.07 * Length; 
     }; 

     if (AuthorLevel == 'A' || AuthorLevel == 'a') 
     { 
     numA++; 
     PayOut = 1.75 * PayOut; 
     } 
     else if(AuthorLevel == 'B' || AuthorLevel == 'b') 
     { 
     numB++; 
     PayOut = 1.25 * PayOut; 
     } 
     else if (AuthorLevel == 'C' || AuthorLevel == 'c') 
     { 
     numC++; 
     PayOut = 1.00 * PayOut; 
     } 
     else 
     { 
     cout << "That was not a valid input. Please try again." << endl; 
     }; 

     userInput(Length, AuthorLevel); 
     counter++; 
     cout << "The amount the author will make from the story will be: $" << PayOut; 
     cout << endl << endl; 
     cout << "The number of payments calculated is: " << counter << endl; 
     cout << "The number of A's inputted: " << numA << endl; 
     cout << "The number of B's inputted: " << numB << endl; 
     cout << "The number of C's inputted: " << numC << endl; 
     averagePayout = (averagePayout += PayOut)/counter; 
     if(highestPayout < PayOut) 
     { 
     highestPayout = PayOut; 
     } 
     else if(highestPayout > PayOut) 
     { 
     highestPayout = highestPayout; 
     } 
     cout << "The highest payout so far has been: $" << highestPayout << endl; 
     cout << "The average payout is: $" << averagePayout << endl << endl; 
    } 
} 
//=================================================== 

這個程序一直工作之前多次輸出所述第一功能,並且所述第一輸出的第一,第二功能保持用於第一輸入輸出的輸入,以及 - 1不會像它應該那樣停止整個程序。誰能幫忙?問題及與輸出/輸入延遲/複製

回答

0

該程序保持多次輸出所述第一功能的工作和第一輸出的第一,第二功能保持用於第一輸入

你必須從computePayuserInput呼叫輸出的輸入之前。這解釋了上述行爲。

和-1不停止整個程序像它應該。誰能幫忙?

computePay調用userInput後,你沒有任何代碼來跳出循環,如果Length == -1的。因此,您可以從以下printf電話獲得更多的產出。

我的建議:

  1. 變化main稍微使控制的流程比較簡單。更換線路:

    userInput(Length, AuthorLevel); 
    computePay(Length, AuthorLevel); 
    

    通過

    while (1) 
    { 
        userInput(Length, AuthorLevel); 
        if (Length == -1) 
        { 
         break; 
        } 
        computePay(Length, AuthorLevel); 
    } 
    
  2. computePay刪除調用userInput

  3. computePay中刪除while循環。

  4. 以下行背後的邏輯是,我不清楚:

    averagePayout = (averagePayout += PayOut)/counter; 
    

    你應該簡化它。我從該行編譯器警告說:

socc.cc: In function ‘int computePay(int&, char&)’: 
    socc.cc:108:58: warning: operation on ‘averagePayout’ may be undefined [-Wsequence-point] 
      averagePayout = (averagePayout += PayOut)/counter; 

這是你的程序的更新版本:

#include <iostream> 
#include <iomanip> 
#include <cstdlib> 

using namespace std; 

int userInput(int& Length, char& AuthorLevel); 
int computePay(int& Length, char& AuthorLevel); 

int main(void) 
{ 
    int Length;  // The length of the story. 
    int counter = 0; 
    int numA = 0; 
    int numB = 0; 
    int numC = 0; 
    char AuthorLevel; // The level of the author. 
    float PayOut;  // The final payout. 
    float averagePayout = 0.0; 
    float highestPayout = 0.0; 

    while (1) 
    { 
     userInput(Length, AuthorLevel); 
     if (Length == -1) 
     { 
     break; 
     } 
     computePay(Length, AuthorLevel); 
    } 

    return 0; 
} //end main() 
//================================================================== 

int userInput(int& Length, char& AuthorLevel) 
{ 
    cout << "Please enter the word count of the story (-1 to stop): "; 
    cin >> Length; 
    if(Length != -1) 
    { 
     cout << "Now enter the author's level (A, B, or C): "; 
     cout << "Level: "; 
     cin >> AuthorLevel; 
     cout << endl; 
    } 
    return 0; 
} 

//================================================================== 
int computePay(int& Length, char& AuthorLevel) 
{ 
    float PayOut;  // The final payout. 
    int numA = 0; // Number of A's that have been printed. 
    int numB = 0; // Total number of B's. 
    int numC = 0; // Number of C's. 
    float averagePayout = 0.0; 
    float highestPayout = 0.0; 
    int counter = 0; // The number of times the program has ran. 

    if(Length < 7500 || Length != -1) 
    { 
     PayOut = 0.08 * Length; 
    } 
    else if(Length < 8000) 
    { 
     PayOut = 600; 
    } 
    else if(Length < 17500) 
    { 
     PayOut = 0.075 * Length; 
    } 
    else if(Length < 19000) 
    { 
     PayOut = 1313; 
    } 
    else if(Length >= 19000) 
    { 
     PayOut = 0.07 * Length; 
    }; 

    if (AuthorLevel == 'A' || AuthorLevel == 'a') 
    { 
     numA++; 
     PayOut = 1.75 * PayOut; 
    } 
    else if(AuthorLevel == 'B' || AuthorLevel == 'b') 
    { 
     numB++; 
     PayOut = 1.25 * PayOut; 
    } 
    else if (AuthorLevel == 'C' || AuthorLevel == 'c') 
    { 
     numC++; 
     PayOut = 1.00 * PayOut; 
    } 
    else 
    { 
     cout << "That was not a valid input. Please try again." << endl; 
    }; 

    counter++; 
    cout << "The amount the author will make from the story will be: $" << PayOut; 
    cout << endl << endl; 
    cout << "The number of payments calculated is: " << counter << endl; 
    cout << "The number of A's inputted: " << numA << endl; 
    cout << "The number of B's inputted: " << numB << endl; 
    cout << "The number of C's inputted: " << numC << endl; 

    // FIXME 
    averagePayout = (averagePayout += PayOut)/counter; 
    // FIXME 

    if(highestPayout < PayOut) 
    { 
     highestPayout = PayOut; 
    } 
    else if(highestPayout > PayOut) 
    { 
     highestPayout = highestPayout; 
    } 
    cout << "The highest payout so far has been: $" << highestPayout << endl; 
    cout << "The average payout is: $" << averagePayout << endl << endl; 

    return 0; 
} 
//=================================================== 
+0

謝謝!對於averagePayout =(averagePayout + = PayOut)/ counter;我想,如果它是不明確的,以計算已打印的所有支出,遺憾的平均值。隨着NUMA,麻木了,NUMC,變量,我試圖讓一共有多少A的,B公司和C公司已經輸入。 – DPuskas96