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.
// 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>
usingnamespace 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;
}
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.
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.
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.
@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.