#include <iostream>

using namespace std;

int main()
{
    char exit;
    
    cout << "Exit? Y / N " << endl;
    cin >> exit;
    
    if ( exit == 'Y' || exit == 'y' )
       { }
       
    if ( exit == 'N' || exit == 'n' )
        system("PAUSE>nul");
        
    else
    { }
    
}

I solved through trial and error, heres the code if you ever want to use it, thanks :)

I guess you could change

else
    { }

To

else
    cout << "Invalid Selection!" << endl;

Hope this helps.

Dani AI

Generated

A few concrete fixes and a safer pattern to make this reliable.

The bug in ’s follow‑up came from using two separate if statements and missing braces: the else ended up attaching only to the second if, so the “re‑ask” code ran when you didn’t expect it. Also avoid naming a variable exit (it hides the library exit function). Use a single validation loop, read a whole line, test only the first character, and normalize case.

Example (robust, portable input loop):

#include <iostream>
#include <string>
#include <cctype>

bool ask_yes_no(const std::string &prompt) {
    std::string line;
    while (true) {
        std::cout << prompt << " (y/n): ";
        if (!std::getline(std::cin, line)) return false;   // EOF/error -> treat as "no"
        if (line.empty()) continue;
        unsigned char ch = static_cast<unsigned char>(line[0]);
        ch = static_cast<unsigned char>(std::tolower(ch));
        if (ch == 'y') return true;
        if (ch == 'n') return false;
        std::cout << "Invalid selection. Please enter 'y' or 'n'.\n";
    }
}

int main() {
    if (ask_yes_no("Exit?")) return 0;
    // continue program...
}

On system("PAUSE>nul"): as asked and hinted, it runs a shell command (Windows only). It’s non‑portable and can be unsafe if you build strings from user input. Prefer std::cin.get() with a prompt for a portable “press Enter to continue”, and use platform APIs (_getch() on Windows, termios on POSIX) only when you really need single‑key behavior.

Quick tips: always use braces to avoid dangling-else bugs, handle EOF from getline, validate input in a loop, and keep prompt wording consistent so users know what behavior to expect.

Actually i'm trying to improve this now, i've figured i'll need a loop?

Because i want to change it so that if the user inputs a different char/int than Y/N it will cls and re-ask question,

Heres what i've got

/* Simple users decision to exit program */

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    char exit;
    
    cout << "Exit? Y / N " << endl;
    cin >> exit;
    
    if ( exit == 'Y' || exit == 'y' )
       { }
       
    if ( exit == 'N' || exit == 'n' )
        getch();
        
    else
        system("cls");
        cout << "Exit? Y / N " << endl;
        cin >> exit;

}

If the user enters another char/int it cls's and asks again but if they enter a char/int again it closes, please help :)

what the meaning of this function, and how and when to use it?? it doesn't requires aspecific header file

system("PAUSE>nul")

system("PAUSE>nul")

system tells C to call a windows command in this case he's calling pause. you can go to a dos prompt and type pause to see the effect.

This is a old post but i thought I would post my suggestion to the problem.

With the code below: The problem that I see is if you type N or n it runs the program, but typing anything else (not just Y) causes it to exit.
So maybe change the wording to say type N to run or any key to exit.

// put a # infront of include I can't figure out how to get it to display here

include <iostream>

using namespace std;
int main()
{
char exit;
cout << "Exit? Y / N " << endl;
cin >> exit;
if ( exit == 'N' || exit == 'n' )
{cout << "Ok, running program";}
else
{cout << "Ok, now exiting" << endl;}
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.