Eclipse Hello world fail on mac , toolchain? - c++

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;

Related

How to display stuff in VS Code debug console without `std::endl`?

When debugging an app using std::cout to print something, nothing appears in the debug console (3rd tab, not external console). One of the exceptions seems to be the use of std::endl:
#include <iostream>
#include <Windows.h>
using namespace std;
int main() {
while(true) {
Sleep(500);
std::cout << "Hello world!" << std::endl; // Works
}
}
#include <iostream>
#include <Windows.h>
using namespace std;
int main() {
while(true) {
Sleep(500);
std::cout << "Hello world!\n"; // Doesn't work
// std::cout << "Hello world!"; // Doesn't work
// std::cout << "Hello world!" << std::flush; // Doesn't work
}
}
In the first example our lines appear over time, while on the second doesn't appear at all (or the console refreshes somewhere around 60 secs). I think this is a bug but I'm not sure, so what is a workaround? Maybe I have to configure something instead? I found this problem while working on something else: C++: cannot see output in VS Code while debugging
Edit
The problem is what to do when u wanna output something without a new line

C++ Beginner. cout not outputting string into console?

I am following along with Bjarne Stroustrup's book "Programming: Principles and practice using C++", and I am running into a problem that I am struggling to solve (I've spent an hour+ trying to figure it out on my own with 0 progress).
He suggests using his custom header file, which I have copy and pasted into a new header file:
http://stroustrup.com/Programming/std_lib_facilities.h
Now, I am to write a simple hello_world program that outputs a "Hello, world." string in the console.
However, when I run the program, all it says is:
"Press any key to continue..."
With no other strings output.
This is the program (my code is identical to the books):
#include "../../std_lib_facilities.h"
int main() {
cout << "Hello, world!\n";
keep_window_open();
return 0;
}
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, world!" << endl;
keep_window_open();
return 0;
}

What to do to see the output of "cout" command?

I am starting with C++ (Visual Studio 2015 and Windows 8.1), with this simple code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world" << endl;
return 0;
}
But, the output screen shows nothing!, what shall I do?
Thanks in advance.
In Visual Studio, start the program with Ctrl-F5 and it will run and pause automagically for you. No additional code needed.
Your code is perfectly fine but the program currently only prints and exits right after, because this can happen very fast you might not be able to even see it,try pausing it :
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world" << endl;
cin.get();
return 0;
}
Also, make sure your Anti Virus isn't blocking Visual Studio.
Your code is just fine, however, if you execute it as a cmd program, the program window will close immediately, you might not be able to even see the output. You can write extra code to solve this problem by "pausing" the program:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
cout << "Hello world" << endl;
system("PAUSE");
return 0;
}
if you don't like include a windows.h file every time you type, you can add a "cin.get();" in the end of the code. But to be honest, since you are just a beginner, the coolest way I think you should try, is not to use Visual Studio to learn C/C++ but to install CodeBlocks(a simple but effective IDE) to write some codes that are not so long. You know, VS is for huge and complex projects and some practical program developing.
Another solution, platform dependent. My answer is for those of you who just need test pause for debugging purposes. It's not recommended release solution!
windows
#include <iostream>
int main()
{
std::cout << "Hello world" << endl;
system("pause");
return 0;
}
linux (and many alternatives)
#include <iostream>
int main()
{
std::cout << "Hello world" << endl;
system("read -rsp $'Press enter to continue...\n'");
return 0;
}
Detecting paltform
I used to do this on programming homework assignments, ensuring this only happens on windows:
#include <iostream>
int main()
{
std::cout << "Hello world" << endl;
#ifdef _WIN32
system("pause");
return 0;
}
Here's a good cheatsheet for ifdef macros and operating systems: http://sourceforge.net/p/predef/wiki/OperatingSystems/
The program exits on return 0; and window closes. Before this, you must pause the program. E.g you can wait for an input.
Here is a snippet from my code to do this. It works in both windows and linux.
#include <iostream>
using std::cout;
using std::cin;
// Clear and pause methods
#ifdef _WIN32
// For windows
void waitForAnyKey() {
system("pause");
}
#elif __linux__
// For linux
void waitForAnyKey() {
cout << "Press any key to continue...";
system("read -s -N 1"); // Continues when pressed a key like windows
}
#endif
int main() {
cout << "Hello World!\n";
waitForAnyKey();
return 0;
}

when i use the c++ string in my code it compiles but i don't show an output

I don't know whether I have installed cygwin wrong or what but the code compiles fine but it doesn't show the output. Here's my code:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string name;
cout << "Enter the name of the person" << endl;
getline(cin, name);
cout << "Name is: " << name << endl;
return 0;
}
Here's the compilation image and the execution:
Your code is fine. The correct way to call the executable using Cygwin is ./string.exe
As I can see, you use cygwin. I had the same issue with gcc 5.2. Try to install gcc 4.9 and all corresponding libraries for this version instead.

Eclipse C/C++ HelloWorld

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.