Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. pliz help me with input validation

pliz help me with input validation

Scheduled Pinned Locked Moved C / C++ / MFC
helpcareer
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mpapeo
    wrote on last edited by
    #1

    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-

    C 1 Reply Last reply
    0
    • M mpapeo

      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-

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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

      A 1 Reply Last reply
      0
      • C Christian Graus

        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

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #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:

        C L 2 Replies Last reply
        0
        • A Anonymous

          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:

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          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

          M 1 Reply Last reply
          0
          • A Anonymous

            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:

            L Offline
            L Offline
            LighthouseJ
            wrote on last edited by
            #5

            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.

            M 1 Reply Last reply
            0
            • C Christian Graus

              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

              M Offline
              M Offline
              mpapeo
              wrote on last edited by
              #6

              You mean passing it as an input? -oam-

              C 1 Reply Last reply
              0
              • M mpapeo

                You mean passing it as an input? -oam-

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                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

                M 1 Reply Last reply
                0
                • L LighthouseJ

                  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.

                  M Offline
                  M Offline
                  mpapeo
                  wrote on last edited by
                  #8

                  let me try chris method first -oam- seems as that im having slight problems why does it takes default now after pass the hProcess

                  M 1 Reply Last reply
                  0
                  • M mpapeo

                    let me try chris method first -oam- seems as that im having slight problems why does it takes default now after pass the hProcess

                    M Offline
                    M Offline
                    mpapeo
                    wrote on last edited by
                    #9

                    I still have the program crashing -oam-

                    1 Reply Last reply
                    0
                    • C Christian Graus

                      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

                      M Offline
                      M Offline
                      mpapeo
                      wrote on last edited by
                      #10

                      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-

                      D 1 Reply Last reply
                      0
                      • M mpapeo

                        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-

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #11

                        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

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups