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. validation

validation

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestioncareer
10 Posts 3 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

    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

    G D 2 Replies Last reply
    0
    • M mpapeo

      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

      G Offline
      G Offline
      Gary R Wheeler
      wrote on last edited by
      #2

      You don't initialize choice in your menu function. If you enter character data when scanf is expecting an integer (the "%d" format), scanf will stop scanning the input, and not set the choice variable. This means that your menu 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;

      1 Reply Last reply
      0
      • M mpapeo

        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

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

        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

        M 1 Reply Last reply
        0
        • D 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

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

          Well i tried your idea but it seems as if its difficult somehow as i am still get the program crashing -oam-

          M D 2 Replies Last reply
          0
          • M mpapeo

            Well i tried your idea but it seems as if its difficult somehow as i am still get the program crashing -oam-

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

            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

            1 Reply Last reply
            0
            • M mpapeo

              Well i tried your idea but it seems as if its difficult somehow as i am still get the program crashing -oam-

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

              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

              M 1 Reply Last reply
              0
              • D 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

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

                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

                M D 2 Replies Last reply
                0
                • M mpapeo

                  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

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

                  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-

                  D 1 Reply Last reply
                  0
                  • M mpapeo

                    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

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

                    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

                    1 Reply Last reply
                    0
                    • M mpapeo

                      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-

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

                      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

                      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