How to place a space at the end of cout

I need to know why c++ doesn't see the space just at the end of cout function. I'm using CLion and C++ 23 (language_standart)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>

using namespace std ;

int main()

{

    string Item ;
    double Price ;
    int Quantity ;
    double Total ;

    cout << "Your item to buy : " ;
    getline(cin, Item) ;
    cout << "Price of the item : " ;
    cin >> Price ;
    cout << "Quantity of the item : " ;
    cin >> Quantity ;

    cout << endl ;

    Total = Price * Quantity ;

    cout << "Item : " << Item << endl ;
    cout << "Price : " << Price << endl ;
    cout << "Quantity : " << Quantity << endl ;
    cout << "Total is : " << Total << endl ;

}


When I run the code, it returns me
1
2
3
Your item to buy :**HereIsNoSpace**
**HereIsSpace**Price of the item : 
**HereIsSpace**Quantity of the item :

So I need to enter an Item just after ":" and not ": "

And as you can see probably my spaces somehow goes just after my input and passes to the next string
Last edited on
Put your headers in so that we can run your code.
1
2
3
#include <iostream>
#include <string>
using namespace std;




And as you can see probably my spaces somehow goes just after my input and passes to the next string

I can't reproduce your problem - the code seems to work fine on cpp.sh (and I can't see why it wouldn't work fine anywhere else, either).

Your item to buy : Shirt
Price of the item : 30
Quantity of the item : 4

Item : Shirt
Price : 30
Quantity : 4
Total is : 120
Last edited on
I mean there should be a space just after the colon, just as a have written in the code, but when I run this code there is no space after the colon, they kinda passes to the next string and are placed before the Price of the item and Quantity of the item. So when I run the code Price of the item and Quantity of the item are displaced and farther than Your item to buy. At stake overflow people say that this may be a problem with the terminal, but I don't know what to do with it, cause I don't even use a cmd in CLion
Last edited on
I mean there should be a space just after the colon

I understood what you meant.


but when I run this code there is no space after the colon

But I can assure you that I don't find that.


At stake overflow people say that that may be a problem with the terminal

"Stack Overflow". Anyway, they are probably right.


Try it on cpp.sh
It works OK with MS VS 2022:


Your item to buy : foobar
Price of the item : 78.98
Quantity of the item : 9

Item : foobar
Price : 78.98
Quantity : 9
Total is : 710.82


If the issue still persists, it looks like it could be a sync issue between i/o streams. I'd raise it as an issue with Clion support. There may be a compile option etc needed to make sure the output stream is fully output before input.

PS. If you put after L15:
 
cout.flush();


does the same happen - or does the issue go away for the first display?

Last edited on
In cpp.sh everything works perfectly of course, so I can guess that this is a problem with CLion or something... Very strange ... :( Can't even imagine why can it be so
Even with cout.flush(); space isn't printing ...
Maybe try running the program outside of the CLion IDE itself. Run it in a standard terminal/command prompt.

Edit: I knew this sounded familiar. It might the same issue as this thread
https://www.cplusplus.com/forum/general/274761/

What compiler are you using?
Last edited on
I'm using CLion and MinGW, i'll try what you said to that guy... Ganado thank you so much ! I hope it'll help !!
Last edited on
It've helped !!! Ganado you're my savior :))) Thank you so much
Happy to help.
Topic archived. No new replies allowed.