Previously, I have been interested in learning C++ so I decided to go for "InfiniteSkills" training video (http://www.infiniteskills.com/training/learning-c-plus-plus.html)
The instructor start by teaching "Hello World" as a basic as always.
Here is the code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!";
return 0;
}
but after I build it using CodeBlocks it won't compile
I have also tried using Sublime text too, but the result seems to be the same
Any suggestion?
Image:
you should add a newline character to the end of the line you want to print. Probably you are not seeing your output because it is still in the buffer. As #Quirliom noted: It may not be the stdio buffer but Sublime buffering until new lines...
cout << "Hello, World!\n";
or
cout << "Hello, World!" << endl;
As per the comments, you are unable to see the output. Try this:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!";
cin.get(); // This waits for you to input something and allows you to see the input.
return 0;
}
I don't know the real solution for this problem. But my guess is because of the complier. I have test with CodeBlocks and Sublime Text 3 on mac both won't print "Hello World" for me. So I decided to test with another which is "Xcode" and it works! I don't know what the real problem is but if anyone have any problem like me you may want to try using another complier :)
Thank you everyone for your suggestion and happy coding!!!!
You should both add a newline command in your print function and some sort of pause.
#include <iostream>
using namespace std;
int main(){
cout << "Hello World!\n" //calls for a newline
cin.get(); //pauses until a key is pressed
return 0;
}
Try this and see if it works
I had this problem too but I was able to fix it by reinstalling the C++ plugin for VS Code. I think the iostream wasn't actually there originally.
Related
I have been writing code like this to display cyrillic text on the console and it has always worked, but it suddenly stopped working for some reason, I don't understand why. What's the problem?
This is on Visual Studio, Windows
#include <iostream>
#include <fstream>
#include <string>
#include <Windows.h>
using namespace std;
int main()
{
setlocale(LC_ALL, "Russian");
//SetConsoleCP(1251);
//SetConsoleOutputCP(1251);
ifstream input{ "in_text.txt" };
if (!input) {
cerr << "Error opening file" << endl;
return 1;
}
cout << "Displaying file contents: " << "\n\n";
string line{};
while (getline(input, line))
cout << line << endl;
input.close();
return 0;
}
I've previously been using setlocale, I've also now tried the windows SetConsoleCP nothing is working. This is the output everytime:
Displaying file contents:
Р?С?РёР?РчС'
Also, if there is a better way to output cyrillic text on the console, please let me know.
Windows OS sometimes behave strangely and unpredictable when it comes to cyrillic in console
You can try SetConsoleOutputCP(CP_UTF8);
it helped me once(<windows.h> header required)
So I'm a starter in C++, and I wanted to somehow get in text with accented characters.
I've read documentations, other questions, but none of them worked for me.
And I don't really understand it. Here is the file, and the output.
Text file:
abc
def
árvíztűrő tükörfúrógép
Console output:
abc
def
árvĂztĹ+rĹ' tĂĽkörfĂşrĂłgĂcp
Somebody please help me, how can I get the same text on the console, as in the file?
And could somebody explain to me, whats going on here? ( i.e. what are SetLocale() and .imbue() )
Thank you for help!
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <locale>
int main()
{
setlocale(LC_ALL, "");
std::string txt;
std::ifstream fstream;
fstream.open("C:\\Dev\\alma.txt");
fstream.imbue(std::locale(""));
while (getline(fstream, txt))
{
std::cout << txt << "\n";
}
std::cout << "\n";
fstream.close();
}
I just want to write a simple script that asks for someone's name and prints it out. But for some reason, when I use the string data type it just kind of, does nothing.
Here is the code and output if I use a string:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Please give me your name: ";
getline(cin, name);
cout << "Hello " << name << endl;
return 0;
}
As you can see, it just runs the .exe and nothing happens. However if I take the C approach to strings and use a character array and also don't use getline(), it works fine (but doesn't skip the '\0' character):
#include <iostream>
#include <string>
using namespace std;
int main()
{
//string name;
char name[15];
cout << "Please give me your name: ";
//getline(cin, name);
cin >> name;
cout << "Hello " << name << endl;
return 0;
}
I have tried a few other approaches and all have worked fine. For some reason, making the variable name as a string data type makes it behave weirdly. I tried looking around some C++ based books and the web but no one seems to have
had this problem.
I'm just going to add this for clarification, I checked to see if the program was just executing too fast for me to see what was going on. It is not doing that, it even skips the part where it is supposed to wait for input.
I am using Visual Studio Code to write the programs and MinGW to compile. I took my code and ran it over at https://www.onlinegdb.com/ and it worked as expected:
Could this possibly be a Windows or Command Prompt problem?
Depending on STL implementation and OS, std::cout may be buffering data in memory. It typically does not flush buffered data to console until either:
a threshold is reached
a '\n' character is output
std::cout.flush() is called (which includes outputting std::endl, which calls flush() after writing a line break).
Try calling std::cout.flush() (directly, or via std::flush) after outputting the prompt and before reading the name:
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
cout << "Please give me your name: " << flush;
getline(cin, name);
cout << "Hello " << name << endl;
return 0;
}
I've seen a couple of solutions to this question, but I'm still running into an issue. I've tried several of the solutions I've seen on this site, but I keep having the same issue. I'm not sure how to make this work, and I'm also not sure why it's not working, since I'm really new to C++.
A brief explanation of what I am attempting to do: I'm writing a simple old-school text-based story/game for a video game design club at my school, and at several points I have the user make decisions, or input things such as their name, which is then used in an output line, as it would be referenced several times. I have had no problem doing that, using something simple like this:
#include <iostream>
#include <string>
#include <limits>
#include <windows.h>
using namespace std;
int main(){
string name, place ;
cout << "???: What is your name? ";
getline (cin, name);
cout << "???: Hello, " << name << "!\n" << "\n";
}
My problem is that I'd like to have the text appear one character at a time, like dialogue, but whenever I try to use something I've seen written by others, it doesn't seem to like it very much. The only one that I tried that I can now find again is this:
#include <iostream>
#include <unistd.h> // include <windows.h> on windows
// function to output as if it was being typed
void type_text(const std::string& text)
{
// loop through each character in the text
for (std::size_t i = 0; i < text.size(); ++i)
{
// output one character
// flush to make sure the output is not delayed
std::cout << text[i] << std::flush;
// sleep 60 milliseconds
usleep(60000); // use Sleep on windows
}
}
int main()
{
type_text("Hej hej hallå!");
}
Apparently there is some sort of conflict regarding my attempt to output the name back to the user when I try to use that code with what I've written. I'm not really sure what the problem is, since I'm so new to C++, can anyone help me out?
Consider using std::this_thread::sleep_for, as it is standard C++11. Example:
#include <iostream>
#include <thread> // standard C++11
// function to output as if it was being typed
void type_text(const std::string& text)
{
// loop through each character in the text
for (std::size_t i = 0; i < text.size(); ++i)
{
// output one character
// flush to make sure the output is not delayed
std::cout << text[i] << std::flush;
// sleep 60 milliseconds
std::this_thread::sleep_for(std::chrono::milliseconds(60));
}
}
int main()
{
type_text("Hello, World!");
}
If you have access to a C++14 compiler, you can simply make use of the std::chrono user-defined literals and have a more "natural" syntax:
using namespace std::literals;
std::this_thread::sleep_for(60ms);
Here is a simple program to output to a text file:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
double myNumber = 42.5;
fstream outfile("test.txt", fstream::out);
outfile << "The answer is almost " << myNumber << endl;
outfile.close();
}
All that ends up being wrote to my text file is, "The answer is almost " and the data is not displayed at all. What am I doing wrong? or could it be a problem with Xcode since I am using that as an IDE.
I'm not sure what the problem is. Is it that it's never executed or that it's writing to the wrong path. To shed light on this try include unistd.h and insert this snippet.
char* s = getcwd(NULL, 256);
printf("im running and pwd is: %s\n", s);
Inside xcode hit CMD-SHIFT-R to open the console and see if it prints anything.
There is no problem with your code. It could be a problem with Xcode.