Cut off Extra data

This is a discussion on Cut off Extra data within the C++ Programming forums, part of the General Programming Boards category; If you had something like this: Code: int age; string firstname; cout << "Please enter in your age and first ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    10

    Cut off Extra data

    If you had something like this:

    Code:
    int age;
    string firstname;
    
    cout << "Please enter in your age and first name:";
    cin >> age >> firstname;
    If the user enters in

    90 Nick

    the code works fine.

    But what if the user 'accidently' enters in their last name.
    like this:
    90 NickSmith

    How would you throw away the "Smith" - assuming that everybody's name is only 4 letters long?

    would you use an ignore()?

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    You could counter for every possible thing the user could do. From that to checking if the user entered numbers, profanity, object names, etc... But the size of your code would be enormous, not to mention the dictionary that you would probably have to load into memory.

    Assuming the name is only 4 letters long, how would you cope with names like DanY, LeaF, and others...

    The best option is to not be too pedantic. But if you really want to do it, then you can either cut everything to just the first 4 letters with std::string.erase() and Michael would become Mich,

    or

    search for the first capital letter after the first index with a combination of find_first_of() against a string of capital letters and std::string.erase(). In this case MiChael would become Mi.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> NickSmith

    Would they not be likely to press space between names?

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    If the user is intent on typing their last name as well as their first, there's nothing you can do about it. You might block from the second capital onwards, but what's to prevent a name like "Nicksmith"? And it might block legitimate names like "DWK" or "McDonald".
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Do what I would do - assume an intelligent user I love it when assignments have those precious words there!

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Quote Originally Posted by twomers
    Do what I would do - assume an intelligent user
    Joe: "'Enter your age and first name.' Okay. Hmm . . ."
    Joe types: "Joe twenty"
    Joe: "'Error'? Okay . . ."
    Joe types: "20 Joe"
    Joe: ""Error'?!?! Fine."
    Joe types: "20 20"
    Joe: "'Welcome, 20'? That's not my name . . ."
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Joe clearly doesn't fit that criteria!

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Quote Originally Posted by twomers
    Joe clearly doesn't fit that criteria!
    Don't you mean 20?
    Sent from my iPad®

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> assuming that everybody's name is only 4 letters long?
    Use get into a vector<char> or character array of size 5 and specify 4 characters as what you want. You can't do it with operator>> and strings since operator>> assumes a string ends at the first whitespace.

    You could also get one character at a time and append them to the string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Dynamic data members?
    By confusalot in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 10:15 AM
  4. Output file contains extra data
    By nizbit in forum C Programming
    Replies: 3
    Last Post: 02-21-2005, 07:00 PM
  5. Getting extra data from control
    By Mithoric in forum Windows Programming
    Replies: 4
    Last Post: 11-30-2003, 02:27 AM

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