Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
I don't know what's wrong here ?
It's just running errors !!!
#include <iostream>
using namespace std;
int main()
{
cout << string("hello world");
return 0;
}
Read more about C++. So read first Programming -- Principles and Practice Using C++.
Then read C++ reference documentation, notably the one about std::string-s.
You need to #include <string>
You should enable all warnings when compiling. If using GCC, compile with g++ -Wall -g
You don't need that string before the actual string:
#include <iostream>
using namespace std;
int main()
{
cout << "hello world";
return 0;
}
Or, alternatively, if you're trying to store a string:
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str = "hello world";
cout << str;
return 0;
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
I need to compile C++ code directly in the terminal or CLI without saving the file
When is use the below way, It shows me an error.
gcc -x c - <<eof
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world";
}
eof
You are trying to compile a C++ program using a C compiler.
This works:
g++ '-xc++' - <<eof
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world";
}
eof
If you are trying to run a C++ program from a source,
this works:
g++ -xc++ - && ./a out <<eof
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world";
}
eof
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
Hello I want to get some input in a string in cpp and I am getting and error. Here is the code:
#include <iostream>
int main() {
using namespace std;
string name;
cout << "Type your name:";
cin >> name;
cout << "Your name is: " << name;
return 0;
}
I am building the project and I get this error:
Test1.cpp:10:6: error: invalid operands to binary expression
It is this line: cout << "Type your name:";
What am I missing here ? It is the first time when I am using c++
You need to include header <string>
#include <string>
<iostream> does not include <string>. Hence, you also need to include <string> in order to use std::string.
#include <iostream> // for std::cout, std::cin
#include <string> // for std::string
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Here is the code:
#include <iostream>
#include <stdio.h>
int main()
{
char name[15];
printf_s("What is your name: ");
scanf_s("%s",name);
printf_s("Nice to meet you, %s", name);
return(0);
}
Please help idk whats wrong. Im doing this in VS2019 and using c++ if that helps.
You can just use scanf instead of scanf_s, to silence the error you can write "#define _CRT_SECURE_NO_WARNINGS" on top of your code. Difference between scanf and scanf_s is, in scanf_s you can specify buffer size and control the size of an input to avoid crash. It's not necessary in this level but i suggest you to look into it.
Also, if you're using C++, you can declare strings like:
std::string varName, and cout/cin operations are way easier imo.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
int main()
{
char name[128];
printf("What is your name: ");
scanf("%s", &name);
printf("Nice to meet you, %s", name);
return(0);
}
Or in easier way:
#include <iostream>
int main()
{
std::string name;
std::cout<< "What is your name: ";
std::cin >> name;
std::cout << "Nice to meet you, " << name;
return 0;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm really really new to C++ and this is my first program on Visual Studio 2015, It shows me 2 errors:
"{" missing function header (old style format list)
expected a declaration
#include "stdafx.h"
#include <iostream>
int main();
{
cout << "Hello World";
return 0;
}
int main() remove ; at the end.
#include "stdafx.h"
#include <iostream>
int main(); // this ';' is giving problem remove it.
{
std::cout << "Hello World"; // use std::cout
return 0;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I am copying this directly from a textbook.
#include <iostream>
using namespace std;
int main()
{
string myHello = "This is Chapter 3 already?";
cout << myHello << endl; //this part is not working
return 0;
}
Why isn't this code working?
I have tried looking through the book for solutions but no solution is provided or available.
You need to #include <string> and then that'll work, do it above the using namespace command, if you got rid of the namespace command you'd need to do std::string myhello;
#include <string>