PDA

View Full Version : Simple C++ Loops


Zangeel
04-13-2010, 04:08 PM
I'm trying to get back to learning C++. I have a basic code here, and it works but the console application keeps exiting after the loop, how can I prevent the application from exiting?


#include <iostream>

void main()
{
int iNumber;
int iCounter = 0;

std::cout << "How many times to loop?" << std::endl;
std::cin >> iNumber;
if ((iNumber) && (iNumber < 10000))
{
do
{
std::cout << "Looped content" << std::endl;
iCounter = iCounter + 1;
} while (iCounter < iNumber);
}

}

BWiz
04-13-2010, 04:23 PM
I assume you are running this under Windows? The console window is exiting because your program has finished running. If you want to view your output, you can one of the following things:

1) Use the either getch() (conio.h) or cin.get() to pause operation
2) Use system("pause") (don't remember exactly)
3) Execute your program in a console window [Start>run>cmd>%path%to%file/file.exe] rather than launching through explorer

Bwiz.

EDIT: Just a side note, I would make your main() function return an int, since this status code is returned to the operating system detailing the exit status of your program.

Zangeel
04-13-2010, 04:56 PM
:D Thanks worked nicely!