started learning void function, got a problem

Hi, i'm new to programming, just started 3 months ago, and I got an issue here. Sounds like an easy task, but I just can not find how to fix this program so it can work properly. I am learning on my own, because in school we didn't yet started programing, so I would apprecciate your help. I think my algorythm is okay, just missed something with void. Getting to the point, this task asks me to find: 1)how many days in all did they work ,2)how many potatoes were digged in all, 3) how many helpers worked in all. In function void i need to return 1)how many days in all did they work, 2)how many potatoes were digged in all, 3)how many potatoes did on average dig one helper per day.
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

#include <iostream>
#include <iomanip>

using namespace std;

void Counting (double potatoes, int helper, int & days, 
                  int & all_helpers, double & all_potatoes, 
                  double & average);

int main ()
{
   double potatoes ,average, all_potatoes=0; 
   int helper, days=0, all_helpers=0; 

   Counting (potatoes, helper, days, all_helpers, all_potatoes, average);
   
   cout<<days<<endl;
   cout<<fixed<<setprecision(2)<<potatoes<<endl;
   cout<<fixed<<setprecision(2)<<average<<endl;
   return 0;
}

void Counting (double potatoes, int helper, int & days, 
                  int & all_helpers, double & all_potatoes, 
                  double & average);
{
 while ((potatoes!=0) || (helper!=0))
{
cin>>potatoes>>helper;
if (potatoes==0 || helper==0) break;
days++;
all_potatoes+=potatoes;
all_helpers+=helper;
}
average=all_potatoes/all_helpers;
}
In function void i need to return


A void function can't return anything, that's why it's called void.

It can calculate, and print the results, even put those values in a global variable but you can't return those values to main().
semi-colon mistake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Counting (double potatoes, int helper, int & days, 
                  int & all_helpers, double & all_potatoes, 
                  double & average); // donot use semi-colon
{
 while ((potatoes!=0) || (helper!=0))
{
cin>>potatoes>>helper;
if (potatoes==0 || helper==0) break;
days++;
all_potatoes+=potatoes;
all_helpers+=helper;
}
average=all_potatoes/all_helpers;
}


use this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Counting (double potatoes, int helper, int & days, 
                  int & all_helpers, double & all_potatoes, 
                  double & average)
{
 while ((potatoes!=0) || (helper!=0))
{
cin>>potatoes>>helper;
if (potatoes==0 || helper==0) break;
days++;
all_potatoes+=potatoes;
all_helpers+=helper;
}
average=all_potatoes/all_helpers;
}


Last edited on
Thanks bird1234 my mistake, but it still does not work properly. :(

SamuelAdams, thanks, so i can not use "cin" in void function? But i dont know then how to find "average" as "days","all_potatoes" and "all_helpers" should be in the while loop (i forgot to mention, that the program ends when i either enter the potatoes or helpers 0) and the "average" i should count after the while loop.. i just got all messed up here..

so it should look something like this?

#include <iostream>
#include <iomanip>

using namespace std;

void Counting (double potatoes, int helper, int & days,
int & all_helpers, double & all_potatoes,
double & average);

int main ()
{
double potatoes ,average, all_potatoes=0;
int helper, days=0, all_helpers=0;

while ((potatoes!=0) || (helper!=0))
{
cin>>potatoes>>helper;
if (potatoes==0 || helper==0) break;
Counting (potatoes, helper, days, all_helpers, all_potatoes, average);
}


cout<<days<<endl;
cout<<fixed<<setprecision(2)<<potatoes<<endl;
cout<<fixed<<setprecision(2)<<average<<endl;
return 0;
}

void Counting (double potatoes, int helper, int & days,
int & all_helpers, double & all_potatoes,
double & average)
{
{
days++;
all_potatoes+=potatoes;
all_helpers+=helper;
}
average=all_potatoes/all_helpers;
}

Last edited on
Hmm let me see
remove the curly braces between line no 33 and 37 they are not doing anything
i have modified your program now it is working

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
#include <iostream>
#include <iomanip>

using namespace std;

void Counting (double potatoes, int helper, int & days, 
int & all_helpers, double & all_potatoes, 
double & average);

int main ()
{
double potatoes ,average, all_potatoes=0; 
int helper, days=0, all_helpers=0; 


cin>>potatoes>>helper;

while ((potatoes!=0) || (helper!=0))
{
cin>>potatoes>>helper;
if (potatoes==0 || helper==0) break;
Counting (potatoes, helper, days, all_helpers, all_potatoes, average);
}


cout<<days<<endl;
cout<<fixed<<setprecision(2)<<potatoes<<endl;
cout<<fixed<<setprecision(2)<<average<<endl;
return 0;
}

void Counting (double potatoes, int helper, int & days, 
int & all_helpers, double & all_potatoes, 
double & average)
{
days++;
all_potatoes+=potatoes;
all_helpers+=helper;
average=all_potatoes/all_helpers;
}



Output

12
20
12
20
0
0
2
0.00
0.60

Last edited on
I appreciate your help ,but still something is not working. For example, i enter:

50.5 3

30.5 1

0 5

and it should print:

2

81.00

20.25
thats how it looks without void :

#include <iostream>
#include <iomanip>

using namespace std;
int main ()
{
double potatoes , average, all_potatoes=0;
int helper, days=0, all_helpers=0;

while ((potatoes!=0) || (helper!=0))
{
cin>>potatoes>>helper;
if (potatoes==0 || helper==0) break;
days++;
all_potatoes+=potatoes;
all_helpers+=helper;
}
average=all_potatoes/all_helpers;

cout<<days<<endl;
cout<<fixed<<setprecision(2)<<all_potatoes<<endl;
cout<<fixed<<setprecision(2)<<average<<endl;
return 0;
}

It works all fine, I just struggle how to make it with void.
Last edited on
You can do this
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
#include <iostream>
#include <iomanip>
void func ();
using namespace std;
int main ()
{
func ();
return 0;
}
void func (){

double potatoes , average, all_potatoes=0;
int helper, days=0, all_helpers=0;

while ((potatoes!=0) || (helper!=0))
{
cin>>potatoes>>helper;
if (potatoes==0 || helper==0) break;
days++;
all_potatoes+=potatoes;
all_helpers+=helper;
}
average=all_potatoes/all_helpers;

cout<<days<<endl;
cout<<fixed<<setprecision(2)<<all_potatoes<<endl;
cout<<fixed<<setprecision(2)<<average<<endl;
}
[Edit]
Looks like you are actually in the pass-by-ref chapter, I didn't read your code close enough, sorry.
Last edited on
My task says to use

void Counting (double potatoes, int helper, int & days,
int & all_helpers, double & all_potatoes,
double & average);

and return in void function days, all_potatoes and the average. I am sorry, but that is just what my tasks requires. I have a headache because of this for the whole week..
Last edited on
It's good practise to always initialise your variables before you use them, especially before sending to functions. Your original code had warnings to that effect. I used cpp.sh - the gear icon top right of the code with all 3 warning options on.

Also, declare and initialise all the variables 1 per line - it will save you from hard to find errors.

Edit: A function should conceptually do one thing only. At the moment your function is getting input and doing calculations.

Good Luck !!
Last edited on
Thanks a lot! So the problem is with initialization. So i need to initialize "potatoes" , "helpers" and "average" ? And i should not use cin in the void function?
It should look something like this?

void Counting (double potatoes, int helper, int & days,
int & all_helpers, double & all_potatoes,
double & average)
{
{
double p=potatoes;
int m=helper;
days++;
all_potatoes+=p;
all_helpers+=m;
}
double a=average;
a=all_potatoes/all_helpers;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Counting (double potatoes, int helper, int & days, 
int & all_helpers, double & all_potatoes, 
double & average)
{
{ // why you are using this 
double p=potatoes;
int m=helper;
days++;
all_potatoes+=p;
all_helpers+=m;
} // and this bracket
double a=average;
a=all_potatoes/all_helpers;
}


Last edited on
now it is displaying what result you want

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 <iomanip>

using namespace std;

void Counting(double potatoes, int helper, int & days,
	int & all_helpers, double & all_potatoes,
	double & average);

int main()
{
	double potatoes=1.0, average=0.0, all_potatoes = 0.0;
	int helper = 1, days = 0, all_helpers = 0;
             
	while (true)
	{
		cin >> potatoes >> helper;
		if (potatoes == 0 || helper == 0)
			break;
		else
		Counting(potatoes, helper, days, all_helpers, all_potatoes, average);
	}


	cout << days << endl;
	cout << fixed << setprecision(2) << all_potatoes << endl; // see your program 
        // you are using potatoes
	cout << fixed << setprecision(2) << average << endl;
	system("pause");
}

void Counting(double potatoes, int helper, int & days,
	int & all_helpers, double & all_potatoes,
	double & average)
{
	days++;
	all_potatoes += potatoes;
	all_helpers += helper;
	average = all_potatoes / all_helpers;
}


expected output


50.5 3
30.5 1
0 5
2
81.00
20.25





Wow, thanks a million! :)
Topic archived. No new replies allowed.