Sign in to follow this  
bige

newbie c++ help

Recommended Posts

hi i need some help plz im creating a text based adventure in c++ as a console app ive cout'd a main menu then cleared the screen, then cout'd a option screen ive then coded a if statement with 3 options 1. start game 2. enter password 3. view credits' when i choose to view the credits i enter option 3 my code then clears the screen then cout'd a credits screen. my question is how do i go back to the main menu screen instead of of cout'in the whole options screen again ? pleease help ta :)

Share this post


Link to post
Share on other sites
make sure you are doing basic program organization ... like putting the cout statements inside of functions like:

diplayMainMenu(), displayOptionsMenu(), ...

and not copying the code into each branch that needs to show it again ...

Share this post


Link to post
Share on other sites
bige, how do you have your program structured? If you have created separate functions for displaying your menu and credits screens you can call the function whenever you need to display the screen.


#include <iostream>
#include <stdlib.h>

using namespace std;

void ShowMenu()
{
cout << "Menu Screen!" << endl;
cout << "1) Do something" << endl;
cout << "2) Do something else" << endl;
cout << "9) Quit" << endl;
}

void DoSomething()
{
cout << "Yay, I'm doing something!" << endl;
}

void DoSomethingElse()
{
cout << "Yay, I'm doing something else!" << endl;
}

int main()
{
bool exit = false;
char choice = 0;

while (exit == false)
{
// Show our menu
ShowMenu();

// Ask player for choice
cout << "Enter your choice: ";
cin >> choice;

switch (choice)
{
case '1':
DoSomething();
break;
case '2':
DoSomethingElse();
break;
case '9':
exit = true;
break;
default:
cout << "Unrecognised option" << endl;
};

}

return 0;
}




In the case above, I've split the DoSomething/DoSomethingElse methods into a separate function that is called when the user enters a certain value. You could quite easily adapt this to match your program by changing the functions to something like "ShowCredits". If you need any further explanation about the above code, ask here.

Share this post


Link to post
Share on other sites
Quote:
Original post by bige
cheers for the help
is there a way when the program runs to set the console size
as in using code so its the same size on all computers ?
No. If you want to do fancy console stuff you should use curses. iostream is an interface to a buffer of text, and has no knowledge of the windowing system or terminal. Curses can identify the terminal and certain attributes, such as the console width and height.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this