LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Validating concatenated cin inputs (C++) (https://www.linuxquestions.org/questions/programming-9/validating-concatenated-cin-inputs-c-554395/)

Swakoo 05-16-2007 12:22 PM

Validating concatenated cin inputs (C++)
 
Hi guys,

I am trying to do a simple, CLi ftp client/server c++ application.

On the client end, once started, the command prompt will be:

ftp>>

just like a regular command line software.

At one point, I am suppose to let use key in the following to transfer file:

Quote:

put fileFromClient fileToServer
basically the above command will just copy a file from client to server.

I achieve that by issuing the following commands:

Code:

cin >> a >> b;
Where a and b are appropriate variables.

My question is, how can I validate the input, such that, if a user enters only 'put' or 'put fileFromClient' (basically not full commands which consist of the 3 arguements), I can capture it and prompt the user?

Right now, by concatenating cin, the software will keep waiting, such that if I type 'put', hit the return key, the application will appear to be waiting (for the next 2 input).

How can I capture the errornous user input from the moment the user hit the return key? and not let the program wait for the next 3 inputs from user, differentiate by the return key?

I tried using cin.good() but i think I am not getting it right...

anyone? thanks!

ps: this is for a school assignment, but I'm just asking about how can I validate concatenated inputs... not asking help for my work hehe - in case you're wondering ;)

graemef 05-16-2007 06:15 PM

You probably want to read the whole line, using the template method getine(), and then analyse the line for correctness.

Swakoo 05-16-2007 09:26 PM

you mean cin.getline()?

I tried.. but that way I can't ensure i get 3 inputs on one line, without having the user enter it 3 times separately

graemef 05-17-2007 05:56 AM

well that's why I said you need to check the line that has been read; that would basically mean split it up into tokens, broken down by spaces and see if there are three.

Swakoo 05-17-2007 09:03 PM

so its not possible to prevent the user from pressing the 'return' key after keying in just 1 argument?

i was looking for possible ways to capture the user's action of entering, say:
Code:

put fileFromClient *hit return*
This way, the program will still wait for the 3rd argument.

osor 05-17-2007 09:42 PM

Quote:

Originally Posted by Swakoo
so its not possible to prevent the user from pressing the 'return' key after keying in just 1 argument?

Not unless you intend to use ncurses or similar libraries.


All times are GMT -5. The time now is 09:24 PM.