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
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
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);
In my main.cpp all of my cout's and cin's have errors.
/**
* Description: This program demonstrates a very basic String class. It creates
* a few String objects and sends messages to (i.e., calls methods on)
* those objects.
*
*/
//#include <iostream>
#include "mystring.h"
//using namespace std;
/* Function Prototypes */
void Display(const String &str1, const String &str2, const String &str3);
/*************************** Main Program **************************/
int main()
{
String str1, str2, str3; // Some string objects.
char s[100]; // Used for input.
// Print out their initial values...
cout << "Initial values:" << endl;
Display(str1, str2, str3);
My main.cpp cannot not be changed, so my question is, how can I fix this error, what do I have to add to my header file and implementation file?
In my main.cpp all of my cout's and cin's have errors.
You simply need to include <iostream> header file, and use std with cout and cin:
#include <iostream>
//^^
int main()
{
std::cout << "Initial values: "<< std::endl;
//^^
}
You have iostream header commented out here:
//#include <iostream>
You also need to add std::, this:
cout << "Initial values:" << endl;
should be:
std::cout << "Initial values:" << std::endl;
I see that you have using namespace std; commented out. I would advise against using namespace std;, it may save you some typing but it is considered bad practice and can cause problem later on.
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;
}