Console Window Closing Immediately even when I Run Without Debugging - c++

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.

Related

Visual Studio 2017 causing problems

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

Infinite loop after cin c++

I'm pretty newbie at c++, so this confuses me.
This code seems to loop infinitely after std::cin :
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "My name is Martin. What's yours?" << endl;
string name;
cin >> name; //It seems to loop around here
cout << "Ah, so you are" << name << "." << endl; //It doesn't print this message
return 0;
}
Thanks!
EDIT: I'm not sure I understand your answers, but what happens is that when you enter a name and press enter, it does nothing. The terminal just continues to the next line, letting you write something on that one too. It does this infinitely, a little bit like when you just press enter without any text.
EDIT 2: My bad, I should try to clear up things. This is what comes in the JDoodle console:
My name is Martin. What's yours?
JDoodle - Timeout - Some common reasons for Timeout
Your Program may have a endless loop
Please check the program and try again or contact JDoodle support at jdoodle#nutpan.com for more info.
The reason I believe it is a loop is that in a console that doesn't write timeout, I can continue to input on a new line all the time.
Since you are using JDoodle the console will be waiting on the server. If you don't type anything there it hangs until it eventually gets killed for being too slow.
The way to fix this is to enter the text you wish to type before running the program into the box labeled "Stdin Inputs...".
Alternatively you can set the "Interactive mode" to "On" which will show the console in the website where you can enter some text and press enter. (I tried, it works).

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.

cout Not Working Reliably

I was working on a school project to make a blackjack/21 console application. The program is somewhat long and complicated, so will only put the offending code here. Complete code will be below.
cout << "Before comp instruct";
cout << endl << "Computer: ";
cout << "After comp instruct";
It will always print the first statement, but it sometimes stops immediately after that. It happens only sometimes, depending on the input in the program. I (and my teacher and peers) could not figure out why this program stops after the first statement. At least one other person in the class is having a similar problem.
These were done using CodeWarrior IDE on a Windows 7 Dell desktop.