Thread: C programming, DOS, Simple text menu, can you help ??

  1. #1
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    Question C programming, DOS, Simple text menu, can you help ??

    Ok,

    I have previous expierence with coding in Visual C++ for windows with MFC style programs. I have expierence in HTML and javascript along with batch, so dont feel the need to give a huge reply if ya dont want to.

    I know the code to clear the screen, pause the program, and execute external .exe's ( anything from edit.com to the latest and greatest game... ).

    What im trying to do is create a text oriented menu. A selection menu similar to a dos batch file.

    example:

    A = Scandisk
    B = Defrag
    C = Fdisk

    X = exit ( or they can type exit....?? )

    Each letter corresponds to the execution of a program.

    If you could give me some small piece of code that i could just repeat, that'd be great.
    The world is waiting. I must leave you now.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    #include <stdio.h>
    
    int main(){
      int selection;
      printf("1. Do stuff\n2. Do more stuff\n3. Do even more stuff\n");
      printf("Choose an option: ");
      scanf("%d", &selection);
      
      switch(selection){
        case 1:
          /*do your thing for option 1*/
          break;
        case 2:
          /*do your thing for option 2*/
          break;
        case 3:
          /*do your thing for option 3*/
          break;
        default:
          printf("Invalid selection\n");
          break;
      }
      return 0;
    }
    Hope that helps

    -Prelude

  3. #3
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    re to prelude

    Thanx ! That worked great. I just needed something small like that then i can just expand and switch around where needed.

    1 question tho...

    How do you loop it ?

    Like, say i run "so-and-so" program, or "so-and-so" routine, when this outside program, routine, or file is done doing it's own purpose, how do i get back to displaying the selections?

    (example)

    Selection 1 - runs defrag
    then gets returned to a cleared screen with the selections again.
    ( note, not using defrag..... )

    Take the.......nevermind, i just figured it out.

    clrscr();
    system("name of this program.exe");

    Im not doing anything too outstanding and haven't "really" programmend in regular C ( VC++....yes ), and am not really aware of the character differences in the language and the neccesary header files.

    If i have a Q, i know where to come !
    sorry for the trouble...
    The world is waiting. I must leave you now.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    loops work the same in c/c++ mfc etc.

    if you have c++ exp. and mfc exp. and you dont know loops....
    Try the tutorials at this site. There must be one on something as fundamental as loops.....

    quick overview...

    counted loop use for

    for(i=0;i<10;i++)
    {
    //loop body
    }

    top tested loop use while

    while (i<10)
    {
    //loop body
    }

    bottom tested loop use do/while

    do
    {
    //loop body
    }
    while(i<10);
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    thanx!

    I got what i needed. As far as the looping, i just needed it looped under any condition.....except closing the selection menu. Obviously.

    "um lets see, what program do i want? That one!" *program runs itself*, "Ok, now which other program do i want?"....and so on.

    BTW, i couldn't figure out looping in MFC . But i did make a useful pgm ! ( that i use to this day..long with other people.... ).

    thanx for the help
    The world is waiting. I must leave you now.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You should really invest in a "C" book!

    Anyway, take what prelude wrote. Now if you encapsulate all that in a function, you then can call it whenever you need to.

    Code:
    #include <stdio.h>
    
    void main_menu( );
    
     int main( )
    {
      
    
    main_menu();
    
      return 0;
    } 
    
    
    
    
    void main_menu()
    {
      int selection;
      printf("1. Do stuff\n2. Do more stuff\n3. Do even more stuff\n");
      printf("Choose an option: ");
      scanf("%d", &selection);
     
      switch(selection){
        case 1:
          /*do your thing for option 1*/
          break;
        case 2:
          /*do your thing for option 2*/
          break;
        case 3:
          /*do your thing for option 3*/
          break;
        default:
          printf("Invalid selection\n");
          break;
      }
      return 0;
    }

    Of course, if you had many more functions like this, you could easily move around the program in a logical, easily accessible way.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    Im getting hits....ok, here's the deal... lol

    Like i said, im doin a selection menu. You people are chipping in (thanks) so...

    Im creating a selection menu for ( seriously ) around, if not more, 100 program executions. It will just allow the user to access several programs ( mainly for my purposes ) that are scattered throughout the hard-drive.....all from one program. Those 100 or so seperate program executions will be divided into different catagories (4-5). It does help me out that im calling the same .exe program though, only each time im adding different command line options. No more command line bs, or typing in the preferred command line options.

    Im lazy, so batch files you sometimes create for temporary tasks that are run frequently and would be nice to have done with the click of a mouse ( or typing it at the promt ), i wanted to adapt that same conecpt into an .exe.
    Last edited by Shadow; 09-12-2001 at 10:38 PM.
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple text analyzsis program
    By prominababy in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 10:15 AM
  2. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  3. Best "Menu" method?
    By SSJMetroid in forum Game Programming
    Replies: 11
    Last Post: 12-08-2005, 12:05 AM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM