Reputation: 3
Basically, I want my function stringFeatures to output the size and capacity of variables text
, text1
and text2
. I'm trying to assign text
, text1
and text2
separately to the variable to make the code less confusing but I haven't had any success. How would I go about this? Thank you
#include <iostream>
#include <string>
using namespace std;
int main() {
string text = "Banana";
string text1 = "Orange";
string text2 = "Grapes";
string variable = text + text1 + text2;
void stringFeatures (string variable);
{
cout << "Size: " << variable.size();
cout << "Capacity: " << variable.capacity();
}
cout << variable << endl;
}
I expect the output to be the size and capacity of each string separately but I don't know how to assign each variable separately, I've just combined all my variables altogether instead as shown in the string variable
because I don't know what to do
Upvotes: 0
Views: 1060
Reputation: 544
Like this?
void MyFunction(string variable) {
cout << variable.size();
cout << variable.capacity();
}
int main() {
string text1 = "Banana";
string text2 = "Orange";
string text3 = "Grapes";
MyFunction(text1);
MyFunction(text2);
MyFunction(text3);
}
Upvotes: 1
Reputation: 390
If I understand correctly, you want to call the function 3 times passing the variable that you want to show its capacity and size. Take a look at the code bellow :
#include <iostream>
#include <string>
using namespace std;
void stringFeatures(string variable)
{
cout << "Size: " << variable.size();
cout << "Capacity: " << variable.capacity();
}
int main()
{
string text = "Banana";
string text1 = "Orange";
string text2 = "Grapes";
stringFeatures(text);
stringFeatures(text1);
stringFeatures(text2);
}
Upvotes: 1
Reputation: 63174
I have fixed and annotated your code such that it works as you intend:
#include <iostream>
#include <string>
// That's the function's definition (body), it should not be inside another function
void stringFeatures (std::string variable) // <-- no ; in a function definition
{
std::cout << "Size: " << variable.size();
std::cout << " Capacity: " << variable.capacity();
std::cout << "\n"; // Additional newline for clarity
}
int main() {
std::string text = "Banana";
std::string text1 = "Orange";
std::string text2 = "Grapes";
// Call the function with each string
stringFeatures(text);
stringFeatures(text1);
stringFeatures(text2);
}
Note that I also removed using namespace std;
, see this Q&A as to why it's a bad habit.
But it's also interesting to know what was actually happening in your code:
#include <iostream>
#include <string>
using namespace std;
int main() {
string text = "Banana";
string text1 = "Orange";
string text2 = "Grapes";
string variable = text + text1 + text2;
// This declares a stringFeatures function that is not used at all
void stringFeatures (string variable);
// This is completely separate from the function declaration above,
// which ended at the semicolon.
// It's a plain code block which only acts on the local variable `variable`.
{
cout << "Size: " << variable.size();
cout << "Capacity: " << variable.capacity();
}
cout << variable << endl;
}
Upvotes: 4