how do i make this program loop?

Sorry for the long code, im trying to make this program loop up to the top after the user enters that they would like to continue, but I can't figure it out. Area i attempted to make the program repeat is at the bottom of the code

Code:

/*Halloween ASCII Art Program


*/

#include<iostream>
#include<iomanip>
#include<string>
#include<windows.h>
#include<algorithm>
#include<stdlib.h>

using namespace std;

int main()
{
//declare variables
string picture = "";
string Next = "";


//colors 1-blue 2-green 3-cyan 4-purple

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
cout << "Which picture would you like to see? " << endl;
cout << "Type Witch, Pumpkin, or Ghost: " << picture;

getline(cin, picture);
transform(picture.begin(), picture.end(), picture.begin(), toupper);
while (picture != "STOP")
{

if (picture == "WITCH")
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
cout << " ( '' ) " << endl;
cout << " ( _ * " << endl;
cout << " * ( / \ ___ " << endl;
cout << " " " _/ / " << endl;
cout << " ( * ) ___/ | " << endl;
cout << " ) '' _ o)'-./__ " << endl;
cout << " * _ ) (_, . $$$ " << endl;
cout << " ( ) __ __ 7_ $$$$ " << endl;
cout << " ( : { _) '--- $\ " << endl;
cout << " ______'___//__\ ____, \ " << endl;
cout << " ) ( \_/ _____\_ " << endl;
cout << " .' \ \------''. " << endl;
cout << " |=' '=| | ) " << endl;
cout << " | | | . _/ " << endl;
cout << " \ (. ) , / /__I_____\ " << endl;
cout << " '._/_)_(\__.' (__,(__,_] " << endl;
cout << " @---()_.'---@ " << endl;
}//end if

else if (picture == "PUMPKIN")
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 1);
cout << " ,---._" << endl;
cout << " ,~~,-._ `._ " << endl;
cout << " v'~ `-. `. " << endl;
cout << " _,- ~. \ " << endl;
cout << " .' ,--`. `\_ " << endl;
cout << " V-'~ ,'~~~`-. `-. " << endl;
cout << " ___ /_/~~~) ` `. `._ " << endl;
cout << " ____,---' ; V `.__ `--, `; " << endl;
cout << " `-._ ; `. ____) : " << endl;
cout << " `.; ; .' ,' _ | " << endl;
cout << " ; | ; ,';'~~~`--' `; : " << endl;
cout << " ,': .-' `,' __ __ : | " << endl;
cout << " )_ `-, ___ __ (__:__) ; ; " << endl;
cout << " _,---. \___,'` ~`---; `-. ||| ; ; " << endl;
cout << " _,-/ :; ! ` `| `-. |~~~~| ; : " << endl;
cout << " _,-' /~ .,' ; !! `.. ``. `. : ; | : " << endl;
cout << " ,' ,-' .; `; !! _,'-' ,--._ ====\__/===: `. " << endl;
cout << " .' ,-' ,--. ~~`-. !! ~ ,' ` `./ \ | | " << endl;
cout << " .' :; ,' \ !: . ;--.__ `;. |. ~.| : ; " << endl;
cout << " .' ,; ' ,-'~~`-. ,! ;-' #; `;. \____/ : `. " << endl;
cout << " .' ,; /__ `-._,'!!( _(0'~~`-' `;. `. ; ; " << endl;
cout << " .' ,; ,' `---._(0)) !! ~ _,-, `; `. ; : " << endl;
cout << " ; ,; ,' ;;-.__,-._~~~ !!__,--::::|. `;: : `; ) " << endl;
cout << " ; :| ,' ;/;;; :::;;;;----'|:: |::;\/#. `; | ) ; " << endl;
cout << " : |: ,' ,' :/ :;; \/))):;;::/ ::' ##: ;; ; ; | " << endl;
cout << " | :| :; : `' \/ \/ `' `' ##; ; .' ,' ; " << endl;
cout << " : |; || .' ;\. ____ __,--._ ##; ;' .'--' ; " << endl;
cout << " _,--`. `. :: `./; /\/;:;,-' `-. `--.__ .'~ ,'~ " << endl;
cout << " / ;. `; ``. :::;\;.-'~~`./~~\/\ .. _ ::: --. ' ,-'~ " << endl;
cout << " / . `. `; ` ~~~ ;~ ~~~~~~`--.__~~`-. ::: ) ~ " << endl;
cout << "/' ;`--`. `. `. :; `; ; `---`._ ,' " << endl;
cout << "`. \/ `-.` `_,_ `:,-'-. `. :_,_ ; `--' " << endl;
cout << " `. `. ` (___)-: ( ) :--,-'- -(___),'~~~`. " << endl;
cout << " {_ `. `.___.' ( ; ;) :((:)): " << endl;
cout << " `. ` `--' `.___.' " << endl;
cout << " `. `. ;;:::; " << endl;
cout << " `- `. ;;;. .; " << endl;
cout << " `-. `.__ \\;;; - ;; // " << endl;
cout << " `. ` `--..___ \\,--v-, // " << endl;
cout << " `--._ ~~~~~`)____(// " << endl;
cout << " ) ~~ ~~~~~~; " << endl;
cout << " `. ~~ ------; " << endl;
cout << " `.~~_ ______,' " << endl;
cout << " `. `.--';: |: " << endl;
cout << " ; `. Cc; Cc " << endl;
cout << " `. ; __ " << endl;
cout << " `. `-' ~\ " << endl;
cout << " `-.__,--'~ " << endl;
}//end else if

else if (picture == "GHOST")
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 3);
cout << " .---. " << endl;
cout << " / \ " << endl;
cout << "---- _ | O O | _ --- " << endl;
cout << "\ ~-. `. .' .-~ / " << endl;
cout << " \ ~-_> <_-~ / " << endl;
cout << " \ / " << endl;
cout << " \ / " << endl;
cout << " \ / " << endl;
cout << " \ / " << endl;
cout << " \ / " << endl;
cout << " \ / " << endl;
cout << " \ \ " << endl;
cout << " \ \ " << endl;
cout << " \ \ " << endl;
cout << " \ \ " << endl;
cout << " \ l " << endl;
cout << " \ / " << endl;
cout << " V " << endl;
}//end else if

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
cout << "Go again? [Stop or Next]: " << endl;
string exit = "Stop";
cin >> exit;
if (exit == "Stop")
return 0;
else if (exit == "Next")
//repeat @ begining of int main () <---------------------------------


system("pause");
return 0;

}//end while

}//end of main function
Last edited on
You do use loops already. Why would one more be a problem?
1
2
3
4
5
6
7
int main()
{
  std::string exit = "Stop";
  do {
    // everything that you want to repeat
  } while ( std::cin >> exit && exit != "Stop" );
}
Last edited on
One small thing..
Regardless of the else if(exit == "Next") the while will go into its next iteration so the condition is not required.
And also if (transform(exit.begin(), exit.end(), exit.begin(), toupper) == "STOP") can be used instead of if (exit == "Stop") just like how you did it with picture, to make sure that the user doesn't have to type exact cases. ;)

keskiverto's approach is pretty neat but yours is fine too.

Oh and please use coding tags it helps us read your code (http://www.cplusplus.com/articles/jEywvCM9/)
Last edited on
One more thing.
Please, use code tags <> when posting code. See http://www.cplusplus.com/articles/jEywvCM9/
Systematic&intuitive indentation&whitespace is important too. It helps to see the logic.

For example, your post ends:
1
2
3
4
5
6
7
8
9
10
11
12
    string exit = "Stop";
      cin >> exit;

      if (exit == "Stop")
         return 0;
      else if (exit == "Next")
         //repeat @ begining of int main () <---------------------------------
         system("pause");

      return 0;
   } //end while
} //end of main function 

The comment tells that brace on line 11 is the end of some loop, but I won't trace it now. Not from that unindented post.

IF line 4 test is true THEN line 5 ends the entire program.

IF line 6 test is true THEN line 8 runs the external program.

Line 10 ends the entire program regardless of what the user input was.
What is the point of a loop, if you will bail out on first time?
1
2
3
4
5
6
7
8
9
10
11
12
while ( cond ) {
  // code
  return 0;
}
return 0;

// does in practice same as

if ( cond ) {
  // code
}
return 0;

Topic archived. No new replies allowed.