Strange Cout

cout << "INSERISCI STRING1: ";
cin >> string1;

cout <<"INSERISCI STRING2: ";
cin >> string2;

if i write in this way the output is

inserisci strin1:""

inserisci string2:"

why clion put the space in the new line and not in the current line as i write?
I'm not sure what you're asking. Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string>
#include <iostream>

using namespace std;

int main()
{
	string string1, string2;

	cout << "INSERISCI STRING1: ";
	cin >> string1;

	cout << "INSERISCI STRING2: ";
	cin >> string2;
}


which gives:


INSERISCI STRING1: str1
INSERISCI STRING2: str2


Note that the cin >> only completes when a <CR> is entered following text. If just a <CR> is entered then a blank line is shown but input is still required until some text has been entered followed by a <CR>.

Last edited on
1
2
3
4
5
6
7
8
9
int main() {
    char * ret_accoda_stringhe;
    char string1[30+1],string2[30+1];

    cout <<"INSERISCI STRING1: ";
    cin >> string1;

    cout <<"INSERISCI STRING2: ";
    cin >> string2;


"C:\Users\Giuseppe\Desktop\SECONDO SEMESTRE\ALGORITMI&CALCOLATORI\progetti algoritmi\Esercitazioni\cmake-build-debug\Es2_2.exe"
INSERISCI STRING1:ciao 
 INSERISCI STRING2:mamma


the space after ':' isn't written in the output, but in the cout i write : ";
there is no space between : and ciao, but the space there is at the beginning of the new line
Last edited on
Well on my Windows system, using:

1
2
3
4
5
6
7
8
9
10
11
12
include <iostream>
using namespace std;

int main() {
	char string1[30 + 1], string2[30 + 1];

	cout << "INSERISCI STRING1: ";
	cin >> string1;

	cout << "INSERISCI STRING2: ";
	cin >> string2;
}


I get:


INSERISCI STRING1: ciac
INSERISCI STRING2: mamma


What OS are you using? What compiler/version?

It looks like there's no sync between input/output, but the input/output streams have been tied for a long time in C++.

What happens if you use:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main() {
	char string1[30 + 1], string2[30 + 1];

	cout << "INSERISCI STRING1: " << flush;
	cin >> string1;

	cout << "INSERISCI STRING2: " << flush;
	cin >> string2;
}


Last edited on
i use windows 10 pro, clion 2020.3

i use your code, but i have the same result :(
You should try a different compiler. Code::Blocks or Visual Studio are good ones that are free and work well with Windows.
Does your CLion use MinGW? If so, what version? You most likely can continue to use CLion as an IDE, but try to hook it up to a different compiler instead.
Last edited on
sorry MinGWw64 v6.0
Just doing a cursory search, it looks like there are some similar issues that people have faced when using MinGW-w64,
https://youtrack.jetbrains.com/issue/CPP-2580
https://youtrack.jetbrains.com/issue/CPP-11382

Apparently one of the workarounds is,
try doing the following: in Registry (Help | Find Action..., type Registry there) disable the run.processes.with.pty option and restart CLion. Does that help?
According to the response in CPP-12752 disabling PTY (without CLion restart, since the run.processes.with.pty option is not saved after CLion's restart - CPP-8395) helps.


You could also try running the program in cmd instead of whatever window CLion shows.
Last edited on
Thanks @Ganado you got it!
Topic archived. No new replies allowed.