d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C++ Programming Question > Don't Even Know How To Start This ...
Prev12
Add Reply New Topic New Poll
Member
Posts: 1,241
Joined: Jun 25 2011
Gold: Locked
Feb 20 2014 12:43am
Okay, im just used to getline() i guess... lol
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 20 2014 12:51am
Quote (m0hawk @ Feb 20 2014 01:43am)
Okay, im just used to getline() i guess... lol


Using getline is great when you want to input a string. But what if you wanted integers? Now you need to do string parsing. Was it a single number? or numbers delimited by spaces? Now you need logic to tokenize. How are you going to do that? strtok for the tokenizing, and then stringstream for the parsing? See where it starts to get a little advanced for a beginner? I believe for this reason your original post was met with criticism because its debatable whether all this added parsing/tokenizing logic makes console input "cleaner."

Furthermore, getline will grab everything. You still need to do validation that the user infact entered the values he needed to. If the input is supposed to be "123 456" and he enters "cat dog" then when you try to parse those tokens into integers you are still in the same boat you were before you switched to strings from simple cin statements.

This post was edited by Minkomonster on Feb 20 2014 12:54am
Member
Posts: 1,241
Joined: Jun 25 2011
Gold: Locked
Feb 20 2014 01:14am
I think the idea is that parsing from the string is more practical than handling cin directly. But thx for the detailed answer
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 20 2014 02:55am
Quote (Minkomonster @ Feb 20 2014 02:19am)
You aren't wrong. std::cin does have a tendency to provoke input validation concerns. But there are ways around this without introducing strings. Especially for a beginner, let's not bog him down with things he hasn't learned yet.

The following will loop until the user enters an integer:
Code
int d;
while(!(std::cin>>d))
{
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}


Thank you was going to post this. You can also parse cin.good() and cin.eof() to tell if cin read correctly.

Quote (m0hawk @ Feb 20 2014 03:14am)
I think the idea is that parsing from the string is more practical than handling cin directly. But thx for the detailed answer


How is it more practical? Not only do you have to use getline() to grab your string, you also have to validate user string input before trying to parse it for your required integers. If you think strings are the answer for everything then you have a long way to go.

Member
Posts: 1,241
Joined: Jun 25 2011
Gold: Locked
Feb 20 2014 04:07am
@Abduct: Oh well, I never said I was a god or anything, my point was that inputting stuff simply with cin << intValue might cause problems if your corrector is picky. If this is enough to justify my incompetence then I'll just stfu from now on....

This post was edited by m0hawk on Feb 20 2014 04:08am
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll