CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2009
    Location
    England
    Posts
    66

    [RESOLVED] Console programming in Visual Studio

    I have switched to using Visual Studio community 2022 on Windows 10. Producing a hello world to the console is no problem.

    My issue is that I can not change the console title (it stays with - Microsoft Visual Studio Debug Console) or do anything with the console provided. I've tried freeconsole() and allocConsole(), even AttachConsole() but nothing I do allows me to manipulate the console window provided by visual studio.?

    What am I missing here?
    What the mind can conceive it can achieve.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,903

    Re: Console programming in Visual Studio

    To set the console window title:

    Code:
    SetWindowText(GetConsoleWindow(), "A window title");
    do anything with the console
    What have you tried?

    These are the console API's:
    https://learn.microsoft.com/en-us/wi...sole-functions
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jan 2009
    Location
    England
    Posts
    66

    Re: Console programming in Visual Studio

    I am trying to move code over when I used CodeBlocks ide. I just don't see my console window like I could in CodeBlocks (CB::Runner). I need to be able to create my own console window and manipulate it, like I could before. In fact, it's code you yourself helped me with!

    Console Title still doesn't change.

    Code:
    #include <iostream>
    #include <Windows.h>
    
    
    int main()
    {
    	SetWindowText(GetConsoleWindow(), TEXT("A window title"));
    
    
    	std::cout << "Hello" << std::endl;
    
    
    	return 0;
    }
    What the mind can conceive it can achieve.

  4. #4
    Join Date
    Jan 2009
    Location
    England
    Posts
    66

    Re: Console programming in Visual Studio

    This doesn't work either.

    Code:
    #include <iostream>
    #include <Windows.h>
    
    
    int main()
    {
    	SetConsoleTitle(TEXT("My console Window"));
    
    
    	std::cout << "Hello" << std::endl;
    
    
    	return 0;
    }
    What the mind can conceive it can achieve.

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,723

    Re: Console programming in Visual Studio

    It looks like the title changes once the code finishes. try adding the following before "return 0"
    (and #include <string>)

    Code:
    std::string s;
    std::getline(std::cin, s);

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,903

    Re: Console programming in Visual Studio

    Console Title still doesn't change.
    How are you running the program exe? From the IDE or from within the console? Have you tried running the program direct from within the console?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Jan 2009
    Location
    England
    Posts
    66

    Re: Console programming in Visual Studio

    I managed to solve this issue early today before logging in here. I am so embarrassed. Everything was working ok, just that my laptop was too fast! Now I have a console debug window as provided by visual studio and my separate console window that I've created. Thanks 2kaud, you were right as well, I was close with my solution.

    Code:
    #include <iostream>
    #include <Windows.h>
    
    
    int main()
    {
    	FreeConsole();
    	AllocConsole();
    
    
    	SetConsoleTitle(TEXT("My console Window"));
    
    
    	std::cout << "Hello" << std::endl;
    
    
    	std::cin.get();
    
    
    	return 0;
    }
    What the mind can conceive it can achieve.

  8. #8
    Join Date
    Jan 2009
    Location
    England
    Posts
    66

    Re: Console programming in Visual Studio

    Thanks to Philip Nicoletti also, appreciated.
    What the mind can conceive it can achieve.

  9. #9
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,237

    Re: Console programming in Visual Studio

    Just a little aside note: SetConsoleTitle does his job either with or without the solution you have found.

    Visual Studio IDE (afaik since VS2010) has the following feature designed to make the programmer's life a little bit easier: if you start a console application from Visual Studio, it stops when exits the main function, as shown in the picture.

    debug-console.jpg

    That is to avoid adding (for testing purpose) before main function return, code which is not necessary in the final release, like std::getline() or std::cin.get().

    This is what I say: forget the obsolete advices you can find over the Internet! If you want to see how the console window looks when application exits, just put a breakpoint and run a debug build.
    brakpoint.jpg
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured