2013-02-10 88 views
0

我想獲得一些關於如何讓我的程序輸出用戶需要的列數的提示。例如,如果用戶爲termsPerLine輸入2,那麼程序應該在兩列中輸出由Juggler系列生成的值,或者如果用戶輸入3爲termsPerLine,3列等等。輸出變量是firstTerm。任何援助將是偉大的。在循環底部輸出所需的列數

if ((count % termsPerLine) == 0) 
{ 
    cout << "\n"; 
} 

或本:在循環頂部

#include <string> 
#include <iostream> 
#include <cmath> 
#include <iomanip> 

using namespace std; 

int ValidateInput(string Prompt); 

int main() 
{ 
    int count; 
    double Odd; 
    double Even; 
    long long int firstTerm; 
    int noOfTerms; 
    int termsPerLine; 

    cout << "Program will determine the terms in a Juggler Series" << endl << endl; 

    firstTerm = ValidateInput("Enter the first term: "); 

    noOfTerms = ValidateInput("Enter the number of terms to calculate (after first): "); 

    termsPerLine = ValidateInput("Enter the terms to display per line: "); 

    cout << "First " << noOfTerms << " terms of JUGGLER SERIES starting with " << firstTerm << endl; 

    count = 1; 

    do 
    { 
     if (firstTerm % 2 == 0) 
     { 
      firstTerm = pow(firstTerm , 0.5); 
      cout << setw(16) << firstTerm << endl; 
      count++; 
     } 
     if (firstTerm % 2 != 0) 
     { 
      firstTerm = pow(firstTerm, 1.5); 
      cout << setw(16) << firstTerm << endl; 
      count++; 
     } 
    } 
    while (count <= noOfTerms); 

    cout << endl; 
    system("Pause"); 
    return 0; 
} 


int ValidateInput(string Prompt) 
{ 
    int num; 
    cout << Prompt << endl; 
    cin >> num; 

    while (num <= 0) 
    {  
     cout << "Enter a positive number" << endl; 
     cin >> num; 
    } 

    return num; 
} 
+0

WTP?計算您打印的項目數量。當它是'termsPerLine'的倍數時,打印換行符。 – Barmar 2013-02-10 19:43:46

回答

1

試試這個

if ((count % termsPerLine) == termsPerLine) 
{ 
    cout << "\n"; 
} 
0

剛修好你的循環爲:

for (count = 1; count <= numOfTerm; count++) 
{ 

if(firstTerm % 2 == 0) 
    firstTerm = pow(firstTerm, 0.5);       
else 
    firstTerm = pow(firstTerm, 1.5); 


if(count % termPerLine != 0) 
    cout << setw(15) << firstTerm; 
else 
    cout << setw(15) << firstTerm << endl; 

};