validate userinput

This is a discussion on validate userinput within the C++ Programming forums, part of the General Programming Boards category; Hello ppl im trying to get user input using getline. i give this statement while (true) { if (!getline(cin, string, ...

  1. #1
    Registered User justdoit22's Avatar
    Join Date
    Dec 2003
    Posts
    18

    Unhappy validate userinput

    Hello ppl
    im trying to get user input using getline. i give this statement

    while (true)
    {
    if (!getline(cin, string, '/'))
    {
    cout<<"invalid input"<<endl;
    continue;

    }

    this program keeps on getting input until i give '/' in cin.

    Question:
    is there anyway if i dont give '/' and give anyother input and it should give me an error that it was a valid input(should have / at end)...

    currently it doest give the error and keeps on getting input even if i hit enter unless i give it '/' delimitor.
    Code:
    while (true)

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >currently it doest give the error and keeps on getting input even if i hit enter unless i give it '/' delimitor.
    It sounds as if you really want to read a line of input and then check that the line is terminated with a slash character:
    Code:
    string s;
    
    while ( getline ( cin, s ) && s[s.length() - 1] != '/' ) {
      cerr<<"Error: Input must end with '/'\n";
      // Other stuff
    }
    My best code is written with the delete key.

  3. #3
    Registered User justdoit22's Avatar
    Join Date
    Dec 2003
    Posts
    18
    thanx man it works....
    wondering i couldnt think of it earlier.. perhaps new @ programming
    thanx again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Validate double number
    By guaro555 in forum C Programming
    Replies: 38
    Last Post: 01-15-2008, 06:08 AM
  2. validate data in a dialog box
    By COBOL2C++ in forum Windows Programming
    Replies: 4
    Last Post: 09-22-2003, 01:55 PM
  3. Validate Float Logic?
    By Bajanine in forum Windows Programming
    Replies: 2
    Last Post: 11-14-2002, 09:00 PM
  4. last part of program i hope to validate
    By sturm100 in forum C Programming
    Replies: 8
    Last Post: 07-10-2002, 06:51 AM
  5. validate alphabets and numbers
    By sturm100 in forum C Programming
    Replies: 4
    Last Post: 07-09-2002, 08:04 AM

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