While loop with counter

I need to make a while loop to count to 100 with only 10 numbers on each line and setw(5) to separate them. I am struggling to get this done correctly. I can do it with 10 while loops but I want to do it in one while loop with resetting the counter after 10 numbers. Here is what I have that worked but I was told it was the wrong way

#include <iostream>
#include <iomanip> //need this to use setw(5)

using namespace std;

int main()
{
int num = 1;
cout << "\n\n\n"; //Is there a better way to do this than \n times 5?

while (num <= 100)
{
cout << setw(5) << num++ << "\t";
}
cout << "\n";
return 0;
}
How about this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iomanip>
#include <iostream>

auto main() -> int
{
   const int MAX{ 100 };
   int count{ };

   while (count < MAX)
   {
	  std::cout << std::setw(5) << ++count;

	  // You should come up with an if-statement that will output
          // a newline after every 10'th element. The best way to achieve
          // this is using the % operator. It should not be difficult to figure
          // it out. :)
          // Hint if variable % X == ? { insert the newline; }
   }
   std::cout << std::endl;

   return 0;
}
Last edited on
Thank you for your help. He said an if statement would be better to do this but he wants it in a while loop instead. This is actually sad as I have already taken both C++ 1 and C++ 2 but came out of that teachers class with not much knowledge at all. The teacher who is giving me side work to do during summer is not my teacher and is just trying to help me to be a stand alone programmer. Is there a way to do it without an 'if' statement?
Your idea was great. I did it like this and it was so much nicer than all the if statements I had. Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <iomanip> //need this to use setw(5)
#include <cmath>

using namespace std;


int main()
{
	int counter = 1;
	int num = 100;
	cout << "\n\n\n"; //Give padding at top?
	
	while (counter <= num)
	{
		cout << setw(5) << counter++; //while loop to display 1 - 10 on screen
		if (counter % 10 == 1)
		{
			cout << "\n";
		}
	}
	
    return 0;
}



Last edited on
You are welcome. And if you happen to read this reply, here is a way to do it with a nested while loop. (As i understand from your last reply this is what you were asked to use instead of an if-statement):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iomanip>
#include <iostream>

auto main() -> int
{
   const int MAX{ 100 };
   int count{ };
   int linecount{ };

   while (count < MAX)
   {
	  std::cout << std::setw(5) << ++count;

	  while (++linecount && count % 10 == 0)
	  {
		 std::cout << std::endl;
		 linecount = -1;
	  }
   }
   std::cout << std::endl;

   return 0;
}


Alternatively you could use the while loop to say:

1
2
3
4
5
while (count % 10 == 0)
{
    std::cout << std::endl;
    break;
}


You can also introduce another variable and write:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iomanip>
#include <iostream>

auto main() -> int
{
   const int MAX{ 100 };
   const int CNT{ 10 };
   int count{ };
   int linecount{ };

   while (count < MAX)
   {
	  std::cout << std::setw(5) << ++count;

	  while (++linecount == CNT)
	  {
		 std::cout << std::endl;
                 linecount = -1;
	  }
   }

   std::cout << std::endl;

   return 0;
}


And i bet there is several more solutions to make this work (without break;) to achieve the same thing. (That said, my solutions to the problem may not be the most elegant under the great C++ coding sun, but they work ;-)).
Last edited on
Topic archived. No new replies allowed.