Is there a way to loop to the beginning?

I have two questions. The first is that is there a way to make the user be able to shut down the program at anytime by typing something in a certain string?

Or does this work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
int main(){
string a;
while(a=="SHUT DOWN")
{
cout<<"Shutting down"<<endl;
return 0}

cout<<"Asking the user for some input. Type in 'SHUT DOWN' to stop"<<endl;
cin<<a

system("pause")
return 0;
}


My 2nd question: is there a way to loop to the beginning of the program once the program is finished? The only way to get out would be to type in 'SHUT DOWN' in the way I showed above.
Last edited on
sort of..

the way it should be done is like this:
1
2
3
4
5
6
7
8
9
10
char a;
while(a != 'n')
{

//code here

cout << "Do you wish to continue? y or n";
cin >> a;

}


string comparison would require the use of the string compare function
std::string, we get reasonable comparison logic auto-magically.

But his logic was definitely all screwed up.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

int main()
{
     std::string input ;

     while ( input != "SHUT DOWN" )
    {
        std::cout << "Type in \"SHUT DOWN\" to stop\n" ;
        std::getline(std::cin, input) ;
    }

    std::cout << "Shutting down" << std::endl ;
}



Topic archived. No new replies allowed.