In C++ when I try to use variables with cout it ill cive me an error pop up. I use eclipse as IDE and minGW as compiler. This is the code:
#include <iostream>
using namespace std;
int main() {
int num = 4;
cout << num << endl;
return 0;
}
between the cout and num and num and endl; I got those to diamond breckets pointing left but they won't show here.
can anone please help me?
edit:
the error that pops up is(translated from dutch to english):
file.exe doesn't work anymore
A problem occurd causing the program to malfunction. The program will be closed and a notification wil be shown when there is a solution available.
rebuild your project : probably you 're running old version of your project
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.
I want to be able to delete a file using c++ remove functions but gets does not work in visual studio 2019 and using fget and gets_s ,the program runs but everytime it shows message that file not deleted as i proggrammed, The question is when it will delete .I tried every thing but it doesn't work.
There would be solutions but i am a student so plz consider my problem.Here is my code :
#include <iostream>
#include <conio.h>
#include <stdio.h>
int main()
{
int status;
char file_name[59];
cout << "enter name of file to delete:\n ";
gets(file_name);
status = remove(file_name);
if (remove(file_name) == 0) //i wrote if(file_name==0); too but it doesn't work
cout << "file deleted";
else
cout << "file not deleted";
}
I was looking for answers in stack over flow other similar questions but they were too professional to be understand by me plz tell me simple way to delete a file that user wants to delete.
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.
Im just starting with C++ and I wanted to try running this test... When I try compiling the code it just gives an error saying (lldb):
#include <iostream>
using namespace std;
//---------My Function--------//`
int addNums(int x, int y){
int answer = x + y;
return answer;
}
int main(int argc, const char * argv[]){
// insert code here...
/*
int num;
cin >> num;
cout << num;
cout << "\n";
char hm[] = "eef";
cout << hm[2] << endl;
*/
cout << addNums(1, 2);
return 0;
}
If by "Build" you mean you clicked the Play button - the leftmost button in the toolbar - then this is going to build and run your code. The build probably went fine. You can check that the build went okay by switching to the Reports navigator (the one with the speech bubble icon) and click on the latest Build report... If you want to build without running, Cmd-B is what you want to do.
Anyway, if you asked to build & run, and the build went okay, Xcode will start your program in the debugger, which will switch to the Debugger UI. Since your program just prints something and exits, the Debugger should have just printed "3" in the Debugger Console and exited. Not sure why you are seeing an lldb prompt, that is not what I see.
You might try setting a breakpoint, and see what the debugger looks like when you hit the breakpoint.
#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.