0

Why does C++ act so weird sometimes?

Sometimes when I'm doing a program in C++, I'll have two (cout)s, but only one of them outputs anything, sometimes I'll have three cin requests, but it will only accept one. Why does it act so weird sometimes? It's never this inconsistent with other languages

8th Sep 2018, 6:40 PM
jacksonofgames 28
jacksonofgames 28 - avatar
13 odpowiedzi
+ 3
jacksonofgames 28 (tiny detail, line 3 needs endl, not end - otherwise 'compilation error')
8th Sep 2018, 8:10 PM
Kirk Schafer
Kirk Schafer - avatar
+ 1
Hi, Executing and even some aspects of coding in sololearning, in special how input is provided, is different from executing C++ compiled code in a command terminal. Does the strange behaviour you described happens when you execute the code in a terminal or only on sololearn application?
8th Sep 2018, 7:06 PM
Mark
+ 1
Hi jacksonofgames 28, So are you trying your code on online editors with embedded c++ interpreters/compilers? Did you try to use an actual C++ compiler installed on your machine? If you want to run your code on a browser that behaves a bit more similar as it is executed in a command terminal you can try this one: www.cpp.sh Another thing, when entering strings that contains white spaces, like in a phrase, the input with "cin" does not work, that is for no white spaces strings. Instead, use the getline() function.
8th Sep 2018, 11:53 PM
Mark
+ 1
"C" for CAT so it make sense
9th Sep 2018, 1:50 AM
Fajar Maftuh Fadli
Fajar Maftuh Fadli - avatar
+ 1
Hi jacksonofgames 28, I tested on my machine the code you posted and it did work perfectly. I compiled it with g++ and run with bash. Remember that when using "cin" to input data you cannot type white space characters like spaces and tabs. For getting a multiple word string use the getline() function.
9th Sep 2018, 4:40 PM
Mark
+ 1
Hi again jacksonofgames 28, Thy this code: https://code.sololearn.com/cKM8sW9MQPru/?ref=app It will work properly even here in sololearn. For some additional info on how to deal properly with I/O and some buffer issues please read this: https://www.google.com/amp/s/www.geeksforgeeks.org/clearing-the-input-buffer-in-cc/amp/
9th Sep 2018, 5:16 PM
Mark
0
Hi Jackson, This is very bizarre, it does not ever happened in my C++ code. But you have to be careful and well aware how I/O is treated, specially input. Do you have any example code? It would be easier to understand what is happening with a piece of code.
8th Sep 2018, 6:58 PM
Mark
0
I don't know what you mean. Never encountered such behavior. In Sololearn app you need to know, that every input has to be given in advance. So if you have 2 inputs, you need to input both separated in new lines. e.g. int a,b; cin>>a; cin>>b; You need to type 3 4 in the Sololearn input prompt
8th Sep 2018, 7:00 PM
Matthias
Matthias - avatar
0
Well I first tried the code described in Sololearn, but I know its touchy, so I tried it in a couple other text editors to the same result. Its always something like: void func() { string a, b; cout << "Enter a string" << end; cin >> a; cin >> b; cout << a << endl; cout << b << endl; } int main () { func(); return 0; } In other text editors, this doesnt even give me the option to enter the second input, and in those editors and sololearn the cout attempt to output a and b both don't do anything. They don't get printed
8th Sep 2018, 7:56 PM
jacksonofgames 28
jacksonofgames 28 - avatar
0
FYI, the text editor is the less important thing when creating a source code. it just makes a file with the contents you want in it. when testing code, you should test the compiler. that is what "interprets" the code.
8th Sep 2018, 8:11 PM
Bebida Roja
Bebida Roja - avatar
0
Bebida Roja but different text editors can have different interpreters. Kirk Schafer Yeah that was just part of the program that didn't copy paste right. It is endl in the program
8th Sep 2018, 8:28 PM
jacksonofgames 28
jacksonofgames 28 - avatar
0
Mark yeah I actually have MinGW on my computer and made an executable file in the command prompt and it did the same thing, its kinda weird
9th Sep 2018, 4:34 PM
jacksonofgames 28
jacksonofgames 28 - avatar
0
Thanks Mark, and thanks to all of you who responded
9th Sep 2018, 6:24 PM
jacksonofgames 28
jacksonofgames 28 - avatar