NO

Author Topic: Very simple question about exiting...  (Read 5251 times)

DigitalJesus

  • Guest
Very simple question about exiting...
« on: October 17, 2014, 01:50:03 AM »
Is there a way to exit my 'program' directly? I'm creating a  menu-based program and cant seem to find the right command to exit immediately instead of hitting enter twice. Example: 1.) takes you to another menu 2.) Exit - i want option 2 to close to close my program.

czerny

  • Guest
Re: Very simple question about exiting...
« Reply #1 on: October 17, 2014, 11:03:05 AM »
Where is your code?

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Very simple question about exiting...
« Reply #2 on: October 18, 2014, 02:40:11 AM »
Is there a way to exit my 'program' directly? I'm creating a  menu-based program and cant seem to find the right command to exit immediately instead of hitting enter twice. Example: 1.) takes you to another menu 2.) Exit - i want option 2 to close to close my program.
Ok, please let me know if the picture that I see in my crystal ball is clear enough:
- your program is a console program
- you hit [Enter] once after entering the 2) in your menu
- and then you hit [Enter] a second time to close the black command line window
- you are doing all this from within the IDE

Correct?

If so, the the answer is simple, start the program (.EXE file) directly in a command prompt,not inside the IDE.
Et voila! With hitting [Enter] only once, you're back to the command line...

Is that working for you?

Ralf

DigitalJesus

  • Guest
Re: Very simple question about exiting...
« Reply #3 on: October 22, 2014, 04:10:59 PM »
Here is a small piece of what i've got. Yes it is a simple console program for school.

void menu_1()
{
   int menu_item = 0;
   system("cls");
   printf("Here are the resistor colors and associated values.\n");
   printf("\n\n");
   printf("Black - 0.\n");
   printf("Brown - 1.\n");
   printf("Red - 2.\n");
   printf("Orange - 3.\n");
   printf("Yellow - 4.\n");
   printf("Green - 5.\n");
   printf("Blue - 6.\n");
   printf("Violet - 7.\n");
   printf("Grey - 8.\n");
   printf("White - 9.\n");
   printf("\n\n");
   printf("1.) Main Menu.\n");
   printf("2.) Exit.\n");
   scanf("%i", &menu_item);
   if (menu_item == 1)
   {
      menu_switch();
   }
}

What i'd like to know is if you choose option 2 it just closes down the console immediately instead of printing out 'hit enter to continue...'

Offline DMac

  • Member
  • *
  • Posts: 272
Re: Very simple question about exiting...
« Reply #4 on: October 22, 2014, 05:44:16 PM »
I have a simple way that I do this sort of thing.  I can't give you the code because that would be cheating however I can give you the idea.

Begin Psuedocode:
Code: [Select]
BOOLEAN fContinue = TRUE

METHOD doSelection1
     PRINT "you selected 1"
END METHOD

METHOD doSelection2
    //Do something else
END METHOD

MAIN
     INTEGER choice

     LOOP WHILE fContinue
          PRINT "Enter a number:" + newline
                    "[1] Task 1" + newline
                    "[2] Task 2" + newline
                    "[3] Exit"

          GET choice //Waits for console input
          CLEARCONSOLE
          SWITCH choice
               CASE 1: doSelection1
                BREAK
               CASE 2: doSelection2
                BREAK
               CASE 3: fContinue = FALSE //drop out of the loop and exit
                BREAK
               CASE ELSE: PRINT "Invalid choice" + newline
          END SWITCH
     END LOOP
END MAIN
End Psuedocode:

This pattern can be nested to provide sub menus and is a clean way to write the kind of adventure games we played back in the early 80's on the very first PC's and AppleII's.

Keep in mind that if you are using this pattern in a sub menu and you toggle the Continue flag to exit the loop; you will want to reset the Continue flag back to true before exiting your method or you will exit out of your main menu loop too.
« Last Edit: October 22, 2014, 05:55:29 PM by DMac »
No one cares how much you know,
until they know how much you care.

laurro

  • Guest
Re: Very simple question about exiting...
« Reply #5 on: October 22, 2014, 08:13:20 PM »
Yes, it is possible ...

Code: [Select]

//in project-project options-compiler-options:
//check "enable microsoft extensions"

#include <windows.h>
#include <stdio.h>
#include <wchar.h>

BOOL CALLBACK enumWindows(HWND hwnd, LPARAM lParam)
{
wchar_t Name[MAX_PATH] = { 0 };
GetClassNameW(hwnd, Name, MAX_PATH);
if (!wcscmp(L"ConsoleWindowClass", Name))
{
Name[0] = L'\0';
GetWindowTextW(hwnd, Name, MAX_PATH);
if (!wcscmp(Name, (wchar_t *)lParam))
SendMessage(hwnd, WM_CLOSE, 0, 0);
}
return 1;
}

int main(void)
{
printf("1.) Main Menu.\n");
printf("2.) Exit.\n");
int choice;
scanf("%d", &choice);

if (choice == 2)
{
wchar_t title[MAX_PATH] = { 0 };
GetConsoleTitleW(title, MAX_PATH);
EnumWindows(enumWindows, (LPARAM)title);
}
else
{
system("cls");
printf("Here are the resistor colors and associated values.\n");
printf("\n\n");
printf("Black  - 0.\n");
printf("Brown  - 1.\n");
printf("Red    - 2.\n");
printf("Orange - 3.\n");
printf("Yellow - 4.\n");
printf("Green  - 5.\n");
printf("Blue   - 6.\n");
printf("Violet - 7.\n");
printf("Grey   - 8.\n");
printf("White  - 9.\n");
printf("\nadd code here to calculate the rezistor\n");
}
return 0;
}


But don't show this to your teacher... will be very angry  >:(

Laur

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: Very simple question about exiting...
« Reply #6 on: October 22, 2014, 11:46:06 PM »
Slightly simpler:

#include <windows.h>
#include <stdio.h>
#pragma comment(linker, "/SubSystem:Console")

int main(void)
{
   printf("1.) Main Menu.\n");
   printf("2.) Exit.\n");

   if ((getchar() & 2) == 2)
      SendMessage(GetConsoleWindow(), WM_CLOSE, 0, 0);
   else
   {
      system("cls");
      printf("Here are the resistor colors and associated values.\n");
      printf("\n\n");
      printf("Black  - 0.\n");
      printf("Brown  - 1.\n");
      printf("Red    - 2.\n");
      printf("Orange - 3.\n");
      printf("Yellow - 4.\n");
      printf("Green  - 5.\n");
      printf("Blue   - 6.\n");
      printf("Violet - 7.\n");
      printf("Grey   - 8.\n");
      printf("White  - 9.\n");
      printf("\nadd code here to calculate the rezistor\n");
   }
   return 0;
}


;-)

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Very simple question about exiting...
« Reply #7 on: October 23, 2014, 01:29:05 AM »
What i'd like to know is if you choose option 2 it just closes down the console immediately instead of printing out 'hit enter to continue...'
Did you bother to read my reply? >:(

The "Hit enter to continue..." is from the Pelle's C IDE, not from your program...

Ralf

laurro

  • Guest
Re: Very simple question about exiting...
« Reply #8 on: October 23, 2014, 04:17:14 PM »
@Jochen

Hi jj!
Much, much better your solution , and my only excuse
(a pathetic but a true one) is I did not know this api.
However...your:
if ((getchar() & 2) == 2)
is wrong, the correct one is
if (getchar() == 50) or if (getchar() == '2')
Is wrong because :
Code: [Select]
#include <stdio.h>
int main(void)
{
for (int i = 0; i < 256; i++)
{
int a = i & 2;
if (a == 2)
printf("keyboard decimal = %i (0x%.2x)\tchar = \'%c\'\t(%i & 2) = %i\tpas the test\n", i, i, i < 0x07 || i > 0x0d ? i : ' ', i, a);
else
printf("keyboard decimal = %i (0x%.2x)\tchar = \'%c\'\t...\n", i, i, i < 0x07 || i > 0x0d ? i : ' ');
}
}

Laur
« Last Edit: October 23, 2014, 04:21:57 PM by laurro »

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: Very simple question about exiting...
« Reply #9 on: October 24, 2014, 09:53:10 AM »
I did not know this api

Hi Laur,
Me neither, but I have clever friends ;-)

Quote
However...your:
if ((getchar() & 2) == 2)
is wrong, the correct one is
if (getchar() == '2')

Point taken, that was rather sloppy. It works, of course, if the user types only "1" or "2", but my code is bad anyway.

tienkhoanguyen

  • Guest
Re: Very simple question about exiting...
« Reply #10 on: October 25, 2014, 07:30:29 PM »
If you are in a loop, you can use "goto" to exit out of it.  Then perform a "return (0);" in your main function.