gpt4 book ai didi

c++ - 输出所需的列数

转载 作者:行者123 更新时间:2023-11-28 07:46:26 28 4
gpt4 key购买 nike

我想获得一些关于如何让我的程序输出用户所需数量的列的提示。例如,如果用户为 termsPerLine 输入 2,则程序应在两列中打印 Juggler 系列生成的值,或者如果用户为 termsPerLine 输入 3,则为 3 列,依此类推。输出变量是 firstTerm。任何帮助都会很棒。

#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;
}

最佳答案

在循环的顶部试试这个:

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

或者循环底部的这个:

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

关于c++ - 输出所需的列数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14801786/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com