function help

Im just started learning function coding.

Complete the print_total() function below.

1
2
3
4
 void_print_total(int price)
{
//Add your code here...
}


The fucntion should total the price passed from the caller function and print the result in one line everytime it is called according to the format below.

Price of item 1 is 15 and total is 15
Price of item 2 is 21 and total is 36
Price of item 3 is 14 and total is 50


Anyone can help me ? i have no idea how to start with this kind of questions.
Last edited on
All you need is a variable called total then you can do something like this total+= price;

The above code means total = total + price

And use cout to output messages to the screen.

You should also make the function an int so it can return the total.

If you dont understand anything, let me know
Last edited on
What about the item identifiers 1, 2, 3 ...
Ahh well just make the function a void in that case
Can it be done through one function returning void and taking one int argument?
1
2
3
4
5
6
void_print_total(int price)
{
int total=0;
total+=price;
return price;
}



something like this ?
Maybe like this:
1
2
3
4
5
6
void print_total(int num, int price, int & total)
{
  total += price;
  cout << "Price of item " << num << "is " 
       << price << " and total is " << total;
}
Last edited on
Can it be done through one function returning void and taking one int argument?

Yes.

Indeed it must be done that way - that's what the original question asks. Changing the function signature (parameter list or return type) is to invent one's own question and answer that instead.
But how Chervil, please show us. I've been banging my head on this for the past hr
According to OP:
The fucntion (sic) should total the price passed from the caller function and print the result in one line everytime it is called according to the format below

I'm not sure all of these can be achieved with the (partial) function signature supplied - OP actually does not specify a return type though the name of the function suggests it might be void
Last edited on
Can it be done through one function returning void and taking one int argument?

Yes, with global variables or with a static var for total and item number.
OP actually does not specify a return type though the name of the function suggests it might be void
Yes - that worries me too. I've assumed it was some sort of typo - but making assumptions can be dangerous.

Still, I came up with two alternatives:
1
2
3
4
5
6
7
8
9
10
void print_total(int price)
{
    static int count = 0;
    static int total = 0;
    
    ++count;
    total += price;
    std::cout << "Price of item " << count << " is " << price 
              << " and total is " << total << std::endl;     
}


Or alternatively (and this might not be permitted)
1
2
3
4
5
6
7
8
9
10
int global_count = 0;
int global_total = 0;

void print_total(int price)
{   
    ++global_count;
    global_total += price;
    std::cout << "Price of item " << global_count << " is " << price 
              << " and total is " << global_total << std::endl;     
}


Though the second version may breach the terms of the original question, it does have the advantage that the global variables can be reset if necessary.
Bravo Chervil!
DesmondLee

we cannot use return in void function
Last edited on
@bird1234 oh yea.. my mistake. Thanks for pointing out! :D
@Chervil Thanks a lot.
Topic archived. No new replies allowed.