C++ 输出所需的列数
我想了解一些技巧,如何让我的程序输出用户想要的列数。例如,如果用户为termsPerLine输入2,则程序应在两列中打印由Juggler系列生成的值,或者如果用户为termsPerLine输入3,则应在三列中以此类推。输出变量是firstTerm。任何援助都将是巨大的C++ 输出所需的列数,c++,formatting,format,multiple-columns,C++,Formatting,Format,Multiple Columns,我想了解一些技巧,如何让我的程序输出用户想要的列数。例如,如果用户为termsPerLine输入2,则程序应在两列中打印由Juggler系列生成的值,或者如果用户为termsPerLine输入3,则应在三列中以此类推。输出变量是firstTerm。任何援助都将是巨大的 #include <string> #include <iostream> #include <cmath> #include <iomanip> using namespace
#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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int ValidateInput(字符串提示);
int main()
{
整数计数;
双奇数;
双倍偶数;
长期的;
中午;
国际术语汇编;
cout在循环的顶部尝试以下操作:
if ((count % termsPerLine) == 0)
{
cout << "\n";
}
if((计数%termsPerLine)==0)
{
cout只需将循环修复为:
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;
};
for(count=1;count WTP?计算您打印的项目数。如果是termsPerLine
的倍数,则打印换行符。
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;
};