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.
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);
}
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..
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.
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;
}
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;
}