During debugging Eclipse doesn't "see" INPUT from built-in console, just ignores it. Simple example:
#include <iostream>
using namespace std;
int main() {
int a;
cin >> a;
cout << a << endl;
cin >> a;
cout << a << endl;
return 0;
}
works perfectly fine when just run, but when I try to debug it, first "data on input"(?) is always a number around 40, and only zeros next, no matter what i will write to console.
So program executes, first variable is set to ~40 and all next to zeros.
Output works fine, values are written to console, only input doesn't work.
I work on Windows 10 and use MinGW.
Thanks in advance.
#EDIT
Everything works, when I use native Windows console
(.gdbinit file with set new-console on line)
Related
I'm having a problem specifically with VS code where cout and cin are not working in c++ as expected.
The program is somehow terminating after the first cout and the debugger shows "paused on exception" and then "segmentation fault".
Since the same program is running perfectly in Apache Netbeans and other online compilers, I don't think cin.ignore() or endl or anything like that is required.
C programs run perfectly fine though (in VS).
#include <iostream>
using namespace std;
int main() {
int l,b;
cout << "\n\nENTER L and B : ";
cin >> l >> b;
cout << "L and B entered are : " << l << "\t" << b;
}
code + output image
Here is the debugger screen image
Even simple one variable cin statement isn't running and the program is terminating automatically. cin ain't executing
cout with any variable or constant ain't running correctly either.
simple cout image
My guess is something's wrong with memory allocation by VS code.
There seems to ba a problem with your Compiler installation.
Consider checking the compiler installation steps from the VS code documentation.
Here is my c++ code to read an integer, double it and then print it out on the screen:
#include
<iostream>
int doubleNumber(int x) { return 2 * x; } int main() { using namespace std; int x; cin >> x; cout
<< doubleNumber(x) << endl; return 0; }
I am compiling it using:
g++ -o example9 example9.cpp
It seems it is fine and it creates the object file but it is impossible to run the file using the following command:
./example9
In fact it does nothing (not even an error message)
What I am doing incorrectly?
Your help is appreciated.
When you say it is "impossible to run the file", I expect you are seeing something like the following:
$ ./example9
_
where I have indicated the cursor position with _ (just sitting there, on the next line, not doing anything except blinking). In this case, your program is running fine. It is waiting for you to type a number (the cin >> x statement). Type a number and press Enter.
#include <iostream>
using namespace std;
int main()
{
int x = 42;
cout << x; // This line doesn't print! Why?
return 0;
}
Screenshot of Visual C++: http://bildr.no/image/ZlVBV0k0.jpeg
This code gives me nothing but a black console window that flashes by when I click on debug. Isn't the number 42 supposed to be printed in the console window? This is my first application in C++. I have experience in C# from high school.
EDIT:
Now I have tried this code:
// Primtallsgenerator.cpp : Defines the entry point for the console application.
//
#include <iostream>
using namespace std;
int main()
{
int x = 42;
cout << x << endl; // This line doesn't print! Why?
cin >> x;
return 0;
}
It still doesn't work. Screenshot of the code here: http://bildr.no/image/ODNRc3lG.jpeg
The black windows still just flashes by...
Two things to note:
First, you are not forcing the buffer to flush, so there is no guarantee the output is being sent to the screen before the program ends. Change your cout statement to:
cout << x << endl;
Second, Visual Studio will close the console when it ends (in Debugging mode). If you do not debug it (Ctrl-F5 by default), it will keep the console open until you press a key. This will allow you to see the output. Alternatively, you can add a cin.get() before your return statement which will force the program to wait for a character to be in the input stream before the program is allowed to exit.
It did print the message, it was just too fast for you to see.
add this command:
cin >> x;
or this one
while(true) {}
before the return statement.
Yes, it will print the number. Then the program ends, and the console window is closed. Run it in the debugger, and put a breakpoint on the return 0; line. Then you'll see it.
This code should work fine:
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int x = 42;
cout << x;
getchar();
return 0;
}
Also check this documentation about getchar().
I recommend to use system pause at the end before the "return 0" statement like this:
system("PAUSE");
This is cleaner and much more effective.
If you are working with a console application in Visual Studio, you have to go to your project's linker properties and set your SubSystem setting to CONSOLE.
And get a habit of running your code without a debugger (Ctrl+F5 by default) when you don't need the debugger. That way the console window will not flash and disappear by itself.
I'm having this weird problem. My code is simple:
#include <iostream>
using namespace std;
int main() {
int num;
cout << "number: ";
cin >> num;
for (int i=0;num>i;i++) {
cout << i <<"\n";
}
system ("Pause");
return 0;
}
If the input for example is 1000, the output contains numbers from 701-999.
Any idea?
I'm using Dev-C++ IDE on Parallels.
Actually it prints all of them, from 0 to 999, but your console's buffer is not large enough. So you see only the last part. if you print into a file, not the console, you'll see :)
The loop ends when num>i is no longer true. This occurs when i is 1000, so the last loop executed will be with value 999. As for not seeing lower than 701, maybe your screen buffer is too small.
It will start with 0-999. Also, it appears to you that it starts with 701 because of your console screen settings. If you want to see it for yourself, change the newline into a space:
cout << i <<" ";
Did 0-700 scroll off the screen? Run your exe like this
your_program > out.txt
Then look at out.txt in an editor.
Works absolutely fine for me. I'd suggest your IDE might be playing tricks on you. Could you redirect output into a file and check that?
Regarding #JoshD answer,
You will need to:
for (int i=0;num>=i;i++) {
cout << i <<"\n";
}
I've searched around here first, and seen many solutions to making Visual C++ hold the console open at the end of program's execution, but I'm getting some weird behavior from one of the methods, and I don't understand where this behavior is coming from.
So (a) what's the best method (besides crtl-F5 and setting subsystem to console) to hold the console open?
and (b) why am I getting the strange behavior that I will now elaborate on?
Here is the code
/*
Learning how about variables and accepting inputs
*/
#include <iostream>
using namespace std; // cout and cin from the standard namespace.
// using std::cout; -- alterative declarations
// using std::cin;
int main()
{
char first, last;
cout << "Please enter your first and last names:\n";
cin >> first; // get one char
cin.ignore(256,' ');
cin >> last; // get one char
cout << "Your first and last initials are " << first << last;
cin.ignore('\n');
return 0;
}
The strange behavior is, I have to press the Enter exactly 4 times before it will exit the console.
Why 4 times?