newbie c++ help

Started by
8 comments, last by igni ferroque 19 years, 5 months ago
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 :)
Advertisement
Without "cout'ing" the menu again, I would suggest you learn curses or another console based output system.
Why wouldn't you want to cout it again? It's no effort.
i kno but is there no a quicker way ?
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 ...
It's fast enough, surely? I don't know of a platform-independent way to do it without curses.
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.
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 ?
Look into your compilers console.h file for a start .. there are often helper functions in their.
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.
Free Mac Mini (I know, I'm a tool)

This topic is closed to new replies.

Advertisement