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.
Where is your code?
Quote from: DigitalJesus 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.
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
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...'
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:
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.
Yes, it is possible ...
//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
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;
}
;-)
Quote from: DigitalJesus on October 22, 2014, 04:10:59 PM
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
@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 :
#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
Quote from: laurro on October 23, 2014, 04:17:14 PM
I did not know this api
Hi Laur,
Me neither, but I have clever friends (http://masm32.com/board/index.php?topic=3605.msg37967#msg37967) ;-)
QuoteHowever...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.
If you are in a loop, you can use "goto" to exit out of it. Then perform a "return (0);" in your main function.