Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
The following code is not working in DEV C++:
#include <iostream>
using namespace std;
void main()
{
cout << "Hello world";
}
How can I do this?
Also, tell me if I can get almost all the functionality of Turbo C++ in Dev C++. Also, tell me if I can easily switch to DEV C++ if I know a moderate amount in Turbo C++. I just want to use C++ with Python 3.x for console applications including GUI.
From what I see in your initial question (printf works while ostream doesn't) the problem may be in buffering. Try that:
std::cout << "Hello world" << std::endl;
or, if you wish to avoid a newline:
std::cout << "Hello world";
std::cout.flush();
There is a difference between streams and printf: the streams are not outputing the data immediately but buffer that to optimize the performance. This however means that in some environments for the small outputs like yours it may stuck forever waiting for more output.
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello world";
return 0;
}
I used int main instead of void main.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I've encountered an error with the hello world program in c++ vscode, it doesn't print out Hello World also warns me the i don't have gcc
#include <iostream.h>
using namespace stl;
integer main()
{
output << "Hello World"
return 0;
}
The source code won't compile, here is a working hello world program:
#include <iostream>
int main()
{
std::cout << "Hello World\n";
return 0;
}
In addition to this, please make sure you have a compiler.
Looks like u don't have a compiler to compile the code, consider installing gcc or any other c++ compiler first. Your source code also seems to have some syntax errors, try this hello world program:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!\n";
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I just started learning C++, and I found a code that doesn't work. It shows me red lines on "std_lib_facilities.h" and on cout.
I found this code in this book "Programming: Principles and Practice Using C++" by Bjarne Stroustrup.
do you recommend another book I have some experience with python and I want to move to c++
thank you
// This program outputs the message “Hello, World!” to the monitor
#include "std_lib_facilities.h"
int main() // C++ programs start by executing the function main
{
cout << "Hello, World!\n"; // output “Hello, World!”
return 0;
}
You need to include <iostream> to use cout:
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
You need to #include <iostream> and a tip is to also type using namespace std; so you can use cin without typing std::cin
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm using a Mac and I'm learning C++. I'm trying to open and read a file and I wrote a few lines code to do it. But when I compile it, either with gcc or g++ or clang, the compiler fails because it can't find the istream file. Is there a way to install or download the default library again. Thank you
Edit:
This is the code:
#include <ifstream>
using namespace std;
int main()
{
ifstream ligand("ligand_dataset.txt");
if( ligand.is_open())
{
cout << "File ok";
}
else cout << "Unable to open the file";
return 0;
}
The ifstream header is replaced with fstream
#include <fstream>
int main()
{
std::ifstream f(....);
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am getting the error "Error: 'cout' was not declared in this scope," but I included <iostream>, which was the solution that was given for similar problems in my research. My code is this:
#include <iostream>
int main(){
Sally so;
Cout << "omg wtf is this on my shoe" << endl;
}
Its cout not Cout, notice the case difference.
cout is in the namespace std. In order to use it you need to resolve the namespace with std::, so use std::cout << ....
As much as people will tell you to just do using namespace std, dont. For more info, see Why is “using namespace std” considered bad practice?.
You are writing "Cout" not "cout" - C++ is case sensitive, so those two are not the same thing.
You should write std::cout since the cout stream lives in the std namespace.
The same goes for endl which should be std::endl.
You could avoid writing std:: by using using namespace std; but I wouldn't advice it - it pulls all of the namespace into the current scope which may not hurt for a trivial program will bite for a more complex one (at the very least, don't do it in headers).
Just do this:
#include <iostream>
int main(){
Sally so;
std::cout << "omg wtf is this on my shoe" << std::endl;
}
Btw; unless you know you want to flush the stream, prefer '\n' over std::endl.
Change your code to this:
#include<iostream>
using namespace std;
int main()
{
Sally so;
cout<<"the text"<<endl;
}
Hope it helps!!
Cheers
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I've been working through the "Programming: Principles and Practice using C++" book, and this example was meant to illustrate how data can be lost in type conversions. But when I tried to execute it, it keeps telling me 'Main' : must return a value. I tried using "return 0;" after the curly brace for the while loop, but then it gave errors around "Unresolved externals".
I've copied the code from the book to Visual Studio and everything else I've copied has worked as expected - would anyone be able to tell me how I could fix this please? I don't know why it's happening with this specific example or how to make it stop asking for a return value.
Sorry if this is dumb, I'm very inexperienced and I've tried googling for an answer, but suggested solutions like "return 0" don't work :)!
#include "std_lib_facilities.h"
int main()
{
double d = 0;
while (cin >> d)
{
int i = d;
char c = i;
int i2 = c;
cout << "d==" << d
<< " i==" << i
<< " i2==" << i2
<< " char(" << c << ")\n";
}
}
I tried using "return 0;" after the curly brace for the while loop, but then it gave errors around "Unresolved externals".
That's a separate, unrelated problem uncovered by fixing the first one. Add the return, then deal with the unresolved externals error.
If that's your whole program, you're unresolved external is most likely iostream. You need to include that and use the correct namespace.
Consider the following code:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
return 0;
}
Better yet, forgo the using statement and use std::cout so that you don't have to worry about namespace collision. See this question Why is “using namespace std;” considered bad practice?
#include <iostream>
int main() {
std::cout << "Hello World!" << std::endl;
return 0;
}