« first day (828 days earlier)      last day (2008 days later) » 

9:20 AM
I have a program in which I use getline(std::cin, someStr) functions in a while loop, but the prompts to enter info (in the terminal) seem to be very out of place.
    std::string move1;
    std::string move2;
    std::cout << "Move From\n>";
    std::getline(std::cin, move1);
//     std::cout << (int)move1[0] - 65 << '\n';
    _x1 = (int)move1[0] - 65;
    _y1 = (int)move1[1] - 48;
    std::cout << "\nMove To\n>"; std::getline(std::cin, move2);
    _x2 = (int)move2[0] - 65;
    _y2 = (int)move2[1] - 48;
I'll run that chunk of code in a loop but it will print:
Move From
>
Move To
>(right HERE is where the cursor is / where text input occurs)
And seemingly skip the first prompt for entering info... it's very odd an if someone could shed light onto why the getline() function behaves weirdly in loops I'd be very grateful!
As of now it appears to run std::cout and getline() functions out of place... odd!
 
 
1 hour later…
nwp
10:28 AM
@SethTaddiken Why is this out of place? You first print "Move From\n>", then read move1, then print \nMove To\n>" and then get move2. What did you expect the output to look like?
Also do not use 65 and 48. Use 'A' and '0' instead.
 
11:16 AM
for that matter, check for well-formed input
also, Don't Repeat Yourself
a prompt function can be separate
 
 
3 hours later…
2:30 PM
@SethTaddiken Do you mean that first time program stops for input is after "Move To"?
It could be that your previous cin operation does not consume something what then get's into move1.
It's very bad code but it's correct regarding where program should wait for input.
 
3:07 PM
@EuriPinhollow yes I wasn't very clear about that, it does skip the first input and acts very strangely... I can make a corilu to show exactly what I mean in a little while... it's very funky
@nwp ah I was unclear, read up^
@nwp Yes good point I'll do that!
 
 
2 hours later…
5:03 PM
@milleniumbug I have that code in a function that i call within the loop... is that what you mean?
 
@SethTaddiken No.
8 hours ago, by Seth Taddiken
    std::string move1;
    std::string move2;
    std::cout << "Move From\n>";
    std::getline(std::cin, move1);
//     std::cout << (int)move1[0] - 65 << '\n';
    _x1 = (int)move1[0] - 65;
    _y1 = (int)move1[1] - 48;
    std::cout << "\nMove To\n>"; std::getline(std::cin, move2);
    _x2 = (int)move2[0] - 65;
    _y2 = (int)move2[1] - 48;
std::string X;
std::cout << Y;
std::getline(std::cin, X);
A = (int)move1[0] - 65;
B = (int)move1[1] - 48;
is repeated twice
 
@milleniumbug Ah I see! Thanks!
 
also, _x1 and _y1 are tightly coupled in the first place, so make it a struct Position { int x; int y; }; or sth
Position prompt_move(std::string prompt_text)
{
    std::string move;
    std::cout << prompt_text << "\n>";
    std::getline(std::cin, move);
    return Position{ (int)move[0] - 65, (int)move[1] - 48 };
}
at the simplest form
now that you have this code, you can put in input error checking there
@SethTaddiken You're most likely mixing >> with std::getline. Typical newbie mistake.
Hang on, gotta write a canonical explanation
int x;
std::string line;
std::cin >> x;
std::getline(std::cin, line);
Now let's say I write in
42
Hello world!
(42, a newline character, Hello world!, newline character)
std::cin >> x; reads in 42.
buffer contains (newline character, Hello world!, newline character)
std::getline reads everything up to newline character
buffer contains (Hello world!, newline character)
line is empty because there was no character after 42 and before the newline character
 
5:24 PM
and cin stops reading at spaces doesn't it?
ah
hold on I gotta look at this for a minute...
 
you can either put in an explicit "ignore everything up to the newline" instruction, or restrict yourself to usage of std::getline only or >> only
I typically do the latter, with std::getline
Feb 24 '17 at 20:12, by milleniumbug
@sweg_yolo_69 read entire lines with std::getline, split the lines by using std::stringstream
 
@milleniumbug this is what my code looks like (no input on corilu?) so i don't think i'm mixing them but thanks for showing me that, now i know! coliru.stacked-crooked.com/a/d06c6206ec5ce6a5
 
@SethTaddiken You do use std::cin >> variable;
 
@milleniumbug what does this mean? the getline() writes "\n, Hello world!, \n" into the buffer? I'm confused about this mention of two different buffers... :/
 
@SethTaddiken std::getline doesn't write anything. There is no two different buffers. There is one input buffer in the explanation.
 
5:31 PM
@milleniumbug oh yes you're right, that effects the program later on?
 
@SethTaddiken You can have input on coliru by piping it into ./a.out: printf 'y' | ./a.out: coliru.stacked-crooked.com/a/a382ce4ba20e6f32
 
@SethTaddiken Yes. You don't do anything in the meantime with it, so its state is as it was before.
 
@milleniumbug So this is what I want to do instead ya?:
int levels;
    std::string str;
    std::cout << "Number of computer levels? (between 1 and 4, inclusive): ";
    std::getline(std::cin, str);
    levels = std::stoi(str);
 
That's one option, yes
 
@milleniumbug It's working cleanly now, thank you! I'll Google up on why not to use cin >> and getline() in the same program... and I'll use your input function idea, much cleaner!
 
5:39 PM
@SethTaddiken I thought my explanation why was clear enough :(
 
@Justin Ah so you can't interact with it as it runs I'm guessing
 
Correct
 
though yes, there's been a lot on this subject
 
@milleniumbug Hmm, why do you mention a buffer that contains (newline character, Hello world!, newline character) after the "cin >> x" and then why does getline() change that buffer be removing the first '\n'?
 
@SethTaddiken std::cin >> int_variable; reads all the whitespace until it encounters something that looks like a number
notably, it reads all the stuff until it reaches something that doesn't
 
5:43 PM
@milleniumbug are you saying that there is one input buffer that the terminal passes to your program and cin and getline both access it differently which causes issues?
In reference to your explanation above
 
ok cin reads everything until it encounters a number...
"cin >> x" executes and i type into my terminal:

>
>
>Hello World! j2p3
Then it reads in "Hello"
and x ="Hello"
but whitespace is not "something that looks like a number" (which im guessing means characters) so it stops after hello... the rest of the text is dumped... or in some buffer?
 
25 mins ago, by milleniumbug
int x;
std::string line;
std::cin >> x;
std::getline(std::cin, line);
try reading from there again
 
@milleniumbug Ok... I see, cin is expecting an int, just confused about why you say that a buffer has (\n, Hello world!, \n) and then getline() takes in (\n, Hello world!, \n) without that first newline char
Oh wait i think i see
cin reads in the \n?
 
5:58 PM
std::cin >> x; doesn't read in the \n
 
or it ends at it and then getline() follows as a separate thing
 
it stops just before it
 
Oh when you say buffer are you just referring to all the input as a whole?
 
yes
that's the input buffer
 
all the input from a user is saved during the lifetime of the program?
 
5:59 PM
???
 
I'm thinking about an input buffer as an actual place in memory that stores all the input but are you just referring to it as the combination of all the inputs?
And that the cin's and getline's all read from it... that was my thought haha
 
it has to be stored somewhere so between the time the user types in something and you issue a read instruction it doesn't get lost
 
Ok I think I get it and see why my questions are so odd
but i don't see why "line" is empty... doesn't getline() read in "Hello world!\n"?
 
39 mins ago, by milleniumbug
std::getline reads everything up to newline character
 
oh wait
 
6:03 PM
newline is the first thing that it sees
therefore, empty
 
So cin>> stops reading at the same time that getline() begins... that transition happens at '\n', so getline() draws the short stick and is empty?
 
That's making sense! Thanks for bearing with me haha I appreciate the patience!
So could you do cin>> then a getline() that does nothing and then another getline() that does read in stuff?
 
yes, that would be one way to solve this problem.
 
cin >> x;
getline(cin, someStr);
getline(cin, someStr);
then someStr would have what you want in this context?
Asking for comprehension, I know it's ugly haha
 
6:07 PM
yes, that would make it do what you want, but it's hacky and not very clear about the intent.
 
6:19 PM
@milleniumbug Hey does this mean that getline() read in the \n is or \n is just the delimited between inputs, never read in itself?
 
This means that the getline removes \n from the input, but it doesn't put it at the end of std::string
 
ok, thanks
 
7:10 PM
@SethTaddiken MCVE please, not a hundreds of lines. "no input on corilu?" - you do it like this: coliru.stacked-crooked.com/a/70af56f174f2bd9e . You can also "share" arbitrary text (not a program) and then use it as input (you can see path to stored text after you click "share").
 

« first day (828 days earlier)      last day (2008 days later) »