0

I'm a complete begginer when it comes to programming. I'm trying to learn using the book "Programming: Principles and Practice Using C++" and it recommended downloading Visual Studio to code (I was previously using Codeblocks). I downloaded the newest version (2017), got the classic applications packet for C++ and created a project.

But when I wrote my first code

#include "stdafx.h"
#include "../../std_lib_facilities.h"


int main()
{
    cout << "Hello world!";
    keep_window_open();
    return 0;
}

Visual Studio started to freak out. When I click the local debugger button, the console app goes to the taskbar, and when I click on it, I can't do anything with it. It jsut displays "Hello World!" but no "Please enter character to exit" as it should. It doesn't react to keyboard input. I can't close it (even if I use task manager and try to close the process manually). Only solution I found is clicking "continue" button in Visual Studio, but it's not very convenient to use as the app still doesn't quite work the way I want to. Is there a way to make it work like it does in codeblocks - I just put in code, click a button and I have a fully functional console app?

One thing that could cause it - I was getting an error about using , so following advice from the internet I added

#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS 1

at the beggining of the header std_lib_facilities.h

Any way someone could help me out? (Maybe I should just go back to Codeblocks and ignore Visual Studio? That doesn't seem like a good way though)

Mihu
  • 69
  • 6
  • Sounds like the "local debugger" environment is not what you expect, you want may want to "Start Without Debugging" (or Ctrl` + F5)? Basically "run program" not "debug program" – chickity china chinese chicken Dec 13 '17 at 19:29
  • [This Question](https://stackoverflow.com/questions/31909879/how-to-keep-window-open-not-working-depending-on-inputs) may not help now, but it would seem there are some issues. – crashmstr Dec 13 '17 at 19:37
  • Perhaps your antivirus is causing this behavior. aVast is known to cause issues like that. Some other antivirus software also do this. Try disabling your AV in either case. – drescherjm Dec 13 '17 at 19:43
  • What is the definition (body) of your keep_window_open(); function? – SoronelHaetir Dec 13 '17 at 19:43
  • I solved the problem by getting rid of Avast and opening VS as administrator. Thank you for your help. – Mihu Dec 13 '17 at 19:55
  • 1
    You should not need (or want) to run VS as an administrator. Turning off the sandbox feature in aVast should be all that is needed. Or adding an exception for the folders where your projects exist. – drescherjm Dec 13 '17 at 20:09
  • My confusion was caused by an accidental use of a breakpoint. I didn't know what this feature did at that time, and simply restarting VS made everything work perfectly. The problem 99% wasn't caused by Avast. So, for everyone who has this problem in the future - just click on a red dot left to your code. – Mihu Dec 13 '17 at 20:22

1 Answers1

0

If you only want to have the behaviour of "Please enter character to exit", you could just include the cstdio header and use std::getchar() at the end. This will make sure the program does not exit until you press any key on the keyboard.

However, this method does not print out the message "Please enter a character to exit" so you would need to print it yourself using cout.

The main.cpp would end up looking something like this:

#include "stdafx.h"
#include <cstdio>
#include <iostream>

int main()
{
    std::cout << "Hello World" << std::endl;
    std::cout << "Please enter character to exit" << std::endl;
    std::getchar();
    return 0;
}
peti446
  • 330
  • 1
  • 4
  • 12
  • This is not quite what I ment, as function "keep_window_open" does exactly what you wrote. The problem has been solved in the comments though. – Mihu Dec 13 '17 at 20:00