function

hi guys,
i'm trying to develop a program for a restaurant where you can log in and order food and a invoice is generated. I'm having a problem how to return the total to the invoice after the calculations since i just started learning c++ just for one month and this is our assignment.

the following is the invoice an I have to add the total.

void invoice()
{
system("cls");

char date[9];

_strdate_s(date);

cout<<"\tDelicious Food Planet Restuarant\a\a\a\a\a"<<endl;
cout<<"\n";
cout<<"Invoice ID : "<<invoiceid<<"\t Date : "<< date <<endl;
cout<<"Customer Name : "<<name<<endl;
cout<<"\n";
cout<<"\n";
cout<<"***Thank You For Your Purchase***"<<endl;
cout<<"***Come Again Soon***"<<endl;

system("pause");

}

here is the a one of the six functions that i have created. I have one function for each food item so I have to get the total of all the six and add the grand total in the invoice.

void chknbur()
{
int chickenburger = 5;
int quantity;
string cus;

cout<<"Enter Quantity Required - "<<endl;
cin>>quantity;

int total;
total = quantity*chickenburger;

cout<<"Total price : "<<total<<endl;

cout<<"Do you wont to Order anymore? y/n"<<endl;
cin>>cus;
if(cus == "y")
{
menu();
}
else
{
invoice();

}
}

so i wont to know how to return the total of the above function to the invoice.

Thanks.
Last edited on
Hello, aazir.

Not related to the topic, but I have to point that it is "want" not "wont".

From what I guess what you want is not returning value.

You want to pass value to invoice function.

============================================


1
2
3
4
void Sum( int a, int b )
{
  std::cout << a + b;
}


The function above takes two integer value as parameter and prints the result. You can use it like this:

1
2
3
int whatever = 3;
int something = 5;
Sum(whatever, something);

You are passing whatever and something to Sum function in this case.




============================================




1
2
3
4
int GetOne( void )
{
  return 1;
}


The function above takes no parameter and returns integer value. In this case, the result is 1. You can use it like this:

1
2
3
4
int myVar = GetOne();

// result of the code above is same as doing this:
// int myVar = 1;  




============================================




Now following function takes two integer and returns one integer value, which is the sum of two integers given:

1
2
3
4
int GetSum( int a, int b )
{
  return a + b;
}



Hope it helped.
Last edited on
since total is int

change invoice to void invoice(int total)
then call it in the chickenburger function like this: invoice(total)

kg1992 Thanks.

Darkmaster I tried that but it gives me an error. I am almost done but i just want to know how do I only return the calculated total. So in the invoice the total of the chicken burger can be showed.
do you know the difference between return and a parameter of a function?

i think you are mixing stuff up here
What kg1992 has explained about is passing parameters and returning the total of that. What i wont to do here is just to send the total to the invoice. I am a bit confused because we were just taught of the basic for a month.
your function can return something, for example an integer value:
1
2
3
4
int func()
{
   return 1;
}


it can also use parameters, that are used in certain tasks, for example calculating the sum of 2 intergers:
1
2
3
4
void func(int a, int b, int &sum)
{
   sum=a+b;
}


and you can combine returning and using parameter:
1
2
3
4
5
int func(int a, int b)
{
   int sum = a+b;
   return sum;
}


hope that helps you a little
thanks
Topic archived. No new replies allowed.