close on command

This is a discussion on close on command within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I am pretty new to c++ programming,and i have learned quite a few things,but somehow i either just ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    4

    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.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    The console window will close once the main function ends. That's all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    my guess is that it's your code(it shouldn't close until everything is over)


    EDIT: Ninja'd by Elysia

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    4
    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;
    }
    Last edited by Lurid; 08-08-2010 at 03:32 PM.

  5. #5
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    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!"}
        }
    }
    Last edited by jerimo; 08-08-2010 at 03:42 PM. Reason: Fixing up code

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    4
    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.

  7. #7
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    your welcome!
    If you have any more problems then you just have to post them!

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Aug 2010
    Posts
    4
    Oh Duh! i completely forgot about that. Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Write in many command prompts
    By cfriend in forum Windows Programming
    Replies: 1
    Last Post: 09-15-2004, 01:32 AM
  2. Replies: 4
    Last Post: 11-14-2001, 09:28 AM
  3. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21