My question is, how do I get my output to display like the photo below?
If you run my code, you'll see that everything works, but the numbers don't have the correct amount of space between them. Also, when I input a single digit number, like one for example, the first two rows don't line up with the others. I've tried using setw, but still couldn't get it to work.
// Program prompts user to enter integer from 1-100
// and then using a loop construct, print values
// starting from the value entered to 100.
// In addition, print 8 numbers per line.
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
int minNumber;
int maxNumber = 100;
cout << "Enter a number: ";
cin >> minNumber;
while ( minNumber < 1 || minNumber > 100 )
{
cout << "Invalid number. Enter a number between";
cout << " 1 and 100.\n";
cout << endl;
cout << "Enter number: ";
cin >> minNumber;
}
int newLine = 0;
for ( int number = minNumber; number <= maxNumber; number++)
{
cout << number << " "" "" ";
if ( ++newLine == 8 )
{
cout << endl;
newLine = 0;
}
}
return 0;
}
It's just an example of what it should look like. The program should take any input from the user between 1-100 and display the numbers from what they input to 100.
Ok, thanks for the tip. I had another question though. Why is there a larger space between the columns? In the example of how it should look, the columns are closer together.
Thank you. Gah, I can't wait until I can stop asking these newbie questions and finally understand what the hell it is I'm doing. We keep getting jam packed with new info every few days, I barely have time to go over what we just learned.
Don't forget to try things out with small test programs of a few lines. Above all, you're the boss, not the machine, there is no magic, and nothing will break. :)
Oh, I've been trying. This program has given me the most trouble so far. I've been working on it non-stop for the past few days and something else just caught my eye.
The 100 for my output isn't lined up correctly. I thought setw fixes that?
If you are required to have 8 columns per line then what you see is what you get unless the number chosen to start with gives you an exact number of columns/lines