Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am getting an complier error, that I cannot for the life of me figure out. Err

ID: 3778090 • Letter: I

Question

I am getting an complier error, that I cannot for the life of me figure out.

Error:

Error   C2664   'void std::vector<std::string,std::allocator<_Ty>>::push_back(const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)': cannot convert argument 1 from 'char' to 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> &&'  

My code:

void ReadSentence(vector<string> &Sentence)
{
   string str;
   cout << "Enter a sentence terminated by a '.' ";
   getline(cin, str);

   for (int i = 0; i < str.size() - 1; i++)
   {
       Sentence.push_back(str[i]);
   }
}

Thanks in advance.

Explanation / Answer

Hi,

I have fixed the issue. Please find the below updated code and highlighted the code changes below.

void ReadSentence(vector<char> & Sentence)
{
string str;
cout << "Enter a sentence terminated by a '.' ";
getline(cin, str);
for (int i = 0; i < str.length() - 1; i++)
{
Sentence.push_back(str[i]);
}
}