I am new to C++, and now learning it using code blocks (version: codeblocks-16.01mingw-setup.exe). My test codes are as follows:
#include<iostream>
#include<stdlib.h>
int main()
{
int sum = 0, val = 1;
// keep executing the until val is greater than 10
while (val <=10 ) {
sum += val; // short-cut assignment
++val; // add 1 to val
}
std::cout << "Sum of 1 to 10 inclusive is "
<< sum << std::endl;
system("pause");
return 0;
}
These codes are written in an empty file named ex1.cpp. Then I tested by click "Build and run". As a result, another file main.cpp (I did not write this) pops up:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
Screenshot attached for your better checking:
The reason why you are getting this error is because your compiler settings is not correct.You need to make sure that you use GNU GCC MinGW Compailer.Go To Settings-->Compiler and make sure every thing is same as on the screen shot.
Solving common codeblocks problems :Link
I really did something wrong about coding:
when I create an empty file in the project, it will result in two main functions in the that project one of which is that "hello world" file automatically generated, which is not allowed by C++.
To build it successfully, what I did is to overwrite the codes in the main.cpp.
Related
I'm slowly moving from using Python to using C++ and I don't understand how to run any code. I'm using the g++ compiler, but I get no results from my functions.
// arrays example
#include <iostream>
using namespace std;
int foo [] = {16, 2, 77, 40, 12071};
int n, result=0;
int main ()
{
for ( n=0 ; n<5 ; ++n )
{
result += foo[n];
}
cout << result;
return 0;
}
If I run this example inside VSCode and specify that I want to use g++ compiler it comes back with: Terminal will be reused by tasks, press any key to close it.. If I compile it through cmd and run the task, a new cmd window flashes and nothing is happening.
I found the g++ doc which says how to compile with g++ and it shows the following example:
#include <stdio.h>
void main (){
printf("Hello World\n");
}
But I can't even run the compiler because it says
error: '::main' must return 'int'
void main(){
^
How can I print something in cmd or the ide terminal? I don't understand.
I believe you are using VSCode in a wrong way. You must know that it does not have integrated compiler by default but you need to compile source file in command line and run the executable:
$ g++ hello.cpp
$ ./a.out
Your first example runs with no problem. Check here
Your second example has an error because there is no void main() in C++. Instead, you need to have
int main() {
return 0;
}
UPDATE
If running the executable results in opening and closing the window you can fix that by using one of the following:
shortcut
#include <iostream>
using namespace std;
int main() {
system("pause");
return 0;
}
preferred
#include <iostream>
using namespace std;
int main() {
do {
cout << '\n' << "Press the Enter key to continue.";
} while (cin.get() != '\n');
return 0;
}
Why std::endl is not needed?
Some of the comments are suggesting that changing
cout << result;
to
cout << result << endl;
will fix the issue but, in this case, when the above line is the last line in the main function it really does not matter since program's exit flushes all the buffers currently in use (in this case std::cout).
Just started a beginner c++ tutorial which eclipse is used. I am using a mac, so when creating a new project i chose the toolchain 'macxxx" something. In the video a different one is used because it is done in windows.
I tried running this super easy program :
#include <iostream>
using namespace std;
int main() {
cout <<'C++ is FUN'\n;
return 0;
}
When i click on the hammer to build it, I just get 3 errors :
Symbol 'cout' could not be resolved firstprogram.cpp Semantic Error
all similar to that.
How can i fix that ?
Does following code produce errors aswell?
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!";
}
You can end current line with << endl after "Some text to display". So it would look like
cout << "hello there " << endl;
I was writing a code for exception handling on Visual C++ 2010 .Here is the code
#include <iostream>
using namespace std;
// Localize a try/catch to a function.
void Xhandler(int test)
{
try{
if(test) throw test;
}
catch(int i) {
cout << "Caught Exception #: " << i << '\n';
}
}
int main()
{
cout << "Start\n";
Xhandler(1);
Xhandler(2);
Xhandler(0);
Xhandler(3);
cout << "End";
return 0;
}
The Program executed properly and the output was the as expected.But when I pressed the close button for closing the console then an error came that cmd has stopped working
.Then I ran my previous code that executed properly ,they also gave the same error
.
Can anybody tell why it is happening?Is it a problem with the Visual c++ 2010 or the code
I think your problem is not with your code. The problem is within your compiler tool chain. You probably are using Qt, and the tool chain has a problem causing this. Google the message you get when you crash with your IDE.
Here's a simple experiment to prove what I'm saying: just run this code:
int main()
{
cout << "Start\n";
cout << "End";
return 0;
}
And your program will crash, which means you have no problems with exceptions or anything else in your code, but with your tool chain.
After I had some problems with a c++ program in Eclipse Juno I decided to start it over and I've created a HelloWorld program.It builds but when I press the Run button nothing happens.The odd thing is that I created a few weeks ago a small C application that worked like it should and my next task is to create a c++ application but if the simple HW isn't working I don't think that something bigger will work.Any suggestions of what should I do?
This is the HW code:
#include <iostream>
using namespace std;
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
Paste this code . If it doesnt work, try removing the namespace to run[dnt forget to recomplie]
#include <iostream.h>
using namespace std;
main()
{
cout << "Hello World!";
getchar();
return 0;
}
Solved it.I needed to append the path to the builder
System-Advanced system settings->hardware->environment variables and in the first table I created the new path and in the bottom table i've appended the same path to the Path variable.
#include <iostream>
using namespace std;
int main() {
bool x = true;
bool y = false;
if(x) {
cout << "if works";
}
if(y==false) {
cout << "else works";
}
int z;
cin >> z;
}
This is a small code that I compiled using Microsoft Visual C++ 2010 Express Edition. When I compile I get a message saying that Your project is out of date.
Why do I get this message ?
If the above code is really out of date, I will be thankful to any suggestion?
You probably hit F5. Which is: run in debug, NOT compile (at least not in C++/VS terms).
It detects that your code is different from the one used to compile your binaries.
If you have this code:
int main()
{
cout << "test";
return 0;
}
And you compile, that's version 1 of your exe.
Then you change the code in
int main()
{
cout << "test1";
cout << "test2";
return 0;
}
Now you hit F5, you are essentially still trying to debug version 1 of your exe since you have not compiled version 2 of your source code into version 2 of your assembly.
That's why it gives you the warning. If I recall correctly you can set a checkbox on that popup to always rebuild. (not sure!)
Projects are out of date dialog will pop up when the time stamp of input files(source code) are newer than output files(binaries).
It has nothing to do with the source code being out of date(If you are thinking in that direction).
Delete all the Debug folder, recompile and then run the project and it should work.