Hi I learning c++ through a book but I have found that one of the examples provided by the book is an infinite loop. So I was wonderring if anyoone can help me stop it. I tried using the 'break;' statement in every 'for' body to stop the loop but the results aren't what the book shows. So if any one can help me find the loop and end it but at the same time do not affect the the output of the program.

This is the code:

using namespace std;

int main()

{


cout << "Counting forward:\n";
for (int i = 0; i < 10; ++i)
{
    cout << i << " ";
}

cout << "\n\nCounting backward:\n";
for (int i = 9; i >= 0; --i)
{
    cout << i << " ";
}

cout << "\n\nCounting by fives:\n";
for (int i = 0; i <= 50; i += 5)
{
    cout << i << " ";
}

cout << "\n\nCounting with null statements:\n";
int count = 0;
for ( ; count < 10; )
{
    cout << count << " ";
    ++count;
}

cout << "\n\nCounting with nested for loops:\n";
const int ROWS = 5;
const int COLUMNS = 3;
for (int i = 0; i < ROWS; ++i)
{
    for (int j = 0; j < COLUMNS; ++j)
    {
        cout << i << "," << j << "  ";
    }

    cout << endl;
}

return 0;


}

Recommended Answers

All 4 Replies

There are no infinate loops in that code, it would be best if you could show the relevent loop.

Thanks for the reply. I just created a new project and copy and paste the code and it work. I think I wasn't using win32 console application.

Learning basic C/C++? Better to use GCC compilers and tools rather than MS ones - they are more "standard". You can install MingW or Cygwin on Windows, or a Linux virtual machine (with Virtualbox for example), and learn there. MS Visual Studio is specific to the Windows environment, and does not translate to other systems very well at all.

FYI, I have 30+ years software development experience in Unix/Linux as well as Windows environments. I only program in Windows (Visual Studio) when I have to, because of its "unique" requirements. IE, code developed for Windows ONLY runs on Windows! Whereas code developed for open source systems (posix primarily) will run pretty much anywhere, including Windows.

@rubberman

Thank you for the advice. I will download GCC compilers and learn on those. I wasn't aware of the limitations using visual studio. Thank you for the advice again. I appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.