cin crashes C++ program - c++

I feel like this is a very rudimentary problem, but I can't seem to get cin working.
Using code as simple as this:
#include <iostream>
using namespace std;
int main ()
{
int i;
cout << "Enter a number: ";
cin >> i;
cout << i;
}
causes the program to crash. It compiles perfectly fine, but when I run it I get the program has stopped working dialog box. I'm using MinGW with the g++ commands to compile my code. I'm also running windows 8.1 if that has any effect whatsoever.

The code is perfectly fine. The issue is with the compiler. This generally happens because of the misconfiguration of linking files (DLL's). It is recommended that you uninstall the current installation and re-install the software by following the installation instructions. If you are looking for bundled alternatives then you can try Code::Blocks - http://www.codeblocks.org/.

Related

I can't write any text into the input box, my IDE is visual studio code

#include<iostream>
using namespace std;
int main() {
string name;
cout << "enter your name : ";
cin >> name;
cout << name;
}
Your error may not be in your code, but ensure you are compiling your program properly. When using visual studio code (assuming you are using the g++ compiler), go to your terminal and type in:
g++ yourprogramname.cpp
This will compile the file with g++, and will produce a file named "a.exe", or "a.out" depending on your operating system.
To run your program, type ".", and then the name of your executable:
.\yourprogrogramname.exe
This will run the program that you just created, allowing you to then input the name. If you are still having problems, try using a different terminal by clicking the dropdown menu right next to the "+" in the Visual Studio Code terminal, or running VSCode as an administrator.

VSCode cannot input data

I'm a noob in C++ and I have VSCode running in Windows11 , with the typical extensions for C++ and MinGW. I am running a little code in this IDE:
#include <iostream>
using namespace std;
int main (){
int num;
cout << "Number: " ;
cin >> num;
return 0;
}
It is something basic for this test, but when I run it in the output option I cannot enter anything, neither letters nor numbers. Nothing comes up in the terminal option either. The code executes without errors and VSCODE does not give any information.
Instead, if I run the .exe file from outside the editor it works perfectly.
I don't know if it's my editor configuration error or some other problem, in Ubuntu or MacOS this has never happened to me.
I appreciate the help in advance.
All the best.

Segmentation Fault in CPXopenCPLEX

I recently reinstalled a 32 bit version of CPLEX on a new Ubuntu 16.04. When I compile the following test program with g++-5.4, it crashes with a segfault.
#include "cplex.h"
#include <iostream>
using namespace std;
int main() {
int err;
cout << "CPX_VERSION: " << CPX_VERSION << endl << flush;
CPXENVptr env_ = CPXopenCPLEX( &err );
cout << "error: " << err << endl << flush;
if (env_) {
cout << "has env" << endl;
}
return 0;
}
I compiled this with the following call
g++ -m32 main.cpp -o main /path/to/cplex/lib/x86_linux/static_pic/libcplex.a -lpthread -I/path/to/cplex/include/ilcplex/
The output is
CPX_VERSION: 12060300
[1] 21609 segmentation fault (core dumped) ./main
Any suggestions of how to fix or debug this are welcome.
Edit:
The 64bit version works (without the compiler flag -m32 and using x86-64_linux instead of x86_linux in the path to the library, of course). But this doesn't solve my problem, as I need the 32bit version.
Edit2: I was able to get a working version on a different computer that also runs on my computer (I think this is a reason to exclude shared libraries as a possible source). I compared the strace output of both binaries and they were almost identical up until the error. The differences were the specific pointer values and the order of lines in the output (probably just an issue with the output buffer and not a real difference).
The working version does two calls to time(NULL) at the place were the other version segfaults. Since this is the first call to time in the output, this might be the issue.
I asked this question in the IBM forums and the end result seems to be that this is probably an incompatibility of an old CPLEX version and a new 32-bit binutils version. Since IBM stopped supporting 32-bit builds in the next CPLEX version, there is next to no chance that this bug will ever be fixed.

No output in c++-program if object is instantiated using Eclipse

I got an issue after installing Eclipse Kepler on my Windows 7 32bit machine. I installed the CDT and the MinGW-compiler. I configured the installation by adding MinGW to the PATH and tested my configuration with a "Hello world"-program, which worked.
The strange thing is, that if I instantiate an object, nothing is outputted. It doesn't matter if it's a std::string or a custom made class. If I instantiate it, nothing is outputted, even if it should be outputted before the instantiation. The exact same code works fine, if I compile it with cygwin gcc from command line. If I change the toolchain to cygwin gcc nothing changes (I've rebuild the program with "build all").
There is no error displayed and no problem listed.
Here's the minimal working example:
#include <iostream>
#include <string>
using namespace std;
class SayWorld{
public:
SayWorld(){
cout << "World!" << endl;
}
};
int main() {
//Only gets outputted, if the lines, that don't work are commented out:
cout << "Hello ";
// Works:
cout << "World!" << endl;
// Doesn't work:
// SayWorld sw;
// Also doesn't work:
// string str("World!");
// cout << str << endl;
return 0;
}
Edit 2:
I narrowed the error to MinGW, as this picture of a Cygwin-Bash-Terminal demonstrates. (The file was not changed beetween the to g++ calls and contains the example above)
Edit 1 (Legacy)
Toolchain-picture:
-picture removed- (don't think it was necessary)
After reinstalling eclipse and MinGW again, step-by-step following this video tutorial:
http://www.youtube.com/watch?v=77xZOT3xer4
and having the same problem afterwards, I stumbled uppon this post in the eclipse forum:
http://www.eclipse.org/forums/index.php/u/104305/
which has brought me to this solution:
Right-click Project -> Properties -> Run/Debug Settings
Choose executable and hit the "New"-button.
Go to the Environment-Tab and create a new variable named PATH with the value: "C:\MinGW\bin".
As I am no expert, I can't explain to you why it works, but it worked for me. If someone knows, how to do this better or wholly avoid this problem, I'd be glad to listen.
This entry was definitively in my Windows-PATH...
PS.: The problem seems also to be known here:
unable to see the output of c++ program in the console

Codeblocks outputs broken executable

I have downloaded plenty of different versions of code blocks, and none of them compiles quite right. My hello world runs within code blocks just fine. However, when I run the executable outside of codeblocks, it says "Hello.exe has stopped working". There isn't anything wrong with my code (I don't think.) and my mingw compiles fine outside of codeblocks. What does codeblocks do to my executable? Is there some option to fix this? I am on windows 7 64 bit, and my current code blocks version is 10.05. My program:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
cin.ignore();
return 0;
}
I solved the problem. I had a broken compiler (or something like that). My suggestion for other people with this problem is to experiment with different versions of the minGW compiler. Also, change the version of code blocks you are using, or even uninstall everything and restart. The problem with mine was I downloaded a bad compiler. [The truth is, codeblocks isn't the best ide.]