Why I cannot cout string like this:
string text ;
text = WordList[i].substr(0,20) ;
cout << "String is : " << text << endl ;
When I do this, I get the following error:
Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) c:\users\mollasadra\documents\visual studio 2008\projects\barnamec\barnamec\barnamec.cpp 67 barnamec**
It is amazing, that even this is not working:
string text ;
text = "hello" ;
cout << "String is : " << text << endl ;
You need to include
#include <string>
#include <iostream>
You need to reference the cout's namespace std somehow. For instance, insert
using std::cout;
using std::endl;
on top of your function definition, or the file.
There are several problems with your code:
WordList is not defined anywhere. You should define it before you use it.
You can't just write code outside a function like this. You need to put it in a function.
You need to #include <string> before you can use the string class and iostream before you use cout or endl.
string, cout and endl live in the std namespace, so you can not access them without prefixing them with std:: unless you use the using directive to bring them into scope first.
Above answers are good but If you do not want to add string include, you can use the following
ostream& operator<<(ostream& os, string& msg)
{
os<<msg.c_str();
return os;
}
You do not have to reference std::cout or std::endl explicitly.
They are both included in the namespace std. using namespace std instead of using scope resolution operator :: every time makes is easier and cleaner.
#include<iostream>
#include<string>
using namespace std;
Use c_str() to convert the std::string to const char *.
cout << "String is : " << text.c_str() << endl ;
If you are using linux system then you need to add
using namespace std;
Below headers
If windows then make sure you put headers correctly
#include<iostream.h>
#include<string.h>
Refer this it work perfectly.
#include <iostream>
#include <string>
int main ()
{
std::string str="We think in generalities, but we live in details.";
// (quoting Alfred N. Whitehead)
std::string str2 = str.substr (3,5); // "think"
std::size_t pos = str.find("live"); // position of "live" in str
std::string str3 = str.substr (pos);
// get from "live" to the end
std::cout << str2 << ' ' << str3 << '\n';
return 0;
}
Related
I want to use strings to input the path of files:
char** argv;
char* mytarget[2]={ (char*)"D:\\testlas\\BigOne.pcd",(char*)"D:\\testlas\\SmallOne.pcd" };
argv = mytarget;
for(int i=0;i<2;i++)
{
std::cout << "m.name: " << argv[i] <<std::endl;
}
However, cout outputs:
m.name: ?D:\\testlas\\BigOne.pcd
m.name: ?D:\\testlas\\SmallOne.pcd
Why is there a ? before the strings?
I use VS2017 C++11.
I created a new program and used the code:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
std::string test = "abc789";
cout << test << endl;
return 0;
}
It also outputs "?abc789". Why?
std::string test = "abc789";
There is a hidden LEFT-TO-RIGHT EMBEDDING character between the opening quote " and the first letter a (Unicode character U+202A, or UTF-8 E2 80 AA). Remove it, for example by deleting and retyping the line, then the ? will go away.
Why I cannot cout string like this:
string text ;
text = WordList[i].substr(0,20) ;
cout << "String is : " << text << endl ;
When I do this, I get the following error:
Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) c:\users\mollasadra\documents\visual studio 2008\projects\barnamec\barnamec\barnamec.cpp 67 barnamec**
It is amazing, that even this is not working:
string text ;
text = "hello" ;
cout << "String is : " << text << endl ;
You need to include
#include <string>
#include <iostream>
You need to reference the cout's namespace std somehow. For instance, insert
using std::cout;
using std::endl;
on top of your function definition, or the file.
There are several problems with your code:
WordList is not defined anywhere. You should define it before you use it.
You can't just write code outside a function like this. You need to put it in a function.
You need to #include <string> before you can use the string class and iostream before you use cout or endl.
string, cout and endl live in the std namespace, so you can not access them without prefixing them with std:: unless you use the using directive to bring them into scope first.
Above answers are good but If you do not want to add string include, you can use the following
ostream& operator<<(ostream& os, string& msg)
{
os<<msg.c_str();
return os;
}
You do not have to reference std::cout or std::endl explicitly.
They are both included in the namespace std. using namespace std instead of using scope resolution operator :: every time makes is easier and cleaner.
#include<iostream>
#include<string>
using namespace std;
Use c_str() to convert the std::string to const char *.
cout << "String is : " << text.c_str() << endl ;
If you are using linux system then you need to add
using namespace std;
Below headers
If windows then make sure you put headers correctly
#include<iostream.h>
#include<string.h>
Refer this it work perfectly.
#include <iostream>
#include <string>
int main ()
{
std::string str="We think in generalities, but we live in details.";
// (quoting Alfred N. Whitehead)
std::string str2 = str.substr (3,5); // "think"
std::size_t pos = str.find("live"); // position of "live" in str
std::string str3 = str.substr (pos);
// get from "live" to the end
std::cout << str2 << ' ' << str3 << '\n';
return 0;
}
So I have been trying for 1.30 hour to get this to work. I am new indeed, but I have searched all over the place and couldn't find an exact answer. I do not wish to do this another way, as it would take away the entire purpose of learning to code. I have to find why this thing isn't working. I tried dozens if not hunderds of syntaxes, but nothing works.
I want to read in a const char* name, than count the number of elements in it, so I thought had to be strlen(), and than output the name and the number of elements. If that works I can write the rest of the code.
#include <iostream>
using namespace std;
int main()
{
//writing your name, and counting the characters including \0
int a;
const char* name;
a = int strlen(name);
cin.getline(name);
cout << name;
cout >> a;
return 0;
}
There are a lot of problems with your code.
You are not allocating any memory for cin.getline() to read into. const char* name; is declaring an uninitialized pointer to nothing. You have to allocate memory for name before you can then read any data into it.
cin.getline() expects two input parameters (a pointer to an allocated buffer, and the max number of characters the buffer can hold), but you are only passing in one value.
You are calling strlen() before you have read anything into name (and there is a syntax error on your strlen() statement anyway).
You are passing a to std::cout using >>, but std::ostream does not implement the >> operator. You have to use << instead.
And lastly, don't use using namespace std;.
Try this instead:
#include <iostream>
#include <cstring>
int main()
{
//writing your name, and counting the characters including \0
int a;
char name[32];
std::cin.getline(name, 32);
a = std::strlen(name);
std::cout << "You entered: " << name << std::endl;
std::cout << "It is << a << " chars in length" << std::endl;
return 0;
}
Or, if you really don't like using std:: everywhere, at least use using <identifier>; instead of using namespace std;:
#include <iostream>
#include <cstring>
using std::cin;
using std::strlen;
using std::cout;
using std::endl;
int main()
{
//writing your name, and counting the characters including \0
int a;
char name[32];
cin.getline(name, 32);
a = strlen(name);
cout << "You entered: " << name << endl;
cout << "It is " << a << " chars in length" << endl;
return 0;
}
Now, that being said, the preferred solution is to use std::getline() instead of cin.getline():
#include <iostream>
#include <string>
int main()
{
int a;
std::string name;
std::getline(std::cin, name);
a = name.length();
std::cout << "You entered: " << name << std::endl;
std::cout << "It is " << a << " chars in length" << std::endl;
return 0;
}
I found a working solution, although I don't see where I had gone wrong. But this does exactly what I want using const char* and strlen() without using std::string.
Thanks for all your help, you have all pointed me to the correct direction.
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main ()
{
const char *name;
int len;
name = "stephane";
len = strlen(name);
cout << name;
cout << len;
return(0);
}
As another user has pointed out, I think it's a good idea for you to take a few steps back and read the basics until you understand how pointers work.
A const char* is that: const. It could be used usually while doing things like this:
const char* cpName = "Stephane"; //expected not to change through the program's lifetime
char* pName = "Stephane"; //can be changed to point to something else
char *pOther = "Vada";
pName = pOther; //pName now points to the string "Vada"
cpName = pOther; //this won't compile as cpName is const
Why I cannot cout string like this:
string text ;
text = WordList[i].substr(0,20) ;
cout << "String is : " << text << endl ;
When I do this, I get the following error:
Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) c:\users\mollasadra\documents\visual studio 2008\projects\barnamec\barnamec\barnamec.cpp 67 barnamec**
It is amazing, that even this is not working:
string text ;
text = "hello" ;
cout << "String is : " << text << endl ;
You need to include
#include <string>
#include <iostream>
You need to reference the cout's namespace std somehow. For instance, insert
using std::cout;
using std::endl;
on top of your function definition, or the file.
There are several problems with your code:
WordList is not defined anywhere. You should define it before you use it.
You can't just write code outside a function like this. You need to put it in a function.
You need to #include <string> before you can use the string class and iostream before you use cout or endl.
string, cout and endl live in the std namespace, so you can not access them without prefixing them with std:: unless you use the using directive to bring them into scope first.
Above answers are good but If you do not want to add string include, you can use the following
ostream& operator<<(ostream& os, string& msg)
{
os<<msg.c_str();
return os;
}
You do not have to reference std::cout or std::endl explicitly.
They are both included in the namespace std. using namespace std instead of using scope resolution operator :: every time makes is easier and cleaner.
#include<iostream>
#include<string>
using namespace std;
Use c_str() to convert the std::string to const char *.
cout << "String is : " << text.c_str() << endl ;
If you are using linux system then you need to add
using namespace std;
Below headers
If windows then make sure you put headers correctly
#include<iostream.h>
#include<string.h>
Refer this it work perfectly.
#include <iostream>
#include <string>
int main ()
{
std::string str="We think in generalities, but we live in details.";
// (quoting Alfred N. Whitehead)
std::string str2 = str.substr (3,5); // "think"
std::size_t pos = str.find("live"); // position of "live" in str
std::string str3 = str.substr (pos);
// get from "live" to the end
std::cout << str2 << ' ' << str3 << '\n';
return 0;
}
I am using VS Professional 2013 and I am building a console application. One of my methods is determining string length from user input. I keep receiving a confusing error on line 5 of this method: that there is a missing semicolon (error C2143). No functions within the line require an extra semicolon to my knowledge. Also, I am intentionally not calling namespaces. The #include functions are replicated below and are stored in the header file.
#include <stdio.h>
#include <tchar.h> //Part of VS' implementation for applications. Can effectively be ignored.
#include <iostream>
#include <string>
int main() {
std::string s;
std::cout << "Enter your string: " << std::flush;
std::string.getline(std::cin, s);
const int size = s.length();
std::cout << "The total number of characters entered is: " << size << std::endl;
}
std::string has no member getline, so std::string.getline(std::cin, s); is illegal.
You want
std::getline(std::cin, s);