how do i write a function

hi,
im new in c++, so i dont know much.

how would i write the following into a function?

1.
1
2
3
4
5
	srand(time(0));
	vector<int> vList;
	int size = vList.size();
	for (int i = 0; i <= rand() % 10 + 1; i++)
		vList.push_back(rand() % 10);



2.
1
2
3
4
	cout << "i    vector" << endl << "--------------" << endl;
	for (int i = 0; i < vList.size(); i++)
		cout << i << "\t" << vList[i] << endl;



3.
1
2
3
4
string input;

	cout << "Enter a sentence of up to 100 characters: ";
	getline(cin, input, '.');



thanks in advance!
Last edited on
hi keskiverto
thanks for your reply.
the article is helpful for the string part. i have to dig more into it.
the thing, though, is that it doesn't say anything about vectors. i reckon it's different from simple void and return type functions?

UPDATE:
this is what i tried for the string:

1
2
3
4
5
6
7
string userInput(string input)
{
cout << "Enter a sentence of up to 100 characters: ";
getline(cin, input, '.');
return input;

}


while above code prints out okay on the console, it doesn't work with the rest of the main program:

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

int main()
{

	string input;

	userInput(input);

	input[0] = toupper(input[0]);

	for (int i = 0; i < input.length(); i++)
	{
		if (input[i] == ' ' || input[i] == '\n')
		{
			newInput += ' ';
			i++;
			while (input[i] == ' ')
				i++;
			i--;
		}
		else
		{
			newInput += input[i];
		}
	}
	cout << newInput << endl;
}
Last edited on
closed account (367kGNh0)
Once you open the curly brackets,just about anything can go in them,
From here we have some difficulty in figuring out what you need the function to do, so I'm making some assumptions here that may be in error.

For example, this block:

1
2
3
4
5
        srand(time(0));
   	vector<int> vList;
	int size = vList.size();
	for (int i = 0; i <= rand() % 10 + 1; i++)
		vList.push_back(rand() % 10);


It seems you intend to make a function that creates vector<int> of random size, filled with random entries.

There is a minor problem before we can build the function, though. The for loop has a test that itself is going to choose random values at each loop. While this may well work, it is an odd formation. Typically one would choose the random value before the loop once, and use that for the loop's termination clause.

To be clear, in the for loop the clause " i <= rand() % 10 + 1" creates a new random value at each loop. Now, it is true that this produces random results, but this works better this way:

1
2
3
4
5
6
        srand(time(0));
   	vector<int> vList;
        int limit = rand() % 10 + 1;
        vList.reserve( limit + 1 );
	for (int i = 0; i <= limit; i++)
		vList.push_back(rand() % 10);


In this version, I omit the "int size" statement. It would always be zero for a new vector, and isn't used elsewhere.

Instead, the limit is calculated in advance, used as the loop's termination clause, and as a bonus it allows the reserve of space in the vector before the loop which improves efficiency (well, as habit, when the volume is larger in future work).

So, to make a function of this you must decide if you want to return a vector, or if you want the caller to provide an empty vector. Returning a vector works, like any other value, but the implication is that the vector is copied. Modern compilers make this issue evaporate through optimization, but generally it's a good idea for large containers to be given as parameters. Something like:


1
2
3
4
5
6
7
8
void make_rand_vec( vector<int> & vList )
{
        srand(time(0));
        int limit = rand() % 10 + 1;
        vList.reserve( limit + 1 );
	for (int i = 0; i <= limit; i++)
		vList.push_back(rand() % 10);
}


This version assume the vector<int> is created by the caller, provided to the function, and due to the & (a reference) is not a copy. There's a minor problem, though, which we can turn into a potential benefit. vList.reserve assumes the vector is empty. Is that required? If so, we may need to empty the vList before the reserve. On the other hand, if you'd like this function to add more integers to a vector<int> that is NOT empty, then the reserve may need to be altered or removed. Perhaps "vList.reserve( vList.size() + limit + 1 )".

These decisions are part of your design choice.

You may also want to return bool, which is merely true if the function has no trouble, false if there's an error.

Apply that thinking to the other blocks and let's see how far you get.

it doesn't work with the rest of the main program

Read the tutorial more carefully.

It's first example contains:
1
2
3
4
5
6
int main ()
{
  int z;
  z = addition (5,3);
  cout << "The result is " << z;
}

You wrote (essentially):
1
2
3
4
5
6
int main ()
{
  int z;
  addition (5,3); // "does not work with the rest"
  cout << "The result is " << z;
}
thanks guys!!! all these things are super helpful!!
my homework is now much more optimized
Topic archived. No new replies allowed.