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?
#include <iomanip>
#include <iostream>
auto main() -> int
{
constint 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;
}
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?
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):
#include <iomanip>
#include <iostream>
auto main() -> int
{
constint MAX{ 100 };
constint 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 ;-)).