VS Not Prints To The Output Window - visual-studio-2013

I would like to print and see the message in the "Output Window" in Visual Studio 2013. After running this C++ code:
#include <iostream>
using namespace std;
int main(){
cout << "hello world";
}
I cannot see the message in the output window. Instead, a black window in which the message "hello world" is written seems instantly and closes.
I disabled "Redirect all output text to the Immediate window". However, it is still not printing the message.

This behavior is expected, because the program is completing its functions. You can change the code to this:
#include <iostream>
using namespace std;
int main(){
cout << "hello world";
system("PAUSE");
}
Which will make the window pause until you hit a key to continue, but after printing hello world the program is finished running and will close.

Related

Visual Studio VS 2022 won't display cout simplest Code

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
string fileTxt;
cout << "disply ok";
return 0;
}
i was working on file handling C++ ifstream but i can't get the Code to open my .txt, so i start troubleshoot and removing all the Codes to only have "hello world" program remaining...
And that still won't work, all i got is a Terminal that goes off instantly...without any text i asked it to display eg. "dsply ok";

keyboard interrupt routine visual studio C++ console app

I am using VS 2022 Preview to write a C++ console application. I wish to detect a keyboard hit and have my interrupt handler function called. I want the key press detected quickly in case main is in a long loop and therefore not using kbhit().
I found signal() but the debugger stops when the Control-C is detected. Maybe it is a peculiarity of the IDE. Is there a function or system call that I should use?
Edit: I am vaguely aware of threads. Could I spawn a thread that just watches kbd and then have it raise(?) an interrupt when a key is pressed?
I was able to do it by adding a thread. On the target I will have real interrupts to trigger my ISR but this is close enough for algorithm development. It seemed that terminating the thread was more trouble than it was worth so I rationalized that I am simulating an embedded system that does not need fancy shutdowns.
I decided to just accept one character at a time in the phony ISR then I can buffer them and wait and process the whole string when I see a CR, a simple minded command line processor.
// Scheduler.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <Windows.h>
#include <iostream>
#include <thread>
#include <conio.h>
void phonyISR(int tbd)
{
char c;
while (1)
{
std::cout << "\nphonyISR() waiting for kbd input:";
c = _getch();
std::cout << "\nGot >" << c << "<";
}
}
int main(int argc, char* argv[])
{
int tbd;
std::thread t = std::thread(phonyISR, tbd);
// Main thread doing its stuff
int i = 0;
while (1)
{
Sleep(2000);
std::cout << "\nMain: " << i++;
}
return 0;
}

How do I handle errors in Lua when executing arbitrary strings?

I'm going for absolute minimalism here. (It's been a while since I've worked with the Lua C API.)
#include <lua.hpp>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char** argv)
{
lua_State* state = luaL_newstate();
luaL_openlibs(state);
string input;
while (getline(cin, input))
{
auto error = luaL_dostring(state, input.c_str());
if (error)
{
cerr << "Lua Error: " << lua_tostring(state, -1) << '\n';
lua_pop(state, 1);
}
}
lua_close(state);
return 0;
}
This program works fine as long as I feed it perfect Lua. However, if I enter something bad (such as asdf()), the program crashes! Why is it not handling my error gracefully?
I've tried breaking out the calls before. It crashes on the call to lua_pcall itself. I never make it past that line.
The binary download (5.2.1 I believe) has a bug that was corrected in 5.2.3. I rebuilt the library from source, and now my program works fine.

Debugging file scope in Visual Studio

In Visual Studio 2012, I have this uber-simple C++ program:
#include "stdafx.h"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << std::cout;
int i = 1;
return 0;
}
In case you wonder, the "std::cout << std::cout" part is only to show that std:cout can be accessed within the scope of the function - yes, it's meant to print the pointer to the console.
So I set a breakpoint at "int i = 1". When the breakpoint triggers, I want to inspect std::cout from the Command Window (or Immediate Window), so I type:
>Debug.Print std::cout
But it returns the following error:
identifier "std" is undefined
I don't understand why this happens, std should be in the scope of the function which is being executed. The same goes for any other stuff coming from the #include directive, I just can't inspect it using Debug.Print. What do I use in Visual Studio 2012 to inspect EVERYTHING accessible in the execution scope? It seems I can only access local variables with Debug.Print.

Keeping Console Window Open in Visual Studio (C)

I usually use XCode but was having a problem opening a file with this code:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
int main(void )
{
printf("Hello");
FILE *filePtr;
filePtr = fopen( "test.txt", "r" );
if (filePtr == NULL)
{
fprintf(stderr, "Can't open \"test\"\n");
exit(EXIT_FAILURE);
}
else
{
int x;
printf("File open successful\n");
/* read one character at a time until EOF is reached */
while ((x = fgetc(filePtr)) != EOF)
{
//printf("%c", x);
fprintf(stderr, "%x\n",x);
}
}
fclose(filePtr);
system("pause");
return EXIT_FAILURE;
}
The console window closes so fast and at the bottom bar of VS it says: "'C_test.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f863c71f\msvcr90d.dll'
The program '[1116] C_test.exe: Native' has exited with code 1 (0x1)."
What does that mean?
Also, can anyone point me to good VS starting points / tutorials?
See also How to keep the console window open in Visual C++?
The reason you can't see it is because there is no possibility for your program to pause during execution. In Visual Studio the typical behavior is to close the console window the second the program has completed its execution.
The bottom bar is telling you that the program complete and what the return value was (1 in this case).
What I would also do is add code right before the exit point of the program with #ifdefs:
#ifdef VS_DEBUG
fgetc(STDIN);
#endif
Now your program will pause when it's done and wait for a keypress then close the window.
I'm sure there is also a way in the project settings to prevent the closing, I've never looked myself.
I generally leave a breakpoint on the closing brace of main, so that the output of my window is visible while debugging, but Visual Studio will keep the console window open if you start the program without debugging (Ctrl+F5). Alternatively, you could simply ask for input, #MadcapLaugher's fgetc(STDIN); is probably your best bet - though I would add a prompt: "Press any key to continue... "
#include<stdio.h>
int main()
{
printf("Hello World from visual Studio \n");
//to prevent console window temination
system("pause");
return 0;
}

Resources