how to print this vector

Ok so I'm ready a C++ book and in it, it says that you should use as little function arguments as possible. 0 is ideal, and 4 is too many, and thats what i'm trying to do, and only using them when necessary but i'm having a problem getting this last function to work. The user enters information and it pushes it back into the vector and then should output it when the program ends, but i'm confused as to how to get it to do that. I want it to output when i'm done entering strings so I cant put it in CollectDialogue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <vector>
#include <limits>

using namespace std;

void CollectDialogue(string getInput);
void PrintVector();

int main()
{
    cout << "enter in strings, type in exit to quit" << endl;

    string input;

    while(input != "Exit")
    {
        cin >> input;
        CollectDialogue(input);
    }

    PrintVector();

    return 0;
}

void CollectDialogue(string getInput)
{
    vector<string> getStrings;

    getStrings.push_back(getInput);
}

void PrintVector()
{
    for(int i = 0; i < print.size(); i++)
    {
        cout << print[i];
    }
}



UPDATE:

Here is an update of what i'm trying to do, i think its closer but idk:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <vector>
#include <limits>

using namespace std;

vector<string> CollectDialogue(string getInput);
void PrintVector();

int main()
{
    cout << "enter in strings, type in exit to quit" << endl;

    string input;

    vector<string> print;

    while(input != "Exit")
    {
        cin >> input;
        CollectDialogue(input);
    }

    PrintVector();

    return 0;
}

vector<string> CollectDialogue(string getInput)
{
    cout << "Enter CollectDialogue Function" << endl;

    vector<string> getStrings;

    getStrings.push_back(getInput);

    cout << "Vector Size" << getStrings.size() << endl;

    return getStrings;
}

void PrintVector()
{
    cout << "Enter PrintVector Function" << endl;

    string str;

    for(int i = 0; i < CollectDialogue(str).size(); i++)
    {
        cout << CollectDialogue(str)[i];
    }
}

void ClearVectorContents()
{

}
Last edited on
While I agree that functions should have few parameters, it is also true that functions that have no parameters and return nothing are often not very useful.

In your program I would probably start with something more like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <vector>

using namespace std;

void fillVector(vector<string>& values, const string& input);
void printVector(vector<string>& values);

int main()
{
    cout << "enter in strings, type in exit to quit" << endl;

    string input;

    vector<string> entered_strings;

    while(cin >> input && input != "Exit")
    {
        CollectDialogue(print, input);
    }

    printVector(entered_strings);

    return 0;
}


Notice that I'm passing the vector into the functions by reference/const reference so that changes made to the vector are reflected in the calling function.


Ok thanks, I got it working now, I forgot all about references. I have a few questions. Why is const reference used? I thought const was used when you dont want the value to change? and they cannot be altered by the program, how can it get different strings each time when its a const?

My second question is why do we need to create a vector in main just to fill space for the argument list, it seems like a waste of space to me, idk though. Something about it really bothers me when I have to create empty variables to fill in an argument list in another function, im not sure why, but maybe an explanation will help.
Last edited on
Why is const reference used? I thought const was used when you dont want the value to change? and they cannot be altered by the program, how can it get different strings each time when its a const?

The const parameter means that the variable can't be changed in that function, it doesn't mean that you can't pass different values into the function.

My second question is why do we need to create a vector in main just to fill space for the argument list, it seems like a waste of space to me, idk though.
Why would it waste space? The function is using the same vector, because you passed it by reference, not a separate vector. In your simple program it would be just as easy to just push() the element into your vector in main() instead of passing the string into a function.

Also the print() function should have a const parameter as well since a "print" function probably shouldn't modify the vector.

idk, it just seems weird that in main if I have a bunch of functions that take arguments and I have all these variables, so something like this for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
     int var1;
     int var2;
     int var3;
     string str1;
     string str2;
     string str3;

     function1(var1, str1);
     function2(var2, str2);
     function3(var3, str3);
}


im not sure why but that just seems strange to me even though its necessary. Maybe its because the variables here have no assigned values?
Topic archived. No new replies allowed.