Need help in client input restriction..

This is a discussion on Need help in client input restriction.. within the C++ Programming forums, part of the General Programming Boards category; I received an assignment which ask for simple calculations between server and client such as addition, subtraction, multiplication, division, etc. ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    3

    Question Need help in client input restriction..

    I received an assignment which ask for simple calculations between server and client such as addition, subtraction, multiplication, division, etc.

    Currently, I had finished the calculation part, where client could input in 2 numbers and the operation they want to the server so the server could do the calculation and send back the result to client. The problem I'm facing now is, how could I restrict the client side so that they are only allowed to input numbers and not characters?

    This is a small part of my codes :
    Code:
    // Reply first number
    	char firstnum[256];
    	cin >> firstnum;
    
    	send(	theSocket,				// Connected socket
    		firstnum,				// Data buffer
    		strlen(firstnum),			// Length of data
    		0);
    Anyhelp would be appreciated, thanks.

  2. #2
    Rog
    Rog is offline
    Registered User
    Join Date
    Jan 2003
    Posts
    78
    use isdigit() to check firstnum[0] thru firstnum[strlen(firstnum)-1] in a for loop. Tip: watch out for '\n' characters at end of input.

    another way is to read the input using fgets() to read stdin, then parse firstnum using sscanf()

    the second method works very well if you do it right.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    3
    hmm, didn't quite get what you mean at there, since I'm pretty new to C++... >_>

    Anyway, just an idea :

    Would it work, if I put the 24 alphabets into an array and scan the input with it so it could detect characters and alert the client?

  4. #4
    Rog
    Rog is offline
    Registered User
    Join Date
    Jan 2003
    Posts
    78
    Quote Originally Posted by cyrus2k
    hmm, didn't quite get what you mean at there, since I'm pretty new to C++... >_>

    Anyway, just an idea :

    Would it work, if I put the 24 alphabets into an array and scan the input with it so it could detect characters and alert the client?
    Excuse me if I was vague. Too many late nights has that effect on me.

    Forget the stuff I told you... those are old C language ways of validating input. You can do it very easily in C++ like this...

    Code:
    int n=0;
    
    cout << "Enter first number: ";
    cin >> n;
    if (cin.fail())
        cout << "Input error, please try again" << endl;
    else
        cout << "You entered the number " << n << endl;
    If the user enters a non-decimal character before any decimal characters, the call to "if (cin.fail())" will evaluate true.

    So, for example, the input "123ttt" will put the number 123 into n, while the input "ttt321" will raise an error.

    pretty simple, huh?

    -Rog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input class project (again)
    By Elysia in forum C++ Programming
    Replies: 41
    Last Post: 02-13-2009, 10:52 AM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  3. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 09:31 PM
  4. Mouse input outside Client Area
    By mrafcho001 in forum Windows Programming
    Replies: 1
    Last Post: 02-12-2006, 09:47 AM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM

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