llSpectrell
llSpectrell

Reputation: 59

Output desired number of columns

I was wanting to get some tips on how to get my program to output a desired number of columns by the user. For example, if the user enters 2 for termsPerLine, then the program should print the values generated by the Juggler series in two columns, or if the user enters 3 for termsPerLine, 3 columns and so forth. The output variable is firstTerm. Any assistance would be great.

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

Upvotes: 0

Views: 1001

Answers (2)

Pepe
Pepe

Reputation: 1

just Fix your loop as:

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;

};

Upvotes: 0

Thomas Matthews
Thomas Matthews

Reputation: 57749

Try this at the top of the loop:

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

or this at the bottom of the loop:

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

Upvotes: 1

Related Questions