Void functions question

I'm working on a program assignment and the instructor wants us to use void functions. I'm not understanding the reason these are used anywhere. Wouldn't it just be easier for me to declare variables and cout those? I'm working with arrays. Does using void with arrays have any benefits to not using it?
Void function as in functions that take nothing or return nothing?

Functions are generally created for one of three purposes:
1. When there's a bit of code that shows up often, making a function out of that code will reduce the room for error and file sizes.
2. To split up a program into pieces that improve readability and can be split over multiple files if necessary.
3. When creating a library.

As for your arrays question, could you please elaborate?

-Albatross
I want to get three variables from the user and put them into three arrays and then display them, but for the life of me I cant get this code to build.
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
#include <iostream>
#include <string>

using namespace std;

const int ARSIZE = 400;

void printArrays (int levelsArray[], int scoresArray[], int starsArray[], int i);

int main()
{


	int levelsArray[ARSIZE];
	int scoresArray[ARSIZE];
	int starsArray[ARSIZE];
	int i;

	cout << "Please enter the level number (-999 to quit): ";
		cin >> i;


	while(i != -999)
	{
		i=ARSIZE;
	

		cout << "Please enter the score: ";
		cin >> scoresArray[i];

		cout << "Please enter the number of stars: ";
		cin >> starsArray[i];
		
		cout << "Please enter the level number (-999 to quit): ";
		cin >> i;
		
		if (i == -999)
		break;
		}
return ARSIZE; 

printArrays (levelsArray[], scoresArray[], starsArray[]);

{
void printArrays (int levelsArray[], int scoresArray[], int starsArray[], int i)

cout << "Level" << "	" << "Score" << "	" << "Stars" << endl;
cout << levelsArray[ARSIZE] << "	" << scoresArray[ARSIZE] << "	" << starsArray[ARSIZE];
}

		
Last edited on
Wait a minute. I just realized this was a continuation of this thread:
http://www.cplusplus.com/forum/general/116087/

The brackets on void functions are no different from other functions, meaning that your change you made messed it up further.

Weren't you supposed to read into levelsArray on lines 20 and 35?

You're still missing a closing bracket on line 43.

You need to get rid of the []s on line 42.

You still haven't switched lines 40 and 42.

Line 25 is still wrong. i should start at zero and go up with each iteration of the loop. Similarly, on line 25, you need to check that i is less than ARSIZE or you'll have to face out-of-bounds issues. Finally, I think you were meant to return i instead of ARSIZE.

On line 48, you need to iterate over each element of the loop. I'm guessing you already learned about for loops? This is where they come in handy.

Code snippet:
1
2
3
for(int i = 0; i < array_size; ++i)
    doSomethingToElementOfAnArray(array[i]);
doSomethingToTheWholeArray(array);


Good luck.

-Albatross
I think I've implemented all the changes but it's still not working. Anything else you notice I can change?
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
#include <iostream>
#include <string>

using namespace std;

const int ARSIZE = 400;

void printArrays (int levelsArray[], int scoresArray[], int starsArray[], int i);

int main()
{


	int levelsArray[ARSIZE];
	int scoresArray[ARSIZE];
	int starsArray[ARSIZE];
	int i;

	cout << "Please enter the level number (-999 to quit): ";
		cin >> levelsArray;


	while(i != -999)
	{
		i=0; i<ARSIZE; i++; 
	

		cout << "Please enter the score: ";
		cin >> scoresArray[i];

		cout << "Please enter the number of stars: ";
		cin >> starsArray[i];
		
		cout << "Please enter the level number (-999 to quit): ";
		cin >> levelsArray;
		
		if (i == -999)
		break;
		}

printArrays (levelsArray, scoresArray, starsArray);

return i; 

{
void printArrays (int levelsArray[], int scoresArray[], int starsArray[], int i)
cout << "Level" << "	" << "Score" << "	" << "Stars" << endl;

for(int i = 0; i < ARSIZE; ++i)
    cout << levelsArray[i] <<"	  " << scoresArray[ARSIZE] << "		" << starsArray[ARSIZE];
}
}

		
Functions need to be defined outside of other functions (lines 45-51), and your brackets are still wrong.

Line 25: This isn't a loop. :P You probably want to change your entire while loop to a for loop.

Line 50: Okay, so far so good. levelsArray wasn't the only array you wanted to iterate over, right?

Here are two articles, one on functions, the other on arrays. Let me know if these help, alright?
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/arrays/

-Albatross
Thanks for being so patient with me. Programming is kicking my butt this semester. I used for(i=0; i<ARSIZE; i++) to start my for loop. for line 50, I want to iterate over all three arrays so my chart has three columns.

The function article really helps but for arrays, how do I code them if I don't know how many levels the user is going to enter? I know I'm using ARSIZE for that variable but I don't understand how the computer will process that and form the end chart.
The start of the for loop bit sounds about right. If you kept the break condition at the end of what used to be your while loop, then if not for line 35 (what's different on this line compared to the previous two cin statements?), the loop should be alright.

Well, as for what you want to do regarding iterating over all three arrays at the same time, what's stopping ya from using i to sequentially access elements of those arrays too?

When you use ARSIZE, you're making an assumption that the user will want to enter no more than 400 values. For now, this is a fair assumption. When, in the future, you want the size of your array to be customizable at runtime, then you can use either the std::vector template or dynamic memory using new and delete. BUT! One thing at a time. Walk before you can run. :)

-Albatross
Topic archived. No new replies allowed.