pliz help me with input validation
-
i have been try it out but failed. The second and third options should only be executed when there is a process running. But now i fail to write that error message so the user does not suspend a process while there is no process running. part of my code is below
int menu(void) { char iobuf[80]; int choice; int i,len,valid; printf ("\n"); printf ("*****************************************\n"); printf ("*\t\t\t\t\t*\n*\tPROCESS CREATION \t\t*\n"); printf ("========================================="); printf ("\n=\t 1: Create a process \t\t="); printf ("\n=\t 2: Suspend the process\t\t="); printf ("\n=\t 3: Resume process \t="); printf ("\n=\t 4: Shutdown the process \t="); printf ("\n=========================================\n"); printf ("\n"); printf("\nEnter choice (1-4): "); valid = 0;
-oam- -
i have been try it out but failed. The second and third options should only be executed when there is a process running. But now i fail to write that error message so the user does not suspend a process while there is no process running. part of my code is below
int menu(void) { char iobuf[80]; int choice; int i,len,valid; printf ("\n"); printf ("*****************************************\n"); printf ("*\t\t\t\t\t*\n*\tPROCESS CREATION \t\t*\n"); printf ("========================================="); printf ("\n=\t 1: Create a process \t\t="); printf ("\n=\t 2: Suspend the process\t\t="); printf ("\n=\t 3: Resume process \t="); printf ("\n=\t 4: Shutdown the process \t="); printf ("\n=========================================\n"); printf ("\n"); printf("\nEnter choice (1-4): "); valid = 0;
-oam-mpapeo wrote: char iobuf[80]; int choice; int i,len,valid; Always give your variables values when you create them. Unless this is a C program ( this is all C code, so I guess it could be ), you shouldn't declare all your variables at the top, but just before you use them. If you're always creating the process, you'd need to keep a handle to it to suspend and resume anyhow, right ? So declare that, make it NULL, and check for NULL before showing the other options. Of course, all of this needs to be in an endless loop then, so it shows more than once. Unless you want to run it every time. Then you need to try and find the handle first. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
-
mpapeo wrote: char iobuf[80]; int choice; int i,len,valid; Always give your variables values when you create them. Unless this is a C program ( this is all C code, so I guess it could be ), you shouldn't declare all your variables at the top, but just before you use them. If you're always creating the process, you'd need to keep a handle to it to suspend and resume anyhow, right ? So declare that, make it NULL, and check for NULL before showing the other options. Of course, all of this needs to be in an endless loop then, so it shows more than once. Unless you want to run it every time. Then you need to try and find the handle first. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
This is what i have,
#include #include #include #include #include #include #include typedef struct _iobuf FILE; #include #include #include int menu(void) { char iobuf[80]; int choice; int i,len,valid; printf ("\n"); printf ("*****************************************\n"); printf ("*\t\t\t\t\t*\n*\tPROCESS CREATION \t\t*\n"); printf ("========================================="); printf ("\n=\t 1: Create a process \t\t="); printf ("\n=\t 2: Suspend the process\t\t="); printf ("\n=\t 3: Resume process \t="); printf ("\n=\t 4: Shutdown the process \t="); printf ("\n=========================================\n"); printf ("\n"); printf("\nEnter choice (1-4): "); valid = 0; while( valid == 0) { fgets(iobuf,sizeof(iobuf),stdin); // remove '\n' len = strlen(iobuf)-1; iobuf[len] = 0; // validate data valid = 1; // assume valid input for(i = 0; i < len; i++) { if( !isdigit(iobuf[i])) { printf("\nPlese enter numeric digits only (1-4)\n"); valid = 0; } } } choice = atoi(iobuf); //scanf("%d", &choice); return choice; } int main(int argc, char **argv) { PROCESS_INFORMATION pi; /* filled in by CreateProcess */ STARTUPINFO si; /* startup info for the new process*/ HANDLE hProcess; DWORD lpAddr = 0; TCHAR lpApplicationName[_MAX_PATH]=""; int result; char ans [4]=""; int choice; while((choice = menu())!=5)//int choice; { switch (choice) { case 1: GetStartupInfo(&si); lpAddr = 0; ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( &pi, sizeof(pi) ); printf("Enter process you want to execute: ",lpApplicationName); scanf ("%s",lpApplicationName); printf("Process %d reporting for creation\n",GetCurrentProcessId());//print out our process ID CreateProcess(NULL, /* lpApplicationName */ lpApplicationName, /* lpCommandLine assumes to use curent process directory*/ NULL, /* lpsaProcess */ NULL, /* lpsaThread */ FALSE, /* bInheritHandles */ CREATE_NEW_CONSOLE, /* dwCreationFlags */ NULL, /* lpEnvironment */ NULL, /* lpCurDir */ &si, /* lpStartupInfo */ &pi /* lpProcInfo */ ); hProcess = pi.hProcess; printf("New Process ID: %d ",pi.dwProcessId); printf("has started \n"); break; case 2: SuspendThread(pi.hProcess);// identifies thread to suspend break; case 3:
-
This is what i have,
#include #include #include #include #include #include #include typedef struct _iobuf FILE; #include #include #include int menu(void) { char iobuf[80]; int choice; int i,len,valid; printf ("\n"); printf ("*****************************************\n"); printf ("*\t\t\t\t\t*\n*\tPROCESS CREATION \t\t*\n"); printf ("========================================="); printf ("\n=\t 1: Create a process \t\t="); printf ("\n=\t 2: Suspend the process\t\t="); printf ("\n=\t 3: Resume process \t="); printf ("\n=\t 4: Shutdown the process \t="); printf ("\n=========================================\n"); printf ("\n"); printf("\nEnter choice (1-4): "); valid = 0; while( valid == 0) { fgets(iobuf,sizeof(iobuf),stdin); // remove '\n' len = strlen(iobuf)-1; iobuf[len] = 0; // validate data valid = 1; // assume valid input for(i = 0; i < len; i++) { if( !isdigit(iobuf[i])) { printf("\nPlese enter numeric digits only (1-4)\n"); valid = 0; } } } choice = atoi(iobuf); //scanf("%d", &choice); return choice; } int main(int argc, char **argv) { PROCESS_INFORMATION pi; /* filled in by CreateProcess */ STARTUPINFO si; /* startup info for the new process*/ HANDLE hProcess; DWORD lpAddr = 0; TCHAR lpApplicationName[_MAX_PATH]=""; int result; char ans [4]=""; int choice; while((choice = menu())!=5)//int choice; { switch (choice) { case 1: GetStartupInfo(&si); lpAddr = 0; ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( &pi, sizeof(pi) ); printf("Enter process you want to execute: ",lpApplicationName); scanf ("%s",lpApplicationName); printf("Process %d reporting for creation\n",GetCurrentProcessId());//print out our process ID CreateProcess(NULL, /* lpApplicationName */ lpApplicationName, /* lpCommandLine assumes to use curent process directory*/ NULL, /* lpsaProcess */ NULL, /* lpsaThread */ FALSE, /* bInheritHandles */ CREATE_NEW_CONSOLE, /* dwCreationFlags */ NULL, /* lpEnvironment */ NULL, /* lpCurDir */ &si, /* lpStartupInfo */ &pi /* lpProcInfo */ ); hProcess = pi.hProcess; printf("New Process ID: %d ",pi.dwProcessId); printf("has started \n"); break; case 2: SuspendThread(pi.hProcess);// identifies thread to suspend break; case 3:
OK, so it IS a C program ? ( Not C++ ) If you want to put the menu into a seperate function ( not a bad idea ), then you need to pass in the hProcess variable, so you know if there's a process going or not. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
-
This is what i have,
#include #include #include #include #include #include #include typedef struct _iobuf FILE; #include #include #include int menu(void) { char iobuf[80]; int choice; int i,len,valid; printf ("\n"); printf ("*****************************************\n"); printf ("*\t\t\t\t\t*\n*\tPROCESS CREATION \t\t*\n"); printf ("========================================="); printf ("\n=\t 1: Create a process \t\t="); printf ("\n=\t 2: Suspend the process\t\t="); printf ("\n=\t 3: Resume process \t="); printf ("\n=\t 4: Shutdown the process \t="); printf ("\n=========================================\n"); printf ("\n"); printf("\nEnter choice (1-4): "); valid = 0; while( valid == 0) { fgets(iobuf,sizeof(iobuf),stdin); // remove '\n' len = strlen(iobuf)-1; iobuf[len] = 0; // validate data valid = 1; // assume valid input for(i = 0; i < len; i++) { if( !isdigit(iobuf[i])) { printf("\nPlese enter numeric digits only (1-4)\n"); valid = 0; } } } choice = atoi(iobuf); //scanf("%d", &choice); return choice; } int main(int argc, char **argv) { PROCESS_INFORMATION pi; /* filled in by CreateProcess */ STARTUPINFO si; /* startup info for the new process*/ HANDLE hProcess; DWORD lpAddr = 0; TCHAR lpApplicationName[_MAX_PATH]=""; int result; char ans [4]=""; int choice; while((choice = menu())!=5)//int choice; { switch (choice) { case 1: GetStartupInfo(&si); lpAddr = 0; ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( &pi, sizeof(pi) ); printf("Enter process you want to execute: ",lpApplicationName); scanf ("%s",lpApplicationName); printf("Process %d reporting for creation\n",GetCurrentProcessId());//print out our process ID CreateProcess(NULL, /* lpApplicationName */ lpApplicationName, /* lpCommandLine assumes to use curent process directory*/ NULL, /* lpsaProcess */ NULL, /* lpsaThread */ FALSE, /* bInheritHandles */ CREATE_NEW_CONSOLE, /* dwCreationFlags */ NULL, /* lpEnvironment */ NULL, /* lpCurDir */ &si, /* lpStartupInfo */ &pi /* lpProcInfo */ ); hProcess = pi.hProcess; printf("New Process ID: %d ",pi.dwProcessId); printf("has started \n"); break; case 2: SuspendThread(pi.hProcess);// identifies thread to suspend break; case 3:
Why comment out the scanf? try this ...
int choice; while (true) { printf ("\n"); printf ("*****************************************\n"); printf ("*\t\t\t\t\t*\n*\tPROCESS CREATION \t\t*\n"); printf ("========================================="); printf ("\n=\t 1: Create a process \t\t="); printf ("\n=\t 2: Suspend the process\t\t="); printf ("\n=\t 3: Resume process \t="); printf ("\n=\t 4: Shutdown the process \t="); printf ("\n=========================================\n"); printf ("\n"); printf ("\nEnter choice (1-4): "); scanf("%u", &choice); switch (choice) { case 1: case 2: case 3: case 4: return choice; break; default: printf("\nPlese enter numeric digits only (1-4)\n"); break; } }
Now the while(true) { } loop is used, it should be used sparingly. That loop will keep iterating through the menu indefinitely. The only way to get out is to choose one of the valid options. The switch will catch options 1 through 4 and return them, anything else will show the error. The switch-case statement is very useful in this case because you can run code for each option and handle all other cases. If you choose say option 2, it'll enter at case 2 but then keep going through case 3 and then it will reach case 4, return the number 2 (stored in choice) and then break; (it's really unreachable code because the function will end at the return statement. -
OK, so it IS a C program ? ( Not C++ ) If you want to put the menu into a seperate function ( not a bad idea ), then you need to pass in the hProcess variable, so you know if there's a process going or not. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
-
Yes, so you can check if it's NULL, and alter your options accordingly. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
-
Why comment out the scanf? try this ...
int choice; while (true) { printf ("\n"); printf ("*****************************************\n"); printf ("*\t\t\t\t\t*\n*\tPROCESS CREATION \t\t*\n"); printf ("========================================="); printf ("\n=\t 1: Create a process \t\t="); printf ("\n=\t 2: Suspend the process\t\t="); printf ("\n=\t 3: Resume process \t="); printf ("\n=\t 4: Shutdown the process \t="); printf ("\n=========================================\n"); printf ("\n"); printf ("\nEnter choice (1-4): "); scanf("%u", &choice); switch (choice) { case 1: case 2: case 3: case 4: return choice; break; default: printf("\nPlese enter numeric digits only (1-4)\n"); break; } }
Now the while(true) { } loop is used, it should be used sparingly. That loop will keep iterating through the menu indefinitely. The only way to get out is to choose one of the valid options. The switch will catch options 1 through 4 and return them, anything else will show the error. The switch-case statement is very useful in this case because you can run code for each option and handle all other cases. If you choose say option 2, it'll enter at case 2 but then keep going through case 3 and then it will reach case 4, return the number 2 (stored in choice) and then break; (it's really unreachable code because the function will end at the return statement. -
let me try chris method first -oam- seems as that im having slight problems why does it takes default now after pass the hProcess
-
Yes, so you can check if it's NULL, and alter your options accordingly. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
I stil have the program crushing, i might be missing something
printf("\nEnter choice (1-4): "); valid = 0; while( valid == 0) { fgets(iobuf,sizeof(iobuf),stdin); // remove '\n' len = strlen(iobuf)-1; iobuf[len] = 0; // validate data valid = 1; // assume valid input for(i = 0; i < len; i++) { if( !isdigit(iobuf[i])) { printf("\nPlese enter numeric digits only (1-4)\n"); valid = 0; } } } hProcess = atoi(iobuf); //scanf("%d", &choice); return hProcess;//choice; }
-oam- -
I stil have the program crushing, i might be missing something
printf("\nEnter choice (1-4): "); valid = 0; while( valid == 0) { fgets(iobuf,sizeof(iobuf),stdin); // remove '\n' len = strlen(iobuf)-1; iobuf[len] = 0; // validate data valid = 1; // assume valid input for(i = 0; i < len; i++) { if( !isdigit(iobuf[i])) { printf("\nPlese enter numeric digits only (1-4)\n"); valid = 0; } } } hProcess = atoi(iobuf); //scanf("%d", &choice); return hProcess;//choice; }
-oam-mpapeo wrote: I stil have the program crushing, i might be missing something While it is a bad design, this code is syntactically correct. The "crush" your program is experiencing is in some other code. Why not single-step through the code until you get to the statement(s) in error?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow