cout not working when using string

This basic code below shows my problem. When I don't use string, cout works fine but when I start using it, it stops printing things. Also, I can include <string> and still have cout working but when I create a string (ex.: string name;), cout stops working.

I hope that's enough information so that I can get help. Thanks!

Here's my code:

#include<string>
#include<iostream>
using namespace std;

int main()
{
string name;
cin >> name;
string message("hi");
cout << name << message;
return 0;
}
Works for me with Visual Studio. What is your compiler?
Sublime Text and PowerShell.
Last edited on
Sublime text is a compiler I have never heard of.

This is probably going to sound like a weird question, but after you start the program what are you entering at the prompt?

Here's the program output when I compile your code in VS2019:
Joe
Joehi

If I were to write the code I'd make it more obvious you want some type of input from the user, and format the output a bit:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string>
#include <iostream>

int main()
{
   std::cout << "Enter your first name: ";
   std::string name;
   std::cin >> name;
   
   std::string message("hi");
   
   std::cout << '\n' << name << ", " << message << '\n';
}
Enter your first name: Joe

Joe, hi


Beyond recommending getting a different compiler I really can't see why you are having problems.
Sublime is a text editor. (A really, really nice one.)
PowerShell is Microsoft's “Windows Console Killer” app. (I hate it.)

What compiler are you using? MSVC? MinGW? Clang++?
Is there anything else unusual about your environment?
Hey, thanks for the answers, I personally use MinGW and I'm pretty sure that Sublime Text isn't the problem here.

Also, Duthomhas, what Console do you use?
Duothomhas wrote:
Sublime is a text editor. (A really, really nice one.)

I'll have to take your word about it, I've never heard of it before and I don't need yet another text editor. I've got several already. For plain text, Rich Text and MS Word compatible.

I know of PowerShell, and I concur about it being wretched.
1
2
3
4
5
6
7
8
9
10
11
12
PS C:\Users\Paul\Desktop\C++> .\prog2.exe
PS C:\Users\Paul\Desktop\C++> Test
Test : Le terme «Test» n'est pas reconnu comme nom d'applet de commande, fonction, fichier de script ou programme
exécutable. Vérifiez l'orthographe du nom, ou si un chemin d'accès existe, vérifiez que le chemin d'accès est correct
et réessayez.
Au caractère Ligne:1 : 1
+ Test
+ ~~~~
    + CategoryInfo          : ObjectNotFound: (Test:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS C:\Users\Paul\Desktop\C++> 


This is what I get on my console.

The text in French is basically meaning that what I've written ("Test") isn't a known command, which basically means that the code has ended before I could put an answer.
I just tried compiling my code on an online compiler and it worked perfectly.
what Console do you use?

I use the Windows Command Processor, cmd.exe.

You can start it:

1. Start Menu, type "cmd" and press enter.

2. This works for Windows 10, it may work for Win 7, using File Explorer navigate to the folder where your executable is. In the address window that shows the location of the folder, click and highlight the contents, type "cmd" and press enter.

Using File Explorer has the advantage of starting the command prompt with the location already set. Win 10 you can drag and drop the .exe file into the command prompt.

There are several Integrated Development Environments available that allow you to edit code, compile and run the executable from within the IDE. Makes writing and testing code go much quicker, and helps with debugging apps.

Two IDEs that I know of for Windows (both are free):

Visual Studio 2017/2019

Code::Blocks

I have both and personally prefer using VS over C::B.
Last edited on
The Problem

You left out the information about using French output.

That will hang the Windows Console / PowerShell Console. MinGW console streams (cin, cout, cerr, and wcin, wcout, wcerr) are particularly bad at it (== totally broken). You must use the Windows Console API to do it right.

I have written a small library (less than 200 lines of actual code) to repurpose the console streams (cin, cout, cerr) to seamlessly make (== no changes in code) Windows UTF-8 (and to fix main()’s argv[] to supply UTF-8 data), but it is not quite ready to go up on GitHub yet. If you want I can email you a working preview version that will work with GCC or MSVC, and your problems will disappear. (PM me.)

However, for now I just recommend using MSVC and using the wcin, wcout, wcerr streams, and std::wstring class, which work just fine in MSVC.

Again, you cannot get past the problem in MinGW; the reasons run deep and are assuredly stupid in the extreme.

As a stop-gap measure: after every input/output, simply cin.clear();/cout.clear();. This keeps things from hanging, but doesn’t actually fix the underlying problem.


What Console?

Primarily ConEmu|https://conemu.github.io/

Also the standard Windows Console, but I have it fairly customized. (The key is managing your PATH.)

And, when I need to compile something Linux-y, MSYS2|https://www.msys2.org/


Sublime

It is well-named. The authors ask $70 for it, but you can technically download it and “evaluate” it indefinitely. I prefer honesty, and will pay $70 when I have the extra funds to spend on it.

I also like Notepad++|https://notepad-plus-plus.org/ as a close second.

I can’t stand Atom. Or Emacs. Or Windows Notepad, LOL.
Topic archived. No new replies allowed.