closing a program whenever the user cares to do so.

Mar 9, 2016 at 8:01am
i am trying to right a program where whenever the user inputs exit the program will close.
so the user can exit the program no mater where he is at in the program.







#include <iostream>
#include <string>

std::string run;
std::string exit;
std::string c;



int main()
{

std::cout << " would you like to run the prgram\n\n" <<
" yes for yes\n no for no or exit\n\n";

std::cin >> run >> exit;


if (run == "yes" || run == "Yes")
{
std::cout << "running....\n\n";
std::cin >> c >> exit;

}


else if(exit == "no" || exit == "No" || exit == "exit" || exit == "Exit")
{
std::cout << " Press any letter key to close\n ";
std::cin >> c;
}

else
{
std::cout << "\n\n\n something is wrong here\n ";
std::cin >> c;
}
}

Last edited on Mar 9, 2016 at 8:26am
Mar 9, 2016 at 5:44pm
OP wrote:
i am trying to right a program where whenever the user inputs exit the program will close.
so the user can exit the program no mater where he is at in the program.


Is this academic? Or is there some actual reason that the user can't use "ctrl+c" like the rest of the world does?
Mar 10, 2016 at 12:52am
i am new to programming and i don't know how to ask for what i am looking for.
well it is more for debugging the code within the program.

so how do i right a debugger in the code.
Mar 10, 2016 at 1:01am
When you are comparing a std::string and a char*, do

 
exit.compare("exit") == 0


That will let you compare the two.

If you want to exit from the main function and quit the program at a specific point, add a return statement like:

 
return 0;


or in your case

1
2
3
4
5
6
else if (exit.compare("exit") == 0 || exit.compare("Exit") == 0)
{
    std::cout << " Press any letter key to close\n ";
    std::cin >> c;
    return 0;
}
Topic archived. No new replies allowed.