Visual Studio 2017 causing problems - c++

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)

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;
}

Related

Why is the console asking for input even though there is no input in code?

I'm taking an online computer science class for grade 12 and we're using c++. I've never touched c++ and I'm starting to wish I never had. The teacher is comparing c++ to java (a language I can use perfectly fine) and we're currently learning how to input and output strings and chars. The simple practice problem was
Use one of fputc(), putc(), or putchar() to print your name one char at a time.
Since I have no clue how to use fputc() or putc() I decided to go with putchar()
#include <iostream>
using namespace std;
#include <stdio.h>
int main() {
cout << "My name is :" << endl;
putchar('J');
putchar('a');
putchar('c');
putchar('o');
putchar('b');
return 0;
}
I tried just using putchar(), and then added the cout, and have tried restarting eclipse, etc. but every time I run the program, the console asks for an input. There should not be an input for this program at all.
Try running your program from outside of the IDE and see what happens. When you launch a console program from inside of an IDE, a new console window is created to run the program in. When the program ends, the console window will close. Many IDEs setup the console to wait for you to press a key, giving you a chance to see the program's output, before the window closes.

Console Window Closing Immediately even when I Run Without Debugging

At my new workplace trying to setup C++. Couldn't get VS Code to work so we just installed the Visual Studio 2015 License we had and I'm running into some basic issues I've never had, the main one being that even when I "Start without debugging" the console window still just immediately closes and does not prompt a "Press any key.." message I'm used to seeing. I started off on Visual Studio 2017.
This is a minor issue, but I'm also used to having a basic .cpp file set up every time I start a new project and this does not happen, have to add a .cpp to the source files and type out int main().
The only solution I've found to keep the console up is to wrap everything up in a do-while loop but I'd prefer not to have to create one for every piece of code I write.
#include <iostream>
#include <string>
using namespace std;
string tellme;
int main()
{
cout << "enter some text" << endl;
cin >> tellme;
cout << "Here's the text: " << tellme << endl;
return 0;
}
No Error messages.

C++: Why does the same code run in Eclipse Neon produces different output in Visual Studio 2013?

I've been writing programs for my C++ class this semester and I have run into the problem a few times where I will write some code in Eclipse on my Mac and it does not give me the results I expect, or won't work at all. However, when I run the same exact code in class on a Windows machine with Visual Studio 2013 it works exactly as expected. An example of this is our last lab. One of the things that we had to do required us to use std::cout << '\b';, but this would not happen when I run the program on my Mac.
'\b' will not work on my Mac:
#include <iostream>
#include <iomanip>
using namespace std;
int main(void)
{
//Prints out header to console
cout << left << setw(20) << "Binary Number" << right << setw(20) << "Decimal Equivalent" << '\b' << endl;
return 0;
}
The code gives the same output without the backspace:
Does anyone know what could be causing this?
Edit:
I edited this post to be more general with my example instead of specific to my project. This problem also came up for one of my classmates who was doing his C++ coding in XCode.
The Eclipse console does not support handling backspace.
There is a long standing bug report for this (Eclipse bug 76936) At one point there was a fix but it was reverted as it caused problems. So the current state is that it still that it doesn't work.

C++ , won't display last line of code

Shouldn't this work? I mean, the code is merely a test and is meant so that the dialogue goes this way : What is your name? name here, Hello name here, and yet it does not show the last line of Hello after I type in my name and click enter it just dissapears. Here is the code.
#include <iostream>
#include <string>
int main (void)
{
using std::cin;
using std::cout;
using std::string;
string name = "";
cout << "What is your name, pls?\n";
cin >> name;
cout << "\nHello " << name.c_str() << "\n";
return 0;
}
My guess is that you are running from the debugger, or double clicking the executable. In either of those cases, when the program ends, the console will close. So, the program produced output, but you just could not see it before the console closed.
Run the program from a pre-existing console so that the console remains after your program ends. Or, just whilst debugging, arrange that your program does not terminate immediately after emitting its final output. A simple way to do that is to place a break point at the end of the program.
It probably showed it right before it disappeared. If you're going to write console programs, and if you're going to send output to a console, you should run them from a console so the output has some place to go.
After you are done with your program, press Ctrl + F5 ( Run without debugging). This will prompt before closing the window and this is what you want.
Make sure you put a breakpoint before main goes out of scope. I guess your console disappears under VS?
Also, you don't need to extract the char* in the last cout statement:
cout << "\nHello " << name << endl;
Open a terminal (or a command prompt window).
Navigate to the folder that contains the executable.
Run it.
It's not disappearing. It is just running really fast.
Every IDE has a keyboard shortcut that allows you to run code and pause after the execution has finished.
This keyboard shortcut is Ctrl-F5 in Visual Studio.
I have no idea what IDE you're running, but that is your basic problem.
The other thing you can do is to test your code in ideone : ideone.com/hb4Cel (it's the same code. There is no point pasting it here)
A dirty workaround is to add something like this
cin >> name;
at the end, just before return 0;. It forces the window to wait for input (i.e. hitting return) before returning (which closes the program).
This isn't necessarily good design, but if all you want to do is run some tests then it'll do the trick.
Basically when you enter your name it displays your last line and exits after return 0.
Here are the following things to avoid that
1- use command line to run the application
Start->accessories->command prompt
Go to folder in which your application is using cd command
c:>cd c:\path\foldername
Now run the application by typing the program name e.g
c:\path\foldername>my_application.exe
It will display your last line.
2- Now if your are using microsoft visual c++ press ctrl+F5 to run your program
3- This is not recommended but you an use it as long as your are debugging then remove it from the code afterwards. Include conio.h header file and add getch(); line before return statement. It would hold the screen for you till you press a key.

Visual Studio C++ -> Local Windows Debugger

While running a program in Visual Studio C++ editor, when i click on Local Windows Debugger, the command prompt window opens up, where i am asked to input data (depending on my program). The problem is the window doesn't stay very long after the output is shown.
What do i need to do to keep the window up for a longer time, or at least until i close the window myself?
I tried for over half hour to check various options and see if there is anything that can be done to prolong the duration.
Thanks and regards,
Nikhil Kenvetil
Try to use one of the below methods to add some codes at the end of the main() function:
getchar();
char ch;
cin>>ch;
Try this:
#include <iostream>
#include "conio.h"
int main()
{
std::cout << "Hello World\n";
getch();
return 0;
}
you could try using system("PAUSE");