Macmo13
Macmo13

Reputation: 5

User defined row, column output in C++

I have a 2 part question. I am showing a user defined array. User input is # of rows, columns, and at what interval in the array to put a symbol.

  1. I cannot get an output of the default symbol ****.
  2. Where do I need to insert the interval input to adjust the array accordingly? Inside the function itself or define another function to do this?

I am looking for this result. Input = 1 (row), 4 (columns), 2 (interval). Output would be **?*.

Here is what I have so far.

#include <iostream>

using namespace std;

int rows = 0, columns = 0, interval = 0;
char symbol;
void Display(int rows = 0, int columns = 0, int interval = 0, char symbol = ('*'));

int main()
{

    cout << "Enter number of rows: ";
    cin >> rows;

    cout << "Enter the number of columns: ";
    cin >> columns;

    cout << "Enter the number of the question mark interval: ";
    cin >> interval;

    cout << "\n";
    cout << "How many rows do you want? " << rows << "\n";
    cout << "How many columns do you want? " << columns << "\n";
    cout << "How far between question marks? " << interval << "\n";

    Display(rows, columns,  interval, symbol);

    return 0;
}
void Display(int rows, int columns, int intervals, char symbol)
{
    for (int y = 1; y <= rows; y++)
    {
        for (int x = 1; x <= columns; x++) {
            cout << symbol;
        }
        cout << endl;
    }
    system("pause");        
}

Upvotes: 0

Views: 2816

Answers (1)

sam
sam

Reputation: 2033

The problem is you never assigned * to symbol.

Change

char symbol;

to

char symbol = '*';

Did you know about the disadvantages of global variables. The sooner you learn about the cons the better. Here is a starting point.

Modify the Display function as below:

void Display(int rows, int columns, int intervals, char symbol)
{
    for (int y = 1; y <= rows; y++)
    {
        for (int x = 1; x <= columns; x++) {
            if ((x % (interval + 1)) == 0)   //<--since you want after every intervals, just do a mod operation
                cout << "?";
            else
                cout << symbol;
         }
         cout << endl;
    }
    system("pause");        
}

Here is the working example.

Upvotes: 1

Related Questions