-
close on command
Hello everyone,
I am pretty new to c++ programming,and i have learned quite a few things,but somehow i either just haven't learned it yet or over looked it,is how to make the program window close when i want it to.
Because i have been trying to projects for practice,but ones that require the user to enter a number so many times(lets say 10-15) before something is output,is kind of hard to do when the window closes automatically after 4-5 entries.
If anyone could help,that would be great.
-
The console window will close once the main function ends. That's all.
-
my guess is that it's your code(it shouldn't close until everything is over)
EDIT: Ninja'd by Elysia
-
Okay hmm...then maybe if i post my code it will help.
I am figuring its the break; command,but i'm not sure how to go about it without having an infinite loop
Code:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "Enter any number other than 5" << endl;
cin >> num;
for(;;)
{
if(num == 5)
cout << "Hey! you werent supposed to enter 5!" << endl;
else
cout << "Enter any number other than 5" << endl;
break;
}
char f;
cin >> f;
return 0;
}
-
You should put in a specific count to end the for loop
Also not sure why you made your code that way, you should maybe make a loop with ev erything inside(not one input outside and the a never ending loop for the rest)
I would do something along the lines of:
Code:
int choice=0;
int main(){
for(int count=15;count!=0;count--;){
cout<<"Enter a number, other than 5";
cin>>choice
if(choice==5){cout<<"I said not 5!"}
}
}
-
Well the count i would have done later,this is part of a practice project,and the modifying it to do a count was the next step,i was just going in order. But thank you for the help and the tip on input.
-
your welcome!
If you have any more problems then you just have to post them!
-
Code:
else
cout << "Enter any number other than 5" << endl;
break;
Contrary to what you might think, the break isn't part of the else statement.
Use braces to put more than one line inside an else block.
As it is, the break will always break your loop after the first iteration in your code.
-
Oh Duh! i completely forgot about that. Thank you