validation
-
Pliz help me to validate my input in my program below.I want to accept the integer input only but not characters, how do i do it because i used dafault in my case statement. If i enter characters the program crushes. #include #define _WIN32_WINNT 0x0400 #define WINVER 0x0400 #include #include #include #include #include #include typedef struct _iobuf FILE; #include #include #include int menu(void) { int choice; 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("%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) { 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.hThread);// identifies thread to suspend break; case 3: ResumeThread(pi.hProcess); break; case 4: printf("\nYou are about to terminate a running process, do you want to continue ( y or n) "); scanf ("%s",ans); result = strcmp(ans,"y"); if (result==0) { TerminateProcess(pi.hProcess, 0);//identifies the p
-
Pliz help me to validate my input in my program below.I want to accept the integer input only but not characters, how do i do it because i used dafault in my case statement. If i enter characters the program crushes. #include #define _WIN32_WINNT 0x0400 #define WINVER 0x0400 #include #include #include #include #include #include typedef struct _iobuf FILE; #include #include #include int menu(void) { int choice; 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("%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) { 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.hThread);// identifies thread to suspend break; case 3: ResumeThread(pi.hProcess); break; case 4: printf("\nYou are about to terminate a running process, do you want to continue ( y or n) "); scanf ("%s",ans); result = strcmp(ans,"y"); if (result==0) { TerminateProcess(pi.hProcess, 0);//identifies the p
You don't initialize
choice
in yourmenu
function. If you enter character data whenscanf
is expecting an integer (the "%d
" format),scanf
will stop scanning the input, and not set thechoice
variable. This means that yourmenu
function will return a random value if you enter character data. Note that this random value could include one of your valid menu values of 1 through 4.
Software Zen:
delete this;
-
Pliz help me to validate my input in my program below.I want to accept the integer input only but not characters, how do i do it because i used dafault in my case statement. If i enter characters the program crushes. #include #define _WIN32_WINNT 0x0400 #define WINVER 0x0400 #include #include #include #include #include #include typedef struct _iobuf FILE; #include #include #include int menu(void) { int choice; 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("%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) { 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.hThread);// identifies thread to suspend break; case 3: ResumeThread(pi.hProcess); break; case 4: printf("\nYou are about to terminate a running process, do you want to continue ( y or n) "); scanf ("%s",ans); result = strcmp(ans,"y"); if (result==0) { TerminateProcess(pi.hProcess, 0);//identifies the p
mpapeo wrote: printf("\nEnter choice (1-4): "); scanf("%d", &choice); return choice; How about some error checking:
int done = 0;
while (! done)
{
printf("\nEnter choice (1-4): ");
done = scanf("%d", &choice);
}
return choice;This is hardly foolproof, and may not even work, but it might give you a foundation to build upon. An alternative is to use
getch()
to get each character as it is typed. If it is a numeric character, proceed. Otherwise, indicate problem and ask for another character.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
mpapeo wrote: printf("\nEnter choice (1-4): "); scanf("%d", &choice); return choice; How about some error checking:
int done = 0;
while (! done)
{
printf("\nEnter choice (1-4): ");
done = scanf("%d", &choice);
}
return choice;This is hardly foolproof, and may not even work, but it might give you a foundation to build upon. An alternative is to use
getch()
to get each character as it is typed. If it is a numeric character, proceed. Otherwise, indicate problem and ask for another character.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
Well i tried your idea but it seems as if its difficult somehow as i am still get the program crashing -oam-
I tried using something different but now i am getting these errors, how can i solve them
#include #define _WIN32_WINNT 0x0400 #define WINVER 0x0400 #include #include #include #include #include #include #include typedef struct _iobuf FILE; #include #include #include int menu(void) { int choice; char iobuf[80]; int i,len,valid; boolean isDigit(); 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; // assumed valid input for(i = 0; i < len; i++) { if( !isdigit(iobuf)) { printf("Plese enter numeric digits only\n"); valid = 0; break; } } } 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) { 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.dwProc
-
Well i tried your idea but it seems as if its difficult somehow as i am still get the program crashing -oam-
What's wrong with:
int menu( void )
{
int choice;do { printf("\\nEnter choice (1-4): "); choice = \_getche(); } while (choice < '1' || choice > '5'); return choice;
}
void main( void )
{
int choice;while ((choice = menu()) != '5') { switch (choice) { case '1': ... } }
}
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
What's wrong with:
int menu( void )
{
int choice;do { printf("\\nEnter choice (1-4): "); choice = \_getche(); } while (choice < '1' || choice > '5'); return choice;
}
void main( void )
{
int choice;while ((choice = menu()) != '5') { switch (choice) { case '1': ... } }
}
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
Well its like it does not want to accept any input whether text nor digits -oam- there might be something missing because it does read input and i can't figure it out
Well i used the other method which i found it simpler even though longer that the one you suggested.
printf("\nEnter choice (1-4): "); valid = 0; while( valid == 0) { fgets(iobuf,sizeof(iobuf),stdin); len = strlen(iobuf)-1; iobuf[len] = 0; // validate data valid = 1; // assume valid input for(i = 0; i < len; i++) //loop controlling invalid input { if( !isdigit(iobuf[i])) { printf("\nPlese enter numeric digits only (1-8):"); valid = 0; } } break; } choice = atoi(iobuf); scanf("%d", &choice); //scanning the iput from the keyboard return choice; }
Thanks anyway. -oam- -
Well its like it does not want to accept any input whether text nor digits -oam- there might be something missing because it does read input and i can't figure it out
The code snippet I provided does indeed work. Are you sure you typed it in correctly?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
Well i used the other method which i found it simpler even though longer that the one you suggested.
printf("\nEnter choice (1-4): "); valid = 0; while( valid == 0) { fgets(iobuf,sizeof(iobuf),stdin); len = strlen(iobuf)-1; iobuf[len] = 0; // validate data valid = 1; // assume valid input for(i = 0; i < len; i++) //loop controlling invalid input { if( !isdigit(iobuf[i])) { printf("\nPlese enter numeric digits only (1-8):"); valid = 0; } } break; } choice = atoi(iobuf); scanf("%d", &choice); //scanning the iput from the keyboard return choice; }
Thanks anyway. -oam-This much code to get a simple number is a maintenance nightmare waiting to happen.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow