need help with validation

This is a discussion on need help with validation within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main(void) { int ch; printf ("Press [Enter] to continue"); while ((ch = getchar()) != '\n' && ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    5

    need help with validation

    Code:
    #include <stdio.h> 
    
    int main(void)
    {
      int ch;
      printf ("Press [Enter] to continue");
      while ((ch = getchar()) != '\n' && ch != EOF);
      return(0);
    }
    yeah, my problem here, is to make sure that a user enters an integer only...

    browsing thru the forum, that code stood out... using the getchar() function..

    could you tell me what i would do in the while loop to make sure a user enters an integer only?

    thx.

    could u explain what EOF means too?


    2nd question..


    lets say, my global variable is work_pay, it is a global variable because i want to access it in a few functions.


    how would i assign the value of work_pay from function1, so i can access it in functions 2 and 3?

    is it possible?

    edit.. i htink i solved it, heres my code...

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    #include <math.h>
    #include <string.h>
    
    int work_pay;
    int work_pay2;
    
    void func1();
    void func2();
    
    int main()
    {
        int count;
        
        func2(); // print data.
        
        func1(); //data entered
        func2(); //print new data
        
        
    
    
        system("PAUSE");
        return 0;
    }
    
    
    void func1(){
         
         int ans, ans1;
         
         printf("Enter a number for work_pay: ");
         scanf("%d",&ans);
         
         printf("enter a number for work_pay2: ");
         scanf("%d",&ans1);
         
         work_pay = ans;
         work_pay2 = ans1;
    }
    
    void func2(){
         
         printf("work_pay is: %d\n\n",work_pay);
         printf("work_pay2 is: %d\n\n",work_pay2);
    }
    Last edited by Enig.Ma; 10-23-2005 at 07:41 AM.

  2. #2
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    Code:
    int num, ch;
    while ( scanf("%d", &num) != 1 ) {
        while ((ch = getchar()) != '\n' && ch != EOF);
        printf("input again\n");
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. validation of TextBoxes!?
    By c9dw2rm8 in forum C# Programming
    Replies: 1
    Last Post: 04-01-2008, 06:19 PM
  2. set validation
    By jmarsh56 in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2005, 07:49 AM
  3. DATE <TIME.H> data validation
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 08-07-2005, 01:10 AM
  4. validation routine help plz
    By rajesh23 in forum C Programming
    Replies: 8
    Last Post: 09-23-2003, 07:21 AM
  5. [C#] Validation class for events?
    By gicio in forum C# Programming
    Replies: 4
    Last Post: 01-03-2003, 11:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21