I used visual studio 2013 two days ago.
But now... It does not working.
I tested below c code and cpp code.
//#include <stdio.h>
#include <iostream>
using namespace std;
void main(){
//printf("hi");
//cout << "test" << endl;
int a;
cin >> a;
cout << a;
}
The result of run is just blinking console.
And does not appear "Press Enter..."
result
If I try to debug then Visual studio stop and no response.
I do reset all options :(
Your console is showing the output but not waiting for you to see it.
you need to add break point or for workaround you can use
cout << a;
int ch = std::cin.get();
}
you must be able to see the value of a.
EDIT: ohh you would need fflush too, my fault too miss it.
cout << a;
fflush(stdin);
int ch = std::cin.get();
}
Related
This question already has answers here:
How to keep the console window open in Visual C++?
(23 answers)
Closed 4 years ago.
My IDE is Visual Studio 2017.
I am pretty new in C++ programming so I need a help about understanding principles of creating a new C++ project in Visual Studio.
So, in my first solo attempt i just chose a empty project option and afther that i chose to add new item and i write this sample code:
#include <iostream>
using namespace std;
int main()
{
return 0;
}
Afther this step and afther steps with compiling, building and a starting without debugging i did not get any message or consol window with time of code execution or option for entering any key for ending.
What is needed for getting this kind of information at the end of code?
You shouldn't use system("pause"); you can read here why. It's platform dependent and adds a huge overhead loading all the Windows specific intstructions.
So you should choose nicer alternatives: std::cin.get() for example. Which will work most of the time. Well, except if there had been input before (std::getline or std::cin). If you're creating a program with user input - use std::cin.ignore() twice to guarantee a "press enter to continue" effect:
#include <iostream>
int main() {
int a;
std::cin >> a;
std::cin >> a;
std::cin >> a; //etc
std::cout << "press enter to exit - - - ";
std::cin.ignore(10000, '\n');
std::cin.ignore(10000, '\n');
return 0;
}
also please don't use namespace std; read here why.
If you don't like this 3-liner (because it looks ugly) you can pack it in a void function and treat the whole thing as a black box:
void pause() {
std::cout << "press enter to exit - - - ";
std::cin.ignore(10000, '\n');
std::cin.ignore(10000, '\n');
}
int main(){
pause();
return 0;
}
Converting Blaze's comment to an answer
Go to Tools->Options->Debugging and look for an option called "Automatically close the console when debugging stops" and ensure that this option is not activated.
I did not get any message or consol window with time of code execution or option for entering any key for ending
Because you didn't ask for it.
What is needed for getting this kind of information at the end of code?
To perform input (see std::cin and operator<<) and output (see std::cout and operator>>). Example:
#include <iostream>
int main()
{
std::cout << "Press enter to terminate\n";
std::cin.get();
}
Whenever I compiled my C++ program in codeblocks ide, it runs but after once it show error that
"cannot open output file C:\Users\AkM\Desktop\code\g1.exe Invalid argument|" .
I had tried killing process(.exe file) from Task Manager, opening and closing codeblocks but same things happen all the time.
Please help, it really creates trouble for me while coding. This happens with almost every C++ program.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n ,m,k;
cin >> n >> m >> k;
int arr[n];
for(int i = 0;i<n;i++)
cin >> arr[i];
int l = n + m;
vector<int>vec[l];
int x,y;
for(int i =0;i<m;i++)
{
cin >> x>>y;
vec[x].push_back(y);
vec[y].push_back(x);
}
cout << vec[1][3] << endl;
vector<int >vv;
vector<int> v;
for(int i = 0;i<n;i++)
{
for(int j=0;j<vec[i].size();j++)
{
v.push_back(vec[i][j]);
cout << "hello" << endl;
cout << vec[i][j]<<" ";
cout << "hello" << endl;
cout << "hello" << endl;
}
sort(v.begin(),v.end(),greater<int>());
if(v.size()>k)
{
vv.push_back(1);
}
else
{cout << "hello" << endl;
vv.push_back(v[k-1]);
}
v.clear();
cout << endl;
}
for(int i =0;i<vv.size();i++)
cout << vv[i] << endl;
}
ERROR LINK :
This is a common error on IDE and modern systems. Most of the time, when you run a program, the executable file is kept open by the running program, and this is enough to prevent changes to the file => the link phase cannot complete.
The normal solution is just to ensure to close any running instance of the program before a build.
When there is a crash in a program launched from the IDE, the IDE can open the process in debug mode to allow the programmer to see what could have happened. In that case you must close that debug session before a new build.
Create a new project and by default it will create a main.cpp file in the project. Try to build the project and see if you face errors. If you don't see any error, replace the main.cpp contents with your code and build the project again. It did not result in runtime error for me and a console opened when I ran your program.
Hi I'm a highschool student in need of help. I am interested in C++ as I wish to become a programmer one day. I started using Visual Studio for C++ and I'm running a few simple lines of code. But everytime I press (Ctrl + F5) or Local Windows Debugger it shows my line of code but it closes out immediately after it runs, making it near impossible to read. The code is the classic "Hello World!" code. Maybe it has to do something with return 0;?
EDIT: Here's my code.
#include <iostream>
using namespace std;
int main()
{
int mark = 90;
if (mark < 50) {
cout << "HES OLD" << endl;
}
else {
cout << "Hes not old!" << endl;
}
}
When making console applications, I use the following lines at the end of my main function:
std::cout << "\nPaused. Press Enter to continue.\n";
std::cin.ignore(100000, '\n');
The idea is to display a prompt and then wait for the Enter key to be pressed.
I am trying to learn basic C++ after being a Java developer. So I decided to give CLion a try. I wrote this basic code just to familiarize myself with some C++ syntax.
#include <iostream>
using namespace std;
int main() {
string word;
cout << "Enter a word to reverse characters: " << endl;
getline(cin, word);
for(int i = word.length(); i != -1; i--) {
cout << word[i];
}
return 0;
}
The code is functional. It reverses whatever word you input. I wanted to step through it to see variables and what not, and to test out CLion's debugger.
My problem occurs when I get to
getline(cin, word);
When I step onto this line, I enter a word and hit enter. Then step over. After I do this nothing happens; all the step over, in, etc. buttons are disabled. I am unable to continue through the loop, or run the remainder of the code.
I have used Eclipse's debugger many times for Java development without any issues. Any ideas could be helpful.
TL;DR How do I step through a C++ command line program with basic input and output using CLion?
I've replicated the problem - looks to me like when debugging the newline is being swallowed by the IDE and not passed back to the program. I've submitted a bug to JetBrains. I don't see a way to work around this aside from getting out of the IDE and debugging directly with GDB or another IDE.
UPDATE: This issue was fixed in the Clion EAP Build 140.1221.2. It even made the first change listed in the release notes:
The most valuable changes are:
Debugger doesn’t hang on ‘cin >>’ operator any more.
Looking at your code, if everything is correct, you need to add #include <string>.
When I run this, it compiles and completes the output.
#include <iostream>
#include <string>
int main() {
std::string word;
std::cout << "Enter a word to reverse chars: ";
std::getline(std::cin, word); //Hello
for (int i = word.length() - 1; i != -1; i--) {
//Without - 1 " olleh"
//With - 1 "olleh"
std::cout << word[i];
}
std::cout << std::endl;
system("pause");
return 0;
}
Use the following code. I have modified your code to make it workable for your purpose. :)
#include <iostream>
#include <string>
using namespace std;
int main() {
string word;
cout << "Enter a word to reverse characters: " << endl;
getline(cin, word);
for(int i = word.length() - 1; i != -1; i--) {
cout << word[i];
}
printf("\n");
system("pause");
return 0;
}
I must write some C++ on Ubuntu Linux. After many searches I found Qt Creator IDE and G++ compiler. So, the problem is: how to run a C++ program and show console black window? For example, how can I get result from this code :
#include <QtCore/QCoreApplication>
#include <iostream>
using namespace std;
int main()
{
cout << "Hello";
int a[5];
cout << "Enter 5 numbers";
for(int i=0;i<5;i++)
cin >> a[i];
for(int i=0;i<5;i++)
cout << a[i] << " ";
}
I am very in a hurry and I will be very thankful for your answer.
I changed code to this:
#include <QtCore/QCoreApplication>
#include <iostream>
using namespace std;
int main()
{
// QCoreApplication a(argc, argv);
cout<<"Hello";
int a[5];
cout<<"Enter 5 numbers";
for(int i=0;i<5;i++)
cin>>a[i];
for(int i=0;i<5;i++)
cout << a[i] << " " << flush;
// return a.exec();
}
but still isn't working. I enter each number in one line, is it correct? How can I enter data for array in true way?
Well... there is the play button that will run your application. On lowest-right part of QTCreator, there is a tab "3 application output" that will show you the output.
If you want to run the application in a console without QtCreator, go to the folder where you have your source code. There should be the executable that you can run with
./excutable_name
There is a checkbox "Run in terminal" in the run configurations (left rim, "Projects" tab, "Run", "Run Configuration"). If you check that you get an external terminal opened, by default xterm. You can configure another terminal in the lobal setting ("Tools", "Options", "Environment", "General", "System")