I wrote a program that reverses the string that is inputted by the user, but it doesn't work. I did it using string reverse_name(name.rbegin(), name.rend()) from Reverse the string in C++, but it doesn't work and gives me the error:
no viable conversion from 'std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> >::reverse_iterator' (aka
'reverse_iterator<__normal_iterator<char *, std::__cxx11::basic_string<char> > >') to
'std::__cxx11::string' (aka 'basic_string<char>')
string reversed_word = (word.rbegin(), word.rend());
My code:
#include <iostream>
using namespace std;
int main()
{
string word, reversed_word;
cin >> word;
reversed_word = (word.rbegin(), word.rend());
cout << reversed_word;
return 0;
}
This line is wrong:
reversed_word = (word.rbegin(), word.rend());
The error message is self explanatory. Here is a simplified version to help make it easier for you to understand:
no viable conversion from 'reverse_iterator' ... to ... 'std::string'
You can't assign a (reverse) iterator to a string, but that is exactly what you are trying to do. The expression (word.rbegin(), word.rend()) does not construct a new string, like you are expecting. It simply evaluates the two iterators as-is, separated by the comma operator, which returns the value on the right side. So the line above is effectively the same as this:
reversed_word = word.rend();
To do what you are attempting, you need to pass the iterators to the std::string constructor instead. Either like this:
string reversed_word;
...
reversed_word = string(word.rbegin(), word.rend());
Or like this 1:
string reversed_word(word.rbegin(), word.rend());
1: as shown in this answer to the question you linked to, and even shown in your own question where you say "I did it using ... ".
The easiest way to do it.
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
string word, copy;
cin >> word;
copy = word
reverse(word.begin(), word.end());
cout << copy << endl;
cout << word << endl;
return 0;
}
Related
This question already has answers here:
How do I print out the contents of a vector?
(31 answers)
Closed 1 year ago.
Why doesn't this vector print out?
void str_read(std::vector<char> str);
int main() {
std::vector<char> str;
str_read(str);
std::cout << str << std::endl;
return -1;
}
void str_read(std::vector<char> str) {
while (1) {
char ch;
scanf("%c", &ch);
if (ch == '\n');
break;
}
}
I get an error:
error: no type named 'type' in 'struct std::enable_if<false, void>'
You get the error because there is no standard operator<< defined for std::vector. If you want that, you have to implement it yourself.
Even so, str_read() takes in a std::vector by value, so it receives a copy of the caller's vector, and thus any modifications it makes will be to that copy and thus lost when str_read() exits. That means the vector in main() is never populated.
Try this instead:
#include <iostream>
#include <vector>
#include <string>
std::ostream operator<<(std::ostream &out, const std::vector<char> &vec) {
for(auto ch : vec) {
out << ch;
}
return out;
/* alternatively:
return out.write(vec.data(), vec.size());
*/
}
void str_read(std::vector<char> &str) {
char ch;
while (cin.get(ch) && ch != '\n') {
str.push_back(ch);
}
}
int main() {
std::vector<char> str;
str_read(str);
std::cout << str << std::endl;
}
That being said, why not just use std::string instead of std::vector<char>? There is a standard operator<< defined for std::string, and a standard std::getline() function for reading characters into std::string until '\n':
#include <iostream>
#include <string>
int main() {
std::string str;
std::getline(cin, str);
std::cout << str << std::endl;
}
There are 3 BIG problems here.
You are passing the vector by value, not by reference. As a result, any changes made to the vector in the function will stay local to the vector initialised as a part of that function call stack. It will be deleted by the time you return. so change the signature to str_read(std::vector<char>& str).
You are going through the stdin capture character by character. AFAIK scanf although will read the new line character, will leave it in the buffer not appending it to your string. From the looks of it, all you are trying to do is read a string from stdin, store it in a char array, and print that char array out. Don't overengineer stuff. just use std::cin and store it in a string, to begin with, like
std::string captured_string;
std::getline(std::cin, captured_string);
You can then std:cout << captured_string << "\n"; directly
If you insist on storing characters in a vector, which I do not understand why would you need to, you can just do std::vector<uint8_t> char_array(captured_string.begin(), captured_string.end()). However, std::cout << char_array << "\n" will still not work. That is for 2 reasons, the first one being that std::vector<T, allocator> does not have an overload for << of std::ostream. The second is that the moment you convert your string to an array of characters, they do not mean the same thing.
I think you misunderstood what you were taught in class about strings being stored as arrays of characters. They are indeed stored as arrays, but an array type and a string type are fundamentally different and are not equivalent.
Capturing the string from stdin will anyway store it in a char* or a std::string as I have shown above. You can use that directly without having to convert a string to an array of characters.
In essence, your program decays to this
#include <iostream>
#include <string>
int main()
{
std::string captured_string;
std::getline(std::cin, captured_string);
std::cout << captured_string << "\n";
return EXIT_SUCCESS;
}
EDIT
I just realised that your objective may have been to print the string character by character. In that case, you don't need to convert it to an array. There are multiple ways of doing this, but the least confusing and probably the easiest way of doing this would be to use the range-based for loop.
int main()
{
std::string captured_string;
std::getline(std::cin, captured_string);
for (auto& ch: captured_string) // containers in the standard library support range based for loops.
std::cout << ch << "\n"; // print each character in the string on a new line
return EXIT_SUCCESS;
}
First, explain
error: no type named 'type' in 'struct std::enable_if<false, void>'
cout is character-oriented stream output and does not accept output formats of the vector<char> type.
Vector is an array of elements of the specified type. If you want to use a vector, you can store values in it first:
vector<char> str;
str.push_back('H');
An element of the array is then printed to standard output cout
cout << str[0] <<endl;
The second problem is that your function str_read is defined as void, which has no value, so it cannot be located in cout's standard output either.
It looks like you want to put the characters as input into a vector and print them out. If you position the function as void, you will need to change the value of str by reference
Next question, I see that your while loop break is mistimed and does not combine with if. You should not add a semicolon after an if statement.
if (ch == '\n')break;
Finally, if you choose to change the value of str by reference, you need to implement this in the function, as mentioned above, when you type a value of ch, you need to use str.push_back(ch) to store it.
Your 'str' variable doesn't have anything in it when you try to display it. You also don't initialize your vector to any variables for the function to read anyways.
I am trying this code to perform the following function : given an input file and a word(string) output should be the lines in the file containing the given word along with the line number. But I am getting an error saying "no matching function for call to ..." in the line where I am performing string operation (strstr). Here is my code. Please help me solve this. I am facing the same error in other programs also where ever I perform operations involving strings.
#include<iostream>
#include<fstream>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
int x;
char c;
ifstream iFile1, iFile2;
ofstream oFile;
char file1[50], file[50];
char word[50];
cout << "First file?\n";
gets(file1);
std::string str;
cout << "Word?\n";
gets(word);
iFile1.open(file1);
while (std::getline(iFile1, str)) {
x++;
if (strstr(str, word)) {
cout << "%d\t,x";
cout << str;
}
}
}
error: no matching function for call to 'strstr(std::string&,char[50])'
if(strstr(str,word))
^
char * strstr (char * str1, const char * str2 );
This is the signature of the strstr function, which requires both elements to be char*, but you are passing as first element a std::string&.
To fix this, try:
if (strstr(str.c_str(), word))
The c_str() method returns a char * to an array that contains the elements of the string
Also please note that the gets function is deprecated
Not the right answer, but still important to point out:
<string.h> is not <string>
<string.h>, and <cstring>, are header files defining several functions to manipulate C Strings and arrays (Source)
<string> defines std::string, which has the handy member function .c_str() allowing you to use functions which want a parameter of char*
this is my first time posting a question so I hope I'm getting this right. Anyways, I'm trying to create a program to ask the user for a string, count the types and numbers of letters, then output the frequency of the letters. So far I'm having an error with even getting the right input, and just can't figure out what the issue is. My (relevant) code is:
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;
string getPhrase(const string & phrase); //Function for gathering string input
int main()
{
const string phrase;
getPhrase(phrase);
...
}
string getPhrase(const string &phrase)
{
cout<<"Enter phrase: "
getline(cin, phrase);
return (phrase);
}
When I run, this I get the error:
freq.cpp: In function ‘std::string getPhrase(const std::string&)’:
freq.cpp:21: error: no matching function for call to ‘getline(std::istream&, const
std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)’
I have no idea what I'm doing wrong, and just can't seem to find anything online that's relevant to what I'm doing.
Your getPhrase should look like this:
std::string getPhrase()
{
std::string result;
std::cout << "Enter phrase: ";
std::getline(std::cin, result);
return result;
}
Then:
int main()
{
std::string phrase = getPhrase();
// ...
}
const string phrase;
remove const in function parameter and local variable declaration since otherwise you can't accept user input to a const variable, which means nonchangable/ non-modifiable.
Like the following:
string getPhrase(string & phrase); //Function for gathering string input
int main()
{
string phrase;
getPhrase(phrase);
//...
}
Notice that phrase is a const string. That means it's constant and can't be modified. Therefore you can't use getline to set phrase to the user's input.
You should declare phrase with string phrase; and then make the parameter of getPhrase a non-const reference.
string getPhrase(string& phrase); //Function for gathering string input
int main()
{
string phrase;
getPhrase(phrase);
...
}
While trying my code to answer another question I found out that the following didn't compile
#include <iostream>
#include <cstring>
#include <sstream>
#include <string>
using namespace std;
// (main omitted)
const char * coin = "3D";
istringstream ss(string(s));
int i;
ss >> hex >> i; <--- error here
cout << (char) i << endl;
It failed with the following error:
test.cpp:15:11: error: invalid operands of types ‘std::istringstream(std::string) {aka std::basic_istringstream<char>(std::basic_string<char>)}’ and ‘std::ios_base&(std::ios_base&)’ to binary ‘operator>>’
While the following compiled and ran properly :
const char* coin = "3D";
string s(coin);
istringstream ss(s); // or directly istringstream ss("3D")
int i;
ss >> hex >> i;
cout << (char) i << endl;
If I look at the definition of the constructor of istringstream, it accepts const std::string& (actually the basic_string<char> equivalent), and that compiles. So I guess the template argument deduction has a behaviour I don't understand and create a not so conform istringstream, but why ?
I am using GCC 4.6.1 (Ubuntu flavor).
EDIT : since istringstream is a typedef, I doubt there's any problem with templates in the end.
istringstream ss(string(s));
Your compiler thinks that's a declaration of a function taking a string (named s) and returning an istringstream. Surround the argument in parentheses in order to disambiguate it. By the way, what is s? Did you mean coin there?
istringstream ss( (string(coin)) );
Read this if you are confused.
In this particular case, you could of course have just done this:
istringstream ss(coin);
If your compiler supports it, you can also avoid the MVP using uniform initialization syntax:
istringstream ss{string{coin}};
That probably looks a bit odd to most people, I know it looks odd to me, but that's just because I'm so used to the old syntax.
I'm trying to figure out how to print a string several times. I'm getting errors. I just tried the line:
cout<<"This is a string. "*2;
I expected the output: "This is a string. This is a string.", but I didn't get that. Is there anything wrong with this line? If not, here's the entire program:
#include <iostream>
using namespace std;
int main()
{
cout<<"This is a string. "*2;
cin.get();
return 0;
}
My compiler isn't open because I am doing virus scans, so I can't give the error message. But given the relative simplicity of this code for this website, I'm hoping someone will know if I am doing anything wrong by simply looking.
Thank you for your feedback.
If you switch to std::string, you can define this operation yourself:
std::string operator*(std::string const &s, size_t n)
{
std::string r; // empty string
r.reserve(n * s.size());
for (size_t i=0; i<n; i++)
r += s;
return r;
}
If you try
std::cout << (std::string("foo") * 3) << std::endl
you'll find it prints foofoofoo. (But "foo" * 3 is still not permitted.)
There is an operator+() defined for std::string, so that string + string gives stringstring, but there is no operator*().
You could do:
#include <iostream>
#include <string>
using namespace std;
int main()
{
std::string str = "This is a string. ";
cout << str+str;
cin.get();
return 0;
}
As the other answers pointed there's no multiplication operation defined for strings in C++ regardless of their 'flavor' (char arrays or std::string). So you're left with implementing it yourself.
One of the simplest solutions available is to use the std::fill_n algorithm:
#include <iostream> // for std::cout & std::endl
#include <sstream> // for std::stringstream
#include <algorithm> // for std::fill_n
#include <iterator> // for std::ostream_iterator
// if you just need to write it to std::cout
std::fill_n( std::ostream_iterator< const char* >( std::cout ), 2, "This is a string. " );
std::cout << std::endl;
// if you need the result as a std::string (directly)
// or a const char* (via std::string' c_str())
std::stringstream ss;
std::fill_n( std::ostream_iterator< const char* >( ss ), 2, "This is a string. " );
std::cout << ss.str();
std::cout << std::endl;
Indeed, your code is wrong.
C++ compilers treat a sequence of characters enclosed in " as a array of characters (which can be multibyte or singlebyte, depending on your compiler and configuration).
So, your code is the same as:
char str[19] = "This is a string. ";
cout<<str * 2;
Now, if you check the second line of the above snippet, you'll clearly spot something wrong. Is this code multiplying an array by two? should pop in your mind. What is the definition of multiplying an array by two? None good.
Furthermore, usually when dealing with arrays, C++ compilers treat the array variable as a pointer to the first address of the array. So:
char str[19] = "This is a string. ";
cout<<0xFF001234 * 2;
Which may or may not compile. If it does, you code will output a number which is the double of the address of your array in memory.
That's not to say you simply can't multiply a string. You can't multiply C++ strings, but you can, with OOP, create your own string that support multiplication. The reason you will need to do that yourself is that even std strings (std::string) doesn't have a definition for multiplication. After all, we could argue that a string multiplication could do different things than your expected behavior.
In fact, if need be, I'd write a member function that duplicated my string, which would have a more friendly name that would inform and reader of its purpose. Using non-standard ways to do a certain thing will ultimately lead to unreadable code.
Well, ideally, you would use a loop to do that in C++. Check for/while/do-while methods.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int count;
for (count = 0; count < 5; count++)
{
//repeating this 5 times
cout << "This is a string. ";
}
return 0;
}
Outputs:
This is a string. This is a string. This is a string. This is a string. This is a string.
Hey there, I'm not sure that that would compile. (I know that would not be valid in c# without a cast, and even then you still would not receive the desired output.)
Here would be a good example of what you are trying to accomplish. The OP is using a char instead of a string, but will essentially function the same with a string.
Give this a whirl:
Multiply char by integer (c++)
cout<<"This is a string. "*2;
you want to twice your output. Compiler is machine not a human. It understanding like expression and your expression is wrong and generate an error .
error: invalid operands of types
'const char [20]' and 'int' to binary
'operator*'