Can't get output to display 8 numbers per line

I've literally been trying to figure this out all night, but I can't seem to get my output to display 8 numbers per line, so 8 columns of numbers. I can only get them to list back to back and wrap around to the next line.

Sorry in advance if my code doesn't come out correctly. Whenever I go to format it, it shows "javascript:tx('code')" and I can't seem to get the preview to show anything to see if I'm doing it right.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Program prompts user to enter integer from 1-99
// and then using a loop construct, print values
// starting from the value entered to 100.
// In addition, print 8 numbers per line.
#include <iostream>
using namespace std;

int main()
{
    int minNumber;
    int maxNumber = 100;

    cout << "Enter a number between 1 and 99: ";
    cin >> minNumber;
    cout << endl;

    for ( int number = minNumber; number <= maxNumber; number++)

        cout << number << " ";

    cout << "\n";
    return 0;
}
Last edited on
1
2
3
4
5
int k = 0;
    for ( int number = minNumber; number <= maxNumber; number++)
        {
                cout << number << " "; if(++k == 8) {cout << endl; k = 0;}
        }
Thank you for that, but can you explain? There's nothing in our textbook that describes doing this.
https://msdn.microsoft.com/en-us/library/dy3d35h8.aspx
Yes, I understand the rules of the de- and increment operators. I'm talking about how the if statement is set up. Why are you initializing a new variable, etc..

Yes, these are newbie questions, but I'm a newbie. Instead of just having an answer given to me, I'd like to understand what's happening.
So your teacher doesn't permit you to declare any variables of your choice?
We can, but I like to keep mine relevant to what the program is doing, which is why I was confused about k. It just seems so out of place.
Can you even understand this?
1.
if(++k == 8)

2.
{cout << endl; k = 0;}

3.
if(++k == 8)

4.
{cout << endl; k = 0;}

5.
if(++k == 8)

6.
{cout << endl; k = 0;}

7.
if(++k == 8)

8.
{cout << endl; k = 0;}

etc

Why? These are very very simple statements.
Last edited on
Well excuse me...

Don't understand why everyone in this community has to say something in a condescending tone. We all start from somewhere. It takes me a little longer to grasp something. Sue me.
How long have you been learning C++?
Depending on that, I can teach you line by line, step by step.

If you look closely, these are very simple statements.
1.
int k = 0; // Assignment statement

2.
if(++k == 8) // If statement, prefix-increment, comparison statement

3.
cout << endl; // Basic std::cout statement

4.
k = 0; // Assignment statement

Have you learned if-statement?
Last edited on
I started a little less than a month ago. Last Wednesday, we started working on all the if-statements and this Monday we started the while and for loops.

Everything in my original code, I understand. I understand relational, arithmetic, and logical operators. I understand initializing a variable.

Last edited on
@SakurasouBusters
Your tone is fairly condescending. JSYK.

A new variable is not strictly necessary, but it is useful. Compare:

1
2
3
4
5
6
7
8
9
10
11
12
13

for (int n = minvalue; n <= maxvalue; n++)
{
  // Print the number (and some space)
  cout << n << " ";

  // Check to see if we have printed 8 numbers...
  if ((n - minvalue) % 8) == 7)
    // yes: print a newline
    cout << "\n";

}
int columns = 0;
for (int n = minvalue; n <= maxvalue; n++)
{
  // Print the number (and some space)
  cout << n << " ";

  // Check to see if we have printed 8 numbers...
  if (++columns == 8)
  {  // yes: print a newline
    cout << "\n";
    columns = 0;  // and reset column counter
  }
}

As a side note, there is a potential maximum value issue with a loop terminating on <=; I would have written it with a different loop:

1
2
3
4
5
6
7
8
9
10
11
if (minvalue <= maxvalue)
{
  int columns = 0;
  int n = minvalue;
  do {
    cout << n 
         << ((++columns == 8) ? "\n" : " ");
    columns %= 8;
  } \
  while (n++ < maxvalue);
}

You may also want to pay attention to the final newline.

Hope this helps.
@Duoas Thank you, I really appreciate it. All I was wanting was a breakdown of what was going on. This helped a lot.
Last edited on
Topic archived. No new replies allowed.