Your function's declaration does not match its definition:
void changepassage (string,string&);
void changepassage (string passage,string word)
See the difference? In the declaration, the 2nd parameter is taking a string
by reference, but in the definition it is taking a string
by value instead. You need to fix the definition to also pass by reference.
And then, in main()
, the word
variable being passed in to that 2nd parameter does not match the declaration of that parameter, which is why you are getting a "no matching function" error. word
is a string[200]
array, not a single string
(or a string&
reference):
string word[200];
changepassage(passage,word);
You need to drop the [200]
:
string word;
changepassage(passage,word);
Also, inside the function itself, you have a store
variable:
int plength = passage.length();
string store[plength];
Aside from the fact that such array syntax is non-standard, you probably don't actually want an array holding plength
number of strings, but rather a single string
whose length can hold plength
number of characters. But even so, you are not actually saving anything into store
, so you should just get rid of it altogether.
You are also writing char
s to word
when it does not have space to hold them. You are not pre-sizing the word
ahead of time, so use of the []
operator is undefined behavior. You should use the string's +=
operator or push_back()
method instead:
word += temp[s];
word.push_back(temp[s]);
But, to be honest, I don't really know what your function is trying to do. It is a jumbled mess of nonsense and should be rewritten from scratch.
UPDATE: based on a comment you added:
I'm trying to assign each word in the passage to its own element in an array
You need to re-write this function. Try something more like this:
#include <iostream>
#include <string>
using namespace std;
size_t changepassage (const string&, string(&)[200]);
int main()
{
const string passage = "...";
string words[200];
size_t count = changepassage(passage, words);
for (size_t s = 0; s < count; ++s)
{
cout << words[s] << endl;
}
}
size_t changepassage (const string& passage, string (&words)[200])
{
if (passage.empty())
return 0;
string temp;
size_t s = 0;
string::size_type plength = passage.size();
for (string::size_type a = 0; a < plength; ++a)
{
if (isalpha(passage[a]) || (passage[a] != ' ' && passage[a] != '.' && passage[a] != ',' && passage[a] != ';'))
{
temp += passage[a];
}
else if (ispunct(passage[a]) || isspace(passage[a]))
{
words[s] = temp;
if (++s == 200) return s;
temp = "";
}
}
if (!temp.empty() && s < 200)
{
words[s] = temp;
++s;
}
return s;
}
Or:
#include <iostream>
#include <string>
using namespace std;
size_t changepassage (const string&, string*, size_t);
int main()
{
const string passage = "...";
string words[200];
size_t count = changepassage(passage, words, 200);
for (size_t s = 0; s < count; ++s)
{
cout << words[s] << endl;
}
}
size_t changepassage (const string& passage, string *words, size_t max_words)
{
if (passage.empty() || !words || max_words == 0)
return 0;
string temp;
size_t s = 0;
string::size_type plength = passage.size();
for (string::size_type a = 0; a < plength; ++a)
{
if (isalpha(passage[a]) || (passage[a] != ' ' && passage[a] != '.' && passage[a] != ',' && passage[a] != ';'))
{
temp += passage[a];
}
else if (ispunct(passage[a]) || isspace(passage[a]))
{
words[s] = temp;
if (++s == max_words) return s;
temp = "";
}
}
if (!temp.empty() && s < max_words)
{
words[s] = temp;
++s;
}
return s;
}
Or, if your instructor will allow it, use std::vector
instead:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void changepassage (const string&, vector<string>&);
int main()
{
const string passage = "...";
vector<string> words;
changepassage(passage, words);
for (const auto& s : words)
{
cout << s << endl;
}
}
void changepassage (const string& passage, vector<string>& words)
{
string temp;
string::size_type plength = passage.size();
for (int a = 0; a < plength; ++a)
{
if (isalpha(passage[a]) || (passage[a] != ' ' && passage[a] != '.' && passage[a] != ',' && passage[a] != ';'))
{
temp += passage[a];
}
else if (ispunct(passage[a]) || isspace(passage[a]))
{
words.push_back(temp);
temp = "";
}
}
if (!temp.empty())
words.push_back(temp);
}