Hello,

I need to prompt the user if they want to actually execute the program or not.

Like, "Do you want to run the program, y=yes and n=no"

Then user enters y or n and the program either runs or quits.

I was thinking that would be like:

char answer;

cout<<"Do you want to run the program, y=yes and n=no";
cin>>answer;

if
(answer=y)
//I don't know what to put here to execute the program

else
//Also don't know what to put here to quit

Recommended Answers

All 19 Replies

You are technically already running the program, so you really only need to check to see if the user doesn't want to run it:

if (answer == 'n')
    return 0;

Otherwise, execution will continue.

or rather

#include<stdlib.h>

if (answer == 'n')
       exit(0);

Maybe the following code will help you:

#include <iostream>

using namespace std;

int main(void)
{
    char answer;

    cout << "Do you want to run this program ? [Y][N] ";

    cin >> answer;

    if(answer == 'y' || answer == 'Y')
    {
        /* ... There has to be nothing here ...*/
        /* Only if answer is 'y' or 'Y' the program will run */
        /* Every other answer will cause the program not to run */
    } else {
        /* Don't run the program */
        cout << endl;
        exit(0);
    }

    /* Put your other instructions here */

    cout << endl;
    cout << "Running the program ..." << endl;

    return 0;
}

Hope this helps !

Don't stop C++ programs with exit() function (except on severy run-time errors).
Use return exit_code_value; in the main.

commented: He said just what I wanted to say. Right Advice +2

>Don't stop C++ programs with exit() function (except on severy run-time errors).
I fully agree with ArkM.
exit(0) is evil
I would extend ArkM's advice that you should not use exit(0) even in case of runtime errors. You should use exception handling mechanism.
Ideally, All functions, sub routines should end back to main() and main should return something.

Don't stop C++ programs with exit() function (except on severy run-time errors).
Use return exit_code_value; in the main.

OK, agreed, but what's so wrong about it? I tought I only had to be careful with the 'abort()' instruction ...

>but what's so wrong about it?
Destructors aren't called when you use exit. It's not a C++ friendly function.

>but what's so wrong about it?
Destructors aren't called when you use exit. It's not a C++ friendly function.

You're absolutely right, but in this case it didn't make sense because I wasn't using any objects ...

>it didn't make sense because I wasn't using any objects ...
What do you think cin and cout are?

>What do you think cin and cout are?

==> Objects ;)

Yet another tip: never use exit() in destructors. The program has undefined befavior if exit() is called during the destruction of an object with static storage duration.

However return n; in the main performs local objects destruction then calls this terrible exit function ;)...

>However return n; in the main performs local objects destruction
extend it to any function. Not just main()

So is there a way to do it to do this with making 'y' the only way to continue the program and 'n' to exit. Not y and other chars to continue and anything but y exit.

Not sure how to do that with if statements.

if ( option == 'y' )
  // continue
else if ( option == 'n' )
  // exit
else
  // print error
commented: How do you come up with these things??I mean: Wow... ;) +14
1.
      if ( option == 'y' )
   2.
      // what would i put here for continue?
   3.
      else if ( option == 'n' )
   4.
      exit(0); //that is correct right?
   5.
      else
   6.
      cout<<"You have entered an invalid choice, please try again";
       cin>>option;

I just dont know what to put for continue, am I going the right way here?

Anyone know how to help plz?

In the continue bit, call the subroutine which does whatever the program does - e.g main menu or whatever

I figured it out:

cout  << "Do you want to play? (y = yes, n = no):  ";

            cin.sync();

            cin.get(userchoice);

 

            userchoice = tolower(userchoice);  //convert user's answer to lowercase

                                                                                

            while (userchoice != 'y' && userchoice != 'n')

            {

                        cout << "Invalid response.  Enter a y for yes or n for no: ";

                        cin.sync();          // clear out buffer

                        cin.clear();          // set for a good read

                        cin.get(userchoice);

            }

 

            if (userchoice == 'n')

                        return 0;
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.