Beginner Error with "getline" - c++

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);
...
}

Related

How to pass string to gets_s() in C++?

#include<iostream>
using namespace std;
int main() {
string str;
gets_s(str);
cout << str << endl;
return 0;
}
When I tried to run the above code it threw an error that no instance of gets_s() matched the argument list.
How can I pass an std::string instead of a char[] to gets_s() function if is possible?
The C function get_s takes a char* and a length argument, not a std::string.
Your best options are:
Formatted input:
std::cin >> str;
Read a line:
std::getline(std::cin, str);
Don't do that. Use the stream in a normal way:
#include<iostream>
using namespace std;
int main()
{
string str;
cin >> str;
cout << str << endl;
return 0;
}
gets_s has a significant limitation in that you must provide an upper limit on the number of characters you want to read.
Since you are using string the superior alternative is to use getline
#include <iostream>
#include <string>
using namespace std;
string str;
getline(cin, str);
This will expand the string to hold as many characters as are entered by the user.
gets_s() takes two arguments: pointer to char array and maximal size (your call is missing it). You cannot pass std::string - only C style strings.
Instead of C functions, why not use C++ way std::cin >> str or getline(std::cin, str)?
In C also don't use gets_s() (it's optional in C11) or gets() - use fgets() instead.
Well, there are a lot of answers about std::getline, but in case if you really need to use get_s, you may write such code:
size_t length = 10; // Just for example
std::string my_string(length, 0);
get_s(&my_string[0], length); // C++14 and older
get_s(my_string.data(), length); // C++17 and newer

String not reversing in C++

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;
}

Using cin.getline with string declared

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line,line2;
char dude[20];
cin.getline(dude,20);
fstream myfile ("example.txt",ios::in);
if(!myfile)
{
cout<<"Not Found! ";
system("pause");
}
while (!myfile.eof())
{
getline(myfile,line);
cout<<line;
}
myfile.close();
exit(0);
}
This works but if i do this:
int main () {
string line,line2,dude;
It gives me an error.
[Error] no matching function for call to 'std::basic_istream::getline(std::string&, int)'
WHY?
std::cin.getline() expects a char* buffer (docs)
std::getline() expects a std::string buffer (docs)
When you change the type, you have to change which function you call, too.
char buf[20] is not a string, it is a character array. If you terminate the array with a '\0' byte, then it can be said to be a c-string. Still not a std::string, though.
The function cin.getline() expects two parameters: a pointer to an array of characters and a count of how many characters the buffer supports - it then populates it with a c-string from cin.
There is no variant of cin.getline() which supports a std::string. For that, you need to use std::getline(iostream, string), e.g.
std::string line;
std::getline(std::cin, line);

no matching function for call to c++ string error

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*

How can I initialize a *char using user input?

Initializing a string in C# is as easy as this:
string str = Console.Read();
with this method, I don't need to know the size of the string which the user enters. But I cannot find a way like this in C++. I want my string to be defined as char *input, and I don't want to know the size of the string.
How can I achieve what I want?
Why not use C++'s string type?
#include <iostream>
#include <string>
int main() {
std::string foo;
std::cin >> foo;
std::cout << foo << "\n";
}
C++ has a string class which works much like C#'s string. So use it. :)
char* is not a string. It's just the closest you get if you're working in C.
So, #include <string>, and then use std::string instead of char*.
Use std::string and std::cin:
std::string str;
std::cin >> str;