Every time I create a new project in CLion the default main.cpp is written like this:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
But I want it to look like this:
/**/
#include <iostream>
using namespace std;
int main(){
return 0;
}
I searched everywhere but can't undestand how to do it.
Thank you in advance.
Related
Trying to figure out why creating an instance of a string results in my program being shut down...
My simple program is:
#include <iostream>
#include <string>
int main(){
std::cout << "HELLO" << std::endl;
std::string str("str");
return 0;
}
My problem is that this program prints nothing. However:
#include <iostream>
#include <string>
int main(){
std::cout << "HELLO" << std::endl;
// std::string str("str");
return 0;
}
works perfectly fine and prints "HELLO".
I'm compiling with mingw this way: g++ main.cpp -o main.exe (doesn't show any errors)
I've tried using the string in any way (like printing it). In general I'm trying to create a string instance to do std::cin>> into it.
Help will be much appreciated! Thank you
Works for me:
$ cat test.cpp
#include <iostream>
#include <string>
int main(){
std::cout << "HELLO" << std::endl;
std::string str("str");
return 0;
}
$ g++ test.cpp
$ ./a.out
HELLO
$
The problem is elsewhere in your system, the code is OK.
I need to parse json in my C++ program. I decided to use RapidJson library for this purpose, but I got "abort() has been called" error. I truncated the code to this:
#include <iostream>
#include <cstdlib>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/encodings.h"
#include "rapidjson/stringbuffer.h"
using namespace std;
using namespace rapidjson;
typedef GenericDocument<UTF16<> > WDocument;
typedef GenericValue<UTF16<> > WValue;
wchar_t request[] = L"{\"result\":\"OK\"}";
int main()
{
WDocument d;
d.Parse(request);
WValue& v = d[L"result"]; // The exception throws here
if (wcscmp(v.GetString(), L"OK"))
{
cout << "Success!" << endl;
}
else
cout << "Fail!" << endl;
system("pause");
return 0;
}
but I got the error again. Where is the mistake? Thanks in advance!
check this line:
wchar_t request[] = L"{\"result\":\"OK\"}";
there is a character before the left brace.
when i try to cout the get function it don't return any value !
my header file :
#pragma once
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class Dog
{
private:
string dogName = "Max";
public:
Dog();
void talking();
void jumping();
void setDogName(string newDogName);
///////thats the function /////
string getDogName();
};
my cpp file :
#include "stdafx.h"
#include <iostream>
#include <string>
#include "Dog.h"
using namespace std;
Dog::Dog() {
cout << " Dog has been Created " << endl;
}
void Dog::setDogName(string newDogName)
{
newDogName = dogName;
}
string Dog::getDogName()
{
return dogName;
}
and my main.cpp file:
#include "stdafx.h"
#include <iostream>
#include "Dog.h"
#include "Cat.h"
int main()
{
Dog ewila;
ewila.setDogName("3wila");
cout << ewila.getDogName();
return 0;
}
im learning c++ new so i don't know whats happening even i tried to type ewila.getDogName(); by itself and it didn't do anything
and i tried to store the setDogname() in a variable to return with the getDogName() and also nothing i don't know what i'm doing wrong
also : im using visual studio 2017 and i run the program from visual c++ 2015 MSBuild command prompt
The problem is in line newDogName = dogName; of function void Dog::setDogName(string newDogName). You are using assignment operator(=) incorrectly. This should be dogName =newDogName;
So the corrected CPP file will be:
#include "stdafx.h"
#include <iostream>
#include <string>
#include "Dog.h"
using namespace std;
Dog::Dog() {
cout << " Dog has been Created " << endl;
}
void Dog::setDogName(string newDogName)
{
dogName = newDogName;
}
string Dog::getDogName()
{
return dogName;
}
My code is a basic HelloWorld but fails to compile when I use cout<<endl.
I'm using Microsoft visual studio fresh download and created a console application for my first test project.
// Test1ConsoleApplication.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <iostream>
//#include <ostream>
using namespace std;
int main()
{
string s = "hello world!!";
cout << "lets see: " << s << endl;
return 0;
}
It generates a
"C1001" at line 1.
Replacing "endl" with ""\n"" works though.
You don't need the precompiled header #include <stdafx.h> so you can safely get rid of it. Also get rid of using namespace std; because it pollutes the global namespace. Try something like this. There's no reason it shouldn't work.
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
int main()
{
string s = "hello world!!";
cout << "lets see: " << s << endl;
return 0;
}
In Visual Studio you can disable use of the precompiled header in the project settings.
I do not see what the problem is. Both options compile and execute for me.
RexTester cppOnline
// Test1ConsoleApplication.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <string>
#include <iostream>
//#include <ostream>
using namespace std;
int main()
{
string s = "hello world!!";
cout << "lets see: " << s << endl;
cout << "lets see: " << s << "\n";
return 0;
}
So idk what was causing the error but it was fixed after pasting imports to the "stdafx.h" header file and then delete them...
I have the following script in C++:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector <string> markus = { "M", "A", "R", "K", "U", "S" };
for (int i = 0; i < markus.size();i++) {
cout << markus[i];
}
return 0;
}
I have successfully installed g++. When I try compiling this code with the command g++ -o test test.cpp, I get no errors. However, when I try running the created test.exe-file, with the command test, I get an error saying "could not find starting point". First, there is a long sequence, then the following message; "The starting point for the procedure could not be found in the library for dynamical links", and the the absolute path for test.exe.
If I remove #include <vector>, and try the following code;
#include <iostream>
using namespace std;
int main() {
cout << "Hello world";
return 0;
}
, it works perfect.
I would be glad for the help.