I am trying to make a text based game for a university assignment, this is an extension of a game i coded previously, with a fhew improvements, i originally had it where you choose two potions, you mix them together, and give it to the princess, i now have to include an option of 3 potions when i choose a potion.(hopefully that makes sense)


what i have so far:

#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std;

void printStoreMenu();
void printBuyPotionMenu();

int main()
{

cout << "************************************************************\n";
cout << "* 		  The Sick Princess                   	    *\n";
cout << "************************************************************\n";
cout << "Welcome. You have now arrived at Nakor's Magic Store and    \n";
cout << "have been asked by the King to purchase two magic potions   \n";
cout << "in order to create a remedy that can heal the sick Princess.\n";
cout << "No current potion available BY ITSELF is able to cure the   \n";
cout << "Princess. Your task is to choose the correct combination of \n";
cout << "potions in order to cure the princess.                      \n";


	printStoreMenu();
}


void printStoreMenu()
{
	char choice, potionMix = 'n', playAgain='N';

	do
	{
	cout << "*  	    Welcome to Nakor's Magic Store         *\n";
	cout << "***************************************************\n";
	cout << "* Please choose one of the following:             *\n";
	cout << "* a) Purchase a potion				   *\n";
	cout << "* b) Create a mixture from the potions            *\n";
	cout << "* c) Give potion to the princess                  *\n";
	cout << "* d) Quit                                         *\n";
	cout << "***************************************************\n";
	cout << "Enter your choice:";
	cin >> choice;
	
	switch(tolower(choice))
	{
		case 'a':

		printBuyPotionMenu();
		
		break;
		case 'b':
		if ( potion1 == '1' && potion2 == '2' && potionMix == 'y')

		cout << "\nYou have already mixed the 2 potions. Stop wasting time!";

		else if ( potion1 == '1' && potion2 == '2')
		{
		cout << "\nYou mixed the 2 potions. Good Luck!.";
		potionMix = 'y';
		}
		else
		cout << "\nYou haven\'t yet bought the 2 potions.";
		break;
		case 'c':
		if ( potion1 == '1' && potion2 == '2' && potionMix == 'y')
		{
			cout << "\nYou gave the potion mix to the princess and...";
			cout << "\n\n...you failed miserably!\n";
			cout << "The Princess will sleep forever, and\n";
			cout << "you will spend the rest of your days in the SCSSE's";
			cout << " dungeons and sewers.\n";
		do
		{
		cout << "\nDo you want to try again? ";
		cin >> playAgain;
		}while (tolower(playAgain) != 'y' && tolower(playAgain) != 'n');
		

		if(tolower(playAgain == 'y'))
		{
			potion1 = '?';
			potion2 = '?';
			potionMix = 'n';
		}
		else
		{
			choice = 'd';
			cout << "\nBetter luck next time!\n\n";
		}
		}
		else
		
		cout << "\nYou haven\'t yet mixed 2 potions.";
		break;

		case 'd':
		cout << "\nThanks for playing!" << endl;
		choice = 'd';
		break;

		default:
		cout << '\n' << choice << " is not a valid choice. Try again\n";
	}
	}while(choice!='d');

}

void printBuyPotionMenu()
{
	char potion1='?', char potion2='?';
 	char potionChoice;

	do
	{
	cout << "***************************************************\n";
	cout << "*		Available Potions		   *\n";
	cout << "***************************************************\n";		
	cout << "*The following potions are available for purchase:*\n";
	cout << "* a) Hogswart   				   *\n";
	cout << "* b) Elven Tears 				   *\n";
	cout << "* c) Light of Heaven 				   *\n";
	cout << "* d) Back to main page 			   *\n";
	cout << "***************************************************\n";
	cout << "Enter your choice:";
	cin >> potionChoice;

				   
	
	switch(tolower(potionChoice))
	{
		case 'a':
		potion1 = 1;
		printStoreMenu();
		break;

		case 'b':
		potion2 = 1;
		printStoreMenu();
		break;

		case 'c':

		cout << " You chose Light of Heaven " << endl;

		
		break;

		case 'd':

		cout << " back to previous menu " << endl;
		printStoreMenu();

		break;

		default:

			cout << " Invalid Choice " << endl;
	}
	}while( potionChoice !='d');

	return;

}
/*
void givePotions(int mix)
{
	switch(mix)
		{
		case 0: cout << "You failed miserably!\n";
		cout << "You did not mix 2 different potions as required.\n";
		cout << "The princess has died and you will be hanged for it!\n";
		break;
		
		case 1: cout << "You failed the Princess!\n";
		cout << "The Princess will sleep forever, and\n";
		cout << "you will spend the rest of your days in the King's";
		cout << "dungeons and sewers\n";
		break;
		
		case 2: cout << "The Princess recovers but develops chronic baldness.\n";
		cout << "Your punishment is to look after the Princess' wig";
		cout << " forever!\n";
		break;
		
		case 3: cout << "The Princess recovers, however she is not 100% cured!\n";
		cout << "You have been banished from the Kingdom and \n";
		cout << "have been forbidden in marrying your betrothed, the lovely";
		cout << " lady Java.\n";
		break;
		
		case 4: cout << "Full dosages of Hogswart and Elven Tears should never be";
		cout << " mixed\n";
		cout << "The princess has died and you will be hanged for it!\n";
		break;
		
		case 5: cout << "Full strength Hogswart and Light of Heaven make a lethal"; cout
		<< " mix\n";
		cout << "The princess has died and you will be hanged for it!\n";
		break;
		case 6: cout << "The Princess recovers completely!!!\n";
		cout << "You are made Baron of SCSSE.";
		break;
		}
	return;
}
*/

i have created a few functions that will be required, (one if commented out to be included later)

I have also only worked on getting it to move to the multiple choice of potions, i am now stuck on how to get it to return the potions i chose, to the void printStoreMenu() function, thats my first and biggest issue at the moment, im thinking its something quite simple, but cant seem to get my head around it.


Hopefully it makes sense what i am asking, thank you for any assistance.

Well, without you pasting the exact errors, its hard to see small mistakes. But in your printBuyPotionMenu() method, you declare a couple of variables of type char. Then you decide to set that char to an int. char is an integral type that usually contains members of the execution character set — in Microsoft C++, this is ASCII. Therefore, setting the char to an int value, will give it the corresponding ASCII value.

I am also unsure how

char potion1='?', char potion2='?';

Even compiles. If you are separating variables of the same type, you only use the type definition for the first variable followed by a comma.

That last part is valid, if confusing. There is a little-used comma operator in C and C++ that acts more or less the same as a semi-colon; it is mostly used to allow multiple statements in the initialization and increment sections of for() loops, but it works elsewhere as well. Unfortunately, this probably wasn't what the OP had in mind; as you stated, the typical idiom would be simply

char potion1 = '?', potion2 = '?';

I can think of a simple to this problem, but have two suggestions. First, declare an enumeration type to represent the potions, rather than using an int or a char:

enum POTIONS {NONE, HOGWART, ELVEN_TEARS, LIGHT_OF_HEAVEN};

The reason for this is that you can declare variables in this new type which can only be one of those three values, and it makes it a lot less likely that you'll make a mistake and use an invalid value.


Second, I recommend replacing the separate potion1 and potion2 variables with a two-element array, which you would declare in the main() menu:

POTIONS potions[2] = {POTIONS.NONE, POTIONS.NONE};

This not only makes the solution easier, it also makes the connection between things clearer, and makes it easier when the time comes to use a combination of more than two potions.

The simple solution, then, is to have printBuyPotionsMenu() run once when it is called, and return the potion value that was selected:

POTIONS printBuyPotionMenu()
{
    char potionChoice;

    while (true)
    {
        cout << "***************************************************\n";
        cout << "*        Available Potions           *\n";
        cout << "***************************************************\n";
        cout << "*The following potions are available for purchase:*\n";
        cout << "* a) Hogswart                      *\n";
        cout << "* b) Elven Tears                    *\n";
        cout << "* c) Light of Heaven                    *\n";
        cout << "* d) Back to main page                *\n";
        cout << "***************************************************\n";
        cout << "Enter your choice:";
        cin >> potionChoice; 
        
        switch (potionChoice)
        {
            case 'a':
                return POTIONS.HOGWART;
            case 'b':
                return POTIONS.ELVEN_TEARS;
            case 'c':
                return POTIONS.LIGHT_OF_HEAVEN;
            case 'd':
                return POTIONS.NONE;
            default:
                cout << "Invalid choice" << endl;
        }
    }
}

You would then have to put the value in the potions[] array, in the current value; you will need a counter variable to keep track of the current potion number:

int potionCount = 0;
 // ... code here ...
    potions[potionCount] = printBuyPotionMenu();
    if (potions[potionCount] != POTIONS.NONE)
        potioncCount++;

Something like this should at least point you in a helpful direction.

This article has been dead for over six months. Start a new discussion instead.