-
May 24th, 2024, 10:33 AM
#1
[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.
-
May 24th, 2024, 10:52 AM
#2
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)
-
May 24th, 2024, 11:29 AM
#3
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.
-
May 24th, 2024, 11:34 AM
#4
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.
-
May 24th, 2024, 05:39 PM
#5
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);
-
May 25th, 2024, 04:10 AM
#6
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)
-
May 25th, 2024, 05:04 AM
#7
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.
-
May 25th, 2024, 05:05 AM
#8
Re: Console programming in Visual Studio
Thanks to Philip Nicoletti also, appreciated.
What the mind can conceive it can achieve.
-
May 25th, 2024, 07:12 AM
#9
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|