Thread: text based interfaces...

  1. #1
    Registered User xconspirisist's Avatar
    Join Date
    Nov 2003
    Posts
    44

    text based interfaces...

    hey, id like to start writing text based interfaces - linux users - like the grub boot loader, the rhl kazdu system, etc... like, where you select options from a menu... but all in text mode..

    Im sorry for the extremely vague disctiption, but I dont know what they're called...

    Anyway. id like to start them, anyone know any tutorials I could follow ?

  2. #2
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    I don't use linux, but I'm guessing you just mean something like this?

    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
     int i;
     cout << "1. Do Something." << endl;
     cout << "2. Do Something Else." << endl;
     cout << "3. Exit." << endl;
     cout << "Enter choice: ";
     cin >> i;
     switch(i) {
      case 1:
       cout << endl << "Choice 1" << endl;
       break;
      case 2:
       cout << endl << "Choice 2" << endl;
       break;
     }
     return 0;
    }
    Do not make direct eye contact with me.

  3. #3
    Registered User xconspirisist's Avatar
    Join Date
    Nov 2003
    Posts
    44
    nah, not really. hmm, if it helps, the sort of thing im thinking of, is where you select items from a menu, but with the up and down arrow keys.. just like in the slackware install... then you tap enter to select that menu item..

    aha !! if you remember installing windows.. you use a boot cd - for xp right ? you know that text based bit at the start, you select the hdd and aggree to the liecense, copy over files from the cd to begin the install ? I want that sort of menu interface..

    I hope someone can remember that and help me explain it ?

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Unfortunately, since there are no standard ways to clear the screen or place the cursor at any particular point, that sort of thing becomes difficult. It's more difficult if you want the code to be portable, but then again maybe you dont want that. Supposing we have a clear screen function, here is a possiblity:
    Code:
    string items[3]={"Do something","Do something else","Quit"};
    int i;
    int currentlyselected;
    char k;
    
    do
    {
            ClearScreen();
            for (i=0;i<3;i++)
            {
                  if (i==currentlyselected)
                       cout << "* ";
                  else
                       cout << "  ";
                  cout << items[i] << endl;
            }
            cin.get() >> k;
            switch (k)
            {
            case '8':
                  if (i==0) i=2;
                  else       i--;
                  break;
    
            case '2':
                  if (i==2) i=0;
                  else       i++;
                  break;
    
            case 13:
                  break;
            default:
                  break;
            }
    } while (k!=13);
    That code was written entirely in this edit box here, so don't give me crap about it not working. It's a close approximation of how you could implement this sort of menu system. You'll have to work out how to clear the screen in Linux, I have no idea how to do it.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Originally posted by bennyandthejets
    That code was written entirely in this edit box here, so don't give me crap about it not working.
    Mine was too, but it works .
    You'll have to work out how to clear the screen in Linux, I have no idea how to do it.
    Safe way:
    Code:
    for(int i = 0; i < 46; i++) {
     cout << endl;
    }
    Slower, non-portable way (linux / unix only):
    Code:
    system("clear");
    Do not make direct eye contact with me.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > where you select options from a menu... but all in text mode..
    ncurses has additional sub-libraries like 'menu' which make this a lot easier to do
    Gory details
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    Just to add to Slem's reply, the ncurses programming HOWTO is here,
    which I'm finding more useful as a tutorial than the other one.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    For simpler interfaces than those made with curses libraries, GNU readline offers functionality for command-line clients such as command history.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User xconspirisist's Avatar
    Join Date
    Nov 2003
    Posts
    44
    I dont have nclasses.h ...

    Any thingelse I can use ?

    I'd like to write my own header - but two things are stopping me, first, I need a tutorial on header files, second, I dont know how to do color coding - im using unix.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I dont have nclasses.h ...
    Huh, I thought we were all talking about ncurses.h

    You must have a pretty lame unix if you don't have curses installed.
    Have you considered downloading the ncurses package for your particular flavour of unix.

    By the way, saying unix is even worse than saying windows, since there are so many more versions which come under the umbrella term 'unix'
    Be specific!!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User xconspirisist's Avatar
    Join Date
    Nov 2003
    Posts
    44
    I meant nCURSES.h, typo. Im using Linux, Fedora Core 1, with the Anjuta IDE.


    Code:
    //#include <curses.h> 
    #include <ncurses.h>
    
    int main()
    {	
    	initscr();			/* Start curses mode 		  */
    	printw("Hello World !!!");	/* Print Hello World		  */
    	refresh();			/* Print it on to the real screen */
    	getch();			/* Wait for user input */
    	endwin();			/* End curses mode		  */
    
    	return 0;
    }

    Using this code, it compiles okey, but I get ' undefined refernece to varaible 'initscr' line 6 ' while building..

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Using this code, it compiles okey, but I get ' undefined refernece to varaible 'initscr' line 6 ' while building..
    Oh right, now you should have said so before!!!

    Code:
    gcc prog.c -lcurses
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Nov 2002
    Posts
    82
    Try looking at my text-based GUI program. This one's for DOS (and Windows) though.

  14. #14
    Registered User xconspirisist's Avatar
    Join Date
    Nov 2003
    Posts
    44
    does anyone know how to add ' -lcurses ' to the compile in the Anjuta IDE ?

  15. #15
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63
    rez, how did you do that??? Could you post the source please?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Text based game..
    By MipZhaP in forum C++ Programming
    Replies: 8
    Last Post: 02-28-2005, 08:35 AM
  2. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  3. Looping Text based battle
    By pujuman in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2004, 05:24 AM
  4. making a text based game
    By MisterSako in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2004, 01:20 PM
  5. Text Based Game
    By drdroid in forum C++ Programming
    Replies: 2
    Last Post: 02-18-2002, 06:21 PM