0

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

Quentin
  • 62,093
  • 7
  • 131
  • 191
  • 1
    You can call the function 3 times, each time passing a different parameter: `stringFeatures(text);stringFeatures(text1);stringFeatures(text2);` – 001 Jul 02 '19 at 14:07
  • 2
    Also, you can't define a function inside of another function. – 001 Jul 02 '19 at 14:08
  • Thanks but would it let me do that? I got a little confused because I thought it'd be printing variable each time and was trying to set each string equal to the variable – Sophie McKeown-Dillon Jul 02 '19 at 14:08
  • @JohnnyMopp - "you can't define a function inside of another function". To be pedantic; yes you can: option one would be to rely on a gcc extension which allows it (at least for C, not sure about C++). But that's not portable, so a bad option. Another option is to use a lambda: `void f() { const auto nested = []() { return true; }; if (nested()) { /* ... */ } }` . – Jesper Juhl Jul 02 '19 at 14:27

3 Answers3

4

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;
}
Quentin
  • 62,093
  • 7
  • 131
  • 191
  • Thank you! I'm just getting a little confused over one thing. When I call the function with each string, I've used to check for variable size and capacity. When I call it with text and text1, does the program recognise text and text1 as "variable"? – Sophie McKeown-Dillon Jul 02 '19 at 14:19
  • @SophieMcKeown-Dillon almost. `variable` is the function's *parameter*, which is initialized upon calling with what you've provided as *argument* (`text` in the first call). As such, `stringFeatures`'s `variable` inside its first call is a copy of `main`'s `text`. Heads-up for further research: this changes if you use a reference parameter (`void stringFeatures(std::string &variable)`), in which case `variable` will *refer to* (be an alias for) `text` directly. – Quentin Jul 02 '19 at 14:22
  • Oh right, thanks! i sort of understand it, so does it kind of just recognise the size of what I've provided as an argument? Where I'm kind of struggling is why I've put variable.size() e.g could I put any working name .size() and it'd recognise it when the function is called for? – Sophie McKeown-Dillon Jul 02 '19 at 14:26
  • @SophieMcKeown-Dillon I'm not sure I understand your question, but: aside from being initialized upon calling, a function's parameters are just regular variables. You can name them however you want, and they only exist inside the function. – Quentin Jul 02 '19 at 14:29
  • Sorry, what I mean is, in my code, I've declared variable inside the function. I understand what you explained, but I was thinking the function recognised the size and capacity of "variable" when it was called, rather than text, text1, etc. e.g i was thinking I had to type text.size(); and was confused how the program would recognise variable's size as text's size. – Sophie McKeown-Dillon Jul 02 '19 at 14:33
  • @SophieMcKeown-Dillon you've got it right all the way through, the only missing piece is that `variable` begins its life as a copy of `text` because that's what you called the function with. Thus its features are indeed the same as `text`'s. – Quentin Jul 02 '19 at 14:36
1

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);  
}
YouneS
  • 390
  • 4
  • 10
1

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);
}
kebbaben
  • 444
  • 3
  • 14
  • 2
    Aside from the typo (`M` -> `;`), your answer looks like it is defining `MyFunction` inside `main`, which is not allowed. – Quentin Jul 02 '19 at 14:18
  • @Quentin Yes you are certainly right about that. Fixed. – kebbaben Jul 03 '19 at 06:22
  • But still the function is not declared when you try to call it -- I recommend [Coliru](http://coliru.stacked-crooked.com/) or [Wandbox](https://wandbox.org/) to compile and test code on the go ;) – Quentin Jul 03 '19 at 08:00