Reputation: 2227
So hello guys, its my code:
#include <iostream>
#include <string>
using namespace std;
void writeDown(string*t)
{
for (int i = 0; *(t+i)!=NULL; i++)
{
cout << *(t+i) <<endl;
}
}
int main()
{
string t;
getline(cin, t);
string *wsk = &t;
writeDown(wsk);
return 0;
}
So I simply insert a string, and program should cout<<
every single char from it in a new line. But here is what pops out:
binary '!=' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion)
What am I doing wrong?
Btw. I am working in VS 2013 for Win Desktop (btw. vol.2 - is it a good environment for C++ coding? - got 2015, but it's to slow on my laptop)
Upvotes: 0
Views: 47
Reputation: 1124
Well that code is bad in so many levels, but just to keep it simple and answer your question I would probably use iterators. Forget about that old C style if you are using C++.
Try this:
void writeDown(const string & t)
{
for (string::const_iterator it = t.begin();
it != t.end();
++it)
{
cout << *it << endl;
}
}
Note that writeDown
no longer takes a pointer as parameter but a constant reference to the string you want to print.
You will have to change your main to call writeDown
:
string t;
getline(cin, t);
writeDown(t);
If you insist on using the "array" method you would have to use it like this, but ...
void writeDown(const string & t)
{
for (int i = 0; t.c_str()[i] != NULL; i++)
{
cout << t.c_str()[i] << endl;
}
}
Upvotes: 2