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.
Related
This question already has answers here:
Uninitialized variable behaviour in C++
(4 answers)
Closed 2 years ago.
I'm a newbie in the programming world, and i've decided to start with C++ code a few days ago as my first programming language.
I just started to read an online course which i'm guiding on (and aply while i'm reading it).
The course in question assigns a serie of small optional exercises, wich go hand in hand with the topic that is being dealt at that moment.
One of this optional exercises is: "Create a program that multiplies two whole numbers in the following way: it will ask the user for a first whole number. If the number that you type is 0, it will write on the screen "The product of 0 by any number is 0". If a number other than zero has been entered, the user will be prompted for a second number and the product of both will be displayed."
How it says, I did my best to coding that program.
The code of what I did is:
#include <iostream>
using namespace std;
int main ()
{
int a;
int b;
int solve;
cout << "Enter a number: ";
cin >> a;
if (a!=0)
{
cout << "Enter another number: ";
cin >> b;
}
if (b!=0)
{
solve = a * b;
cout << "The result of your operation is: " << solve << endl;
}
else cout << "The product of 0 by any number is 0." << endl;
return 0;
}
So, I press F9 to compile, then F10 to run it.
I proceed to testing it.
I put and different number from zero, I put another one. Throws me a multiplication of both. Nice.
I put and different number from zero, I put another one that actually its zero. Throws me the message of "else" order. Nice.
**BUT
I put and number equal to zero, and throws me the message of the "cout" order of "if (b!=0)".**
I didn't really know what I had done wrong, so, I ask for help from a friend who has some more experience than me, and tells me that actually it's nothing wrong. In fact, he proved me sending to me a screen cap of his Dev C++ with my code in it, and how it runs just how it had to be.
Then, I opened an online compiler (https://www.onlinegdb.com/online_c++_compiler#) to get down my doubts, and yes, there runs correctly too.
So, here's my question?
What's the problem? Why that happens?
I have the DevC++ 5.11 TDM-GCC 4.9.2, and I'm using the deafult compiler.
Please, I would like some of help, I feel more comfortable compiling in PC than online, it's more quick.
Thank you anyway for reading until here.enter image description here
The reason why this happens, as #TrebledJ mentioned, is that you've not initialised the variable b. So you can get over this problem just by initialising b to any value of your choice (preferrably 1 or 0 for simplicity). But there's a workaround if you don't want to initialise the values. I would transform your code to something like this:
if (a!=0)
{
cout << "Enter another number: ";
cin >> b;
if (b!=0)
{
solve = a * b;
cout << "The result of your operation is: " << solve << endl;
}
else cout << "The product of 0 by any number is 0." << endl;
}
else cout << "The product of 0 by any number is 0." << endl;
Basically, what I'm doing is, if the value of a after input is 0, I'm not running the part for taking the user input of b.
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)
I am using g++ version 4:4.8.2-1ubuntu6 with Eclipse 3.8 on Linux Mint.
Following example from my C++ book does not work as expected:
//bondini.cpp -- using escape sequences
#include <iostream>
int main()
{
using namespace std;
cout << "\aOperation \"HyperHype\" is activated\n";
cout << "enter sercret code:________\b\b\b\b\b\b\b\b";
long code;
cin >> code;
cout << "\aYou entered: " << code << "...\n";
cout << "\aCode OK! Commencing Z3!\n";
return 0;
}
I get following result when running the program:
In Eclipse and directory I am using UTF-8 encoding. Why does not '\a' play sound as it should and '\b' does not move the cursor one space back, while '\n' works properly.
edit: As I understand it, it is the compiler that makes the mess of it. --> I was wrong, in terminal it works fine, but eclipse 'terminal' does not work.
Wherever you are sending your output. What the destination does with it is entirely in its own hands. So while eclipse might not support these special characters your terminal should.
Yes, I know this is a common question. No other question/answer sets meet my needs here. Taking c++ this quarter. Been going along just fine. Trying to do last assignment, and I'm getting the above error, even on a fresh project with:
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
cout << "hello world" << endl;
}
As an experiment, I tried pasting in much more elaborate code from previous assignments. This runs. Just pasting in -- not running the previous assignment.
I took the simplest of them, and stripped it down to this:
Main.cpp:
#include <iostream>
using namespace std;
int main()
{
double hours, rate;
cout << "Hello World";
cin >> hours;
cout << "Hello World";
cin >> rate;
system("pause");
return 0;
}
This compiles and runs fine. However, if I remove either of the cin lines I get the error stated in the title.
I'm just flabbergasted because pasted in code compiles and runs, but even "Hello World" in a fresh project won't.
Does my above example provide any clues?
Does it happen if you run it as administrator? Were you running it from Visual Studio or did you clicked the executable?
If you have an anti virus, it may be causing the error as well.
I wanted to start learning how to program, so I asked my math professor if he had a book that I could borrow. He did and so I have been reading a C++ book from ~1994 (it still has a floppy disk :P). Anyway, I made it to a point in it and it sets up a program that calculates y in y=mx+b. Pretty simple, but I decided to try it out and it is not working. I would really like to figure out why it is not working and fix it.
Here is the code for it:
#include <iostream>
using namespace std; //not in the book: added by me after some Googling
int main() {
cout << "Input m: " << flush;
int m;
cin >> m;
cout << "Y-intercept: " << flush;
int b;
cin >> b;
cout << "X coordinate of interest: " << flush;
int x;
cin >> x;
int y;
y = m * x + b;
cout << "y = " << y << "when m = " << m << "; " << "b = " << b << "; x = " << x << endl;
}
edit: Sorry. Forgot to describe what was going on. lol. The program executes properly until it it comes to displaying the final line. After submitting "X coordinate of interest: " the program simply exits. I mean I am no expert in C++, but should the final cout write to the console?
And I know it is really outdated, but I really just want a platform to stand on when I begin to look at the newage languages. The book itself is only about 700 pages, and there is a LOT of explaining in it, so it is not very much code wise. I have probably 10 to 20 700 page pdfs on Java and C#/C++/C all written within the past six years. So I'll be good. Just want a starting point. :) Plus this book explains a lot about how a computer works and certain jargon that some of the newer books just don't.
This is a common windows cmd problem. Either run the program through cmd, type in the executable name, or add getchar or cin >> variable to the end of the program.
Assuming this is in Visual Studio when you run a program with debugging (F5) the console instance is closed automatically. You can either add a input line to the end of the program as others have mentioned or run the program without debugging (Ctrl+F5) and the console window will pause and let you see the output at the end of the programs execution.