Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Resetting C++ program instead of closing compiler

Posted on 2006-05-21
4
Medium Priority
?
529 Views
Last Modified: 2008-02-01
OK, I know this is a stupid and probably easy question, but is there a way in C++ to reload your program in the same window after getting your results instead of "pressing any key to continue" to close the window?  I want to give the user an option to reload the program until he/she presses Q for quit.  How can I accomplish this?
0
Comment
Question by:MSAJM
  • 2
4 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 16730078
>>but is there a way in C++ to reload your program in the same window after getting your results instead
>>of "pressing any key to continue" to close the window?

A simple solution, is to put your program in a sub function, instead of in the main() function, and then give the user the option from the main function to call the sub function until Q is pressed.
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 16730098
Sure. Just use a loop in your 'main()' for that, e.g.

#include <iostream>
#include <string>
using namespace std;

int main () {

    while(true) { // loop until the user wants to exit

        // your code and functionality here


        // check if user wants to quit
        string response;
        cout << "If you want to quit, type 'Q' and hit 'Enter' or anything else to continue" << endl;
        cin >> response;

        if(response[0] == 'Q' || response[0] == 'q') break;
    }

    return 0;
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 16730108
Or, a more C-like solution:

#include <stdio.h>

int main () {

    while(true) { // loop until the user wants to exit

        // your code and functionality here


        // check if user wants to quit

        printf("If you want to quit, type 'Q' and hit 'Enter' or anything else to continue\n");
        char response;
        response = getc(stdin);

        if(response == 'Q' || response == 'q') break;
    }

    return 0;
}
0
 

Author Comment

by:MSAJM
ID: 16730117
Thank you!!!
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question