Thread: Easy Question

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    12

    Smile Easy Question

    I'm new to C++ working on my second project. I'm working on a project that will begin with a list of items which the user is asked to pick from. Once the user enters the option, the screen is cleared and we begin a new screen. How does this work? Thanks for your replies.

    Gagi

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Code:
    
    int var, choice;
    do
    {
      cin>>choice;
    
      switch(choice)
      {case 1:
         //function one call
         break;
         ......
        default:
          cout<<"Bad Choice, input again.";
          var=1
          break;
       }
    }while(var);

  3. #3
    Registered User
    Join Date
    Sep 2001
    Location
    England
    Posts
    121
    The easiest way would be to print the available options, accompanied by a number (cout<<"1. First option \n2. Second option etc." << endl;
    then ask for a choice from the user. There are different ways of doing this, you could get an input to an int, but the program would fail if you input a char, so I would take the input as a char, then when you need to use the input do something like this:

    char choice;

    cout << "Enter your choice";
    cin >> choice;
    while( choice != 49 || choice !=50 )
    {
    cout << "Input was invalid, enter your choice again";
    cin >> choice;
    }
    clrscr();
    switch (choice)
    {
    case 49://49 is 1 in ASCII
    //add code for the first choice here
    break;

    case 50:
    //second choice
    break;

    default:
    cout << "The program should stop anything except a valid input getting to here, so this should never be seen" << endl;
    break;
    }

    There are 'better' ways but as a beginner I found the above method relatively error proof and easy to understand.

    Clearing the screen is a little bit more complicated. If you are using borland or another dos compiler #include<conio.h> and just call clrscr(); whenever you want to clear. If not this is the MSVC equivalent:

    #include <windows.h>

    void clrscr()
    {
    COORD coordScreen = { 0, 0 };
    DWORD cCharsWritten;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD dwConSize;
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    GetConsoleScreenBufferInfo(hConsole, &csbi);
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
    GetConsoleScreenBufferInfo(hConsole, &csbi);
    FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
    SetConsoleCursorPosition(hConsole, coordScreen);
    }

    then call clrscr();, that's taken from the FAQ on this site, there are som eother methods there too if neither work.

    I havent tried any of this yet so there might be a few errors, I havent done pure c++ for a while
    Last edited by Robert602; 01-26-2002 at 04:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. easy Vector contructor question
    By noodle24 in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2006, 07:43 PM
  2. Another Embarassingly Easy Question
    By almo89 in forum C Programming
    Replies: 2
    Last Post: 02-11-2006, 04:59 PM
  3. This is hopefully an extremely easy question...
    By rachaelvictoria in forum C Programming
    Replies: 2
    Last Post: 11-07-2005, 01:36 AM
  4. 4 easy question
    By Zeratulsdomain in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2005, 10:43 PM
  5. Easy Question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-12-2002, 12:19 PM