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;
}
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 don't know how to explain it, but I will try.
So I write C++ code and I run it with code runner. But the program closes itself. I don't want to use system("PAUSE"); because I need to remove it if I'm in a competition.
So is there another way?
Ok, I tried something that came to my mind and it works.
"code-runner.executorMap": {
"cpp": "g++ $fullFileName -o $fileNameWithoutExt.exe && start cmd /k $fileNameWithoutExt.exe"
}
I used this in the settings.json
You can use Dev C++ as an alternative IDE. But if you wish to do it in Visual Studio without a system("PAUSE"); you can use cin.get(); or cin.ignore(); as an alternative.
Code:
#include<iostream>
using namespace std;
int main()
{
cout << "Hello World";
cin.ignore();
}
or
#include<iostream>
using namespace std;
int main()
{
cout << "Hello World";
cin.get();
}
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 have a C++ program which uses sys.argv for loop counters and an input file to initialize std::vector's. Can I directly input those at compilation with g++?
I would also be happy with something that uses bash to do this. I am not proficient enough in either C++ or bash to figure this out myself.
Thank you in advance for answers.
C++ is compiled before it is executed. That means that the compiler won't have a list of arguments to give to the program.
Consider the scenario where you build the C++ program on one computer but then distribute the C++ program to another computer. The second computer doesn't have, or need, a compiler. How do you get your program arguments then? Consider this example in Python:
import sys
for arg in sys.argv:
print(arg)
In C++20, that might be:
#include <iostream> // std::cout
#include <cstdlib> // EXIT_SUCCESS
int main(int argc, char **argv) {
for (int i = 0; i < argc; ++i) {
std::cout << argv[i] << std::endl;
}
return EXIT_SUCCESS;
}
If you know the program arguments for the second computer when you compiled it into the first computer then you can provide it as a static variable. Consider these examples:
my_args = ["hello", "world!"]
for arg in my_args:
print(arg)
In C++20, that might be:
#include <iostream> // std::cout
#include <cstdlib> // EXIT_SUCCESS
#include <vector> // std::vector
std::vector<const char *> my_args{"hello", "world!"};
int main(int, char **) {
for (const char * arg : my_args) {
std::cout << arg << std::endl;
}
return EXIT_SUCCESS;
}
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 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.
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 6 years ago.
Improve this question
So I am a beginner and for some reason this still give me the undeclared identifier error and the system cannot find specified file errro I would be grateful if someone would answer me this
#include <iostream>
#include "stdafx.h"
using namespace std;
int main()
{
cout << "hello world" << endl;
return 0;
}
When using visual studio, this line #include "stdafx.h" needs to be at the top of your code when using pre-compiled headers.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
cout << "hello world" << endl;
return 0;
}