reading string in c++ - c++

In the Dev C++ compiler I can write and successfully compile this:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string method;
cin >> method;
return 0;
}
but when I wrote the above code in Visual Studio 2013 (console app mode) I got this error:
Error: no operator ">>" matches these operands
operand types are: std::istream >> std::string
EDIT
in Visual Studio:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string method;
cin >> method;
return 0;
}
I understand what the error tells me. But why in only Visual Studio 2013?

Try to place header "stdafx.h" before other headers.
#include "stdafx.h"

simple exlample for read string in visual studio :
#include "stdafx.h"
#include<iostream>
#include<string>
#include<conio.h>
using namespace std;
int main()
{
std::string str ;
getline(cin, str);
cout << str;
_ getch();
return 0;
}

Related

I have just started c++ but I'm getting error in hackerRank for this code

this is the program
#include <iostream>
#include <cstdio>
#include <conio.h>
using namespace std;
int main() {
clrscr();
cout<<"Hello, World!";
getch();
}
fatal error: conio.h: No such file or directory #include <conio.h>sdfs
getch return a value
as follows
#include <iostream>
#include <cstdio>
#include <conio.h>
using namespace std;
int main() {
clrscr();
cout<<"Hello, World!";
char a = getch();
}

why g++ shows "gets()" not declared ,even after including <cstdio>

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char str[30];
gets(str);
}
when i use gets () function compiler gives me the following error
error: 'gets' was not declared in this scope
i was using G++ with geany ide
please make the solution simple cuz iam a beginner.
gets was deprecated in C++11 and removed from C++14. If you are using GCC6.0 or newer then by default it uses C++14 and won't be available. Instead of using
main()
{
char str[30];
gets(str);
}
use
int main()
{
std::string str;
std::getline(cin, str);
}
gets is an unsafe function and is not supported by the C Standard any more.
Instead use fgets.
For example
#include <iostream>
#include <cstdio>
#include <cstring>
int main()
{
char str[30];
std::fgets(str, sizeof( str ), stdin );
str[ std::strcspn( str, "\n" ) ] = '\0';
//...
}
You can use scanf() to input string.
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char str[30];
scanf("%s", str);
}

Xcode C++ Vectors: Implicit instantiation of undefined template

I ran this code on a different IDE and it was successful. For some reason I get the above error message on Xcode. I assume I'm missing a header of some kind, but I'm not sure which one.
#include <iostream>
#include <limits>
#include <string>
#include <vector>
int main() {
vector<string> listRestaurants; // error: Implicit instantiation of undefined template
return 0;
}
Xcode 10.2.1 was showing me the error
Implicit instantiation of undefined template 'std::__1::vector<std::__1::basic_string<char>, std::__1::allocator<std::__1::basic_string<char> > >'.
#include <iostream>
#include <vector>
int main(int argc, const char * argv[]) {
std::vector<std::string> listRestaurants;
....
return 0;
}
Fixed my issue.
If adding std:: is not the issue for you, then check if you have #include <vector>. That fixed the issue for me.
Didn't realize that #include <vector> is required. I thought it was part of standard library template; I ran this code in VSCODE IDE and it worked fine for me
#include <iostream>
#include <vector>
using namespace std;
int main()
{
uint_least8_t i; // trying to be conscious of the size of the int
vector<int> vect;
for(i = 0; i < 5; ++i)
{
vect.push_back(i);
}
for(auto i : vect)
{
cout << i << endl;
}
return 0;
}
From the comments:
The std namespace houses both of those templates. Change vector to std::vector, and string to std::string. – WhozCraig
the vector and string were placed in the namespace std
using namespace std;

Undefined reference to error, when using namespaces in headers in c++

I am getting a bunch of errors saying "Undefined reference to ...", and cant understand why. I have read other questions with the error "Undefined reference to ...", but the answers doesent work for me. Here is part of my code:
main:
#include <iostream>
#include <armadillo>
#include <string>
#include <DombsMain.h>
using namespace std;
using namespace arma;
int main(){
cout << "Armadillo version: " << arma_version::as_string() << endl;
dombsmain::initilize("test");
return 0;
}
dombsmain.h:
#ifndef DOMBSMAIN_H_INCLUDED
#define DOMBSMAIN_H_INCLUDED
#include <string>
#include <vector>
#include "Body.h"
#include "Constraint.h"
#include <Solver.h>
namespace dombsmain {
extern void initilize(std::string fileName);
extern std::string inputfileName;
...blabla
}
#endif // DOMBSMAIN_H_INCLUDED
dombsmain.cpp:
#include <DombsMain.h>
#include <BallJoint.h>
#include <dombs.h>
#indlude blabla
using namespace std;
using namespace arma;
namespace dombsmain{
void initilize(string infileName){
inputfileName = infileName;
..blabla
I think the error has something to do with namespace dombsmain. DombsMain.h is included both in main and in dombsmain.cpp, but it still says that the variables and functions in the namespace in undefined. I think it might be some conflict with including DombsMain.h int both main and dombsmain.cpp. I tried deleting the #include <DombsMain.h> in main.cpp, but then I couldent call dombsmain::initilize("test");
You just forgot to define std::string inputfileName. For example you can do it in dobsmain.cpp:
namespace dombsmain{
void initilize(string infileName){
inputfileName = infileName;
}
std::string inputfileName = "";
}

Is <iostream> needed to use a string in c++?

This code works for me:
#include <string>
#include <iostream>
int main()
{
std::string s;
s = "hello world";
cout << s;
return 0;
}
But this one doesn't:
#include <string>
int main()
{
string s;
s = "hello world";
return 0;
}
Is the include of <iostream> needed as well as the <string> one?
I'm using Eclipse CDT IDE.
Iostream is not needed to use string. You are missing using namespace std (or alternatively using the std:: prefix) in the second example, that's why it's not working.