954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ Return to Point in Programme

Hello DaniWeb Users,
I am new to the forum however from what I have seen it is exceptionally good! I have searched the forums already and found various questions relating to a similar problem as mine however I am unable to work out how I can amend this for my problem. During web research I found various suggestions such as While loops (which I can't see how it can run in my piece of code), GOTO (which apparently is a big no) and Switch.

I am trying to make a Text Based Adventure Game to run in CMD as my first more advanced C++ project.
I am currently at the stage of creating the menu option however I am struggling to get it to return to the main menu once the user has selected their text colour through the GameOptions.

The code which I have so far for the menu is:

#include <iostream>
#include <string>
#include <windows.h>


int main()
{
    using namespace std;

    string Option;

    cout << "\t********************************************************\n";
    cout << "\t*             Welcome to Dragon Slayer                 *\n";
    cout << "\t*           Please select your option now              *\n";
    cout << "\t*                                                      *\n";
    cout << "\t*            PlayGame   GameOption  Quit               *\n";
    cout << "\t********************************************************\n\n\n";

    cin >> Option;
    {

    if (Option == "PlayGame")
        {

        string Name;

        cout << "Great, let's get started!\n\n";
        cout << "What is your name?\n";
        cin >> Name;
        cout << "\n\nHello " << Name << ", how are you doing? My name is Alexander.";
        }

    if (Option == "GameOption")
        {
        string Colour;

        cout << "You are now able to change the text colour!\n\n";
        cout << "Green\n";
        cout << "Yellow\n\n";
        cin >> Colour;
            {
            if (Colour == "Green")
            system("Color 0a");

            if (Colour == "Yellow")
            system("Color 0e");

            system("cls");

            cout << "Sample Text\n\n";

            }

        }

    if (Option == "Quit")
        {
        cout << "What a shame, I was looking forward to it\n";
        return 0;
        }
}


}


How would I get it so once the user has selected their colour (lines 35 to 53) it goes back to the original menu whilst keeping the colour they chose?

Thank you

AHarrisGsy
Light Poster
31 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Just replace the lines 21 and 22 with these two lines:

while(true) {
  cin >> Option;


The rest can remain the same, for now.

mike_2000_17
Posting Virtuoso
Moderator
2,136 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
 

Just replace the lines 21 and 22 with these two lines:

while(true) {
  cin >> Option;

The rest can remain the same, for now.


Thanks for the help Mike, it however does not seem to work how I expected it to.
I have inserted the code and it comes up with the "Sample Text" once you have chosen a colour however it then doesn't go back to the main menu.
It instead just sits there with a blank space beneath and the ability to enter any random thing and enter it without effect.

AHarrisGsy
Light Poster
31 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Move the while(true) { line to line 11. That should display the menu again every time.

mike_2000_17
Posting Virtuoso
Moderator
2,136 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
 
Move the while(true) { line to line 11. That should display the menu again every time.


Thanks again for the help, now it says that Option is undeclared and that it is the first use of this function?
How can that be when it has clearly been declared only a couple of lines above it?

AHarrisGsy
Light Poster
31 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Post the updated code.

mike_2000_17
Posting Virtuoso
Moderator
2,136 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
 
Post the updated code.


Thanks for the help which I'm sure you would have given, I have however found another way around it by using a Char and a loop as opposed to attempting to loop a string.

Thanks anyway!

AHarrisGsy
Light Poster
31 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: