有没有办法在 2 列中显示我的素数列表输出?

Is there a way to display my prime number list output in 2 columns?

我正在参加我的第一次编程 class,这是我第一次发帖。当我遇到困难时,我可以在这个网站上找到以前项目的帮助,我希望我做对了。

我已经完成了下面的程序,只显示 0 到 100 之间的质数,用于我的 C++ 入门 class。

唯一让我有点困扰的是它在一个单独的列中,我想采取额外的步骤让它看起来很漂亮并在几个列中显示数字。我尝试使用“\t”,但无法正常工作。关于我可以添加到我的代码中的任何想法? 我想我可以使用数组来完成,但我们没有在 class 中介绍它,我还不应该使用它们。

挑战是:

"Use the isPrime function that you wrote in Programming Challenge 21 in a program that stores a list of all the prime numbers from 1 through 100 in a file."

这是我的代码:

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;

bool isPrime(int);

int main()
{
static int num1=0;

cout<<"Listed below is all prime numbers from 1 through 100."<<endl<<endl<<endl;

do
{
    num1++;
    if (isPrime(num1))
    {
    cout<<num1<<endl;
    }
}
while (num1<100);

cout<<endl;

return 0;
}

bool isPrime(int num1)
{

bool primeNum=true;
for (int i=2;i<num1;i++)
{
    if (num1%i==0)
    {
        primeNum=false;
    }
}
return primeNum;
}

提前感谢您的任何意见,

查找cout.width()

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;

bool isPrime(int);

int main()
{
    static int num1 = 0;

    cout << "Listed below is all prime numbers from 1 through 100." << endl << endl << endl;

    int column = 0; // column variable
    int width = 10; // column width size

    do
    {
        num1++;
        if (isPrime(num1))
        {
            cout.width(width); // set column's width
            cout << num1;

            if (column == 1) { // if prime number is printed in column 2
                cout << endl; // add new line
                column = 0; // set column to first
            }
            else {
                column++; // increase column index
            }
        }

    } while (num1<100);

    cout << endl;

    return 0;
}

bool isPrime(int num1)
{
    // error: your isPrime returns true when num1 is 1 or 2. change it
    if (num1 == 1 || num1 == 2) return false;

    // your isPrime
    bool primeNum = true;
    for (int i = 2; i<num1; i++)
    {
        if (num1%i == 0)
        {
            primeNum = false;
        }
    }
    return primeNum;
}

我刚刚意识到要求我将列表存储 到文件的问题。所以我重写了,这是我的新代码:

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
using namespace std;

bool isPrime(int);

int main()
{
int num=0;

cout<<"This Program will store a list of only the prime numbers "<<endl;
cout<<"between 0 and 100 to the text file \"PrimeNumberList\"."<<endl<<endl;
cout<<"Find the list by using the file explorer to search for \"PrimeNumberList.txt\"."<<endl;


ofstream outFile;
outFile.open("PrimeNumberList.txt");

if (outFile.fail())
{
    cout<<"Error opening \"PrimeNumberList.txt\" for output."<<endl;
    return 1;
}

for (int i=1;i<100;i++)
{
    if(isPrime(i))
    {
        outFile<<i<<endl;
    }
}
return 0;
}

bool isPrime(int num1)
{
if (num1==1)return false;
bool primeNum=true;
for (int i=2;i<num1;i++)
{
    if (num1%i==0)
    {
        primeNum=false;
    }
}
return primeNum;
}