If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Sending Data back to Main()

I am doing a homework assignment (I know there are a lot of those questions here). I am creating a program that takes input from a file and outputs various items in the form of a restaurant invoice to the screen, a file, as well as an error file. I wrote the whole thing and it works. However, now the teacher tells me that she wants Main() to control everything. Meaning, she wants each function to report back to Main() and Main() to call the next function.

Obviously, this is not ALL the code, just what applies to my question.

So here are the real questions:

1) The CalculateBill() function calls the PrintResults() function. How do I get it to send data back to Main() and allow Main() to call the PrintResults() function instead?

I could use structures, but that will require me to re-write the entire thing and then I will not understand my own code as structures are very new to me. Also, structures have not been taught in class yet and I do not want to go too far outside of what is being taught.

Is there a way to do this w/o using structures or a ton of global variables?

Re: Sending Data back to Main()

You can return multiple values from a function either by wrapping them up in a class or struct and returning an instance of that class or struct, or by passing them in as output parameters. That's a lot of values to pass into a function though. Typically in a case like this you'd create a class that represents the bill. If you haven't got that far yet, you may want to consider globals too.

Re: Sending Data back to Main()

Is there a way to do this w/o using structures or a ton of global variables?

Use pass by reference rather than pass by value so that the function can return data to the calling routine as part of its arguments.

Any parameter of PrintResults is going to have to be defined at global/main level rather then in CalculateBill. Any variable of CalculateBill that has to be passed to PrintResults will need to be passed to CalculateBill by reference rather than by value. With this number of parameters, it would be so much easier using a struct/class but I understand why you don't want to.

All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Re: Sending Data back to Main()

The problem with that is that the variables do not exist globally or in Main(),

Yes, at the moment. But to fullfil your teacher requirements you now need to define them either in main() and pass by reference to CalculateBill and by value to PrintResults - or globally so you don't need to pass them as parameters at all. If you don't want to use a struct/class then this is your only option.

All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Re: Sending Data back to Main()

Yes, after talking to my teacher, I found that the easiest way to accomplish this was to not use one function to calculate the invoice. Instead, use individual float functions that return pieces of the invoice. that way I can just send the variables to the print function.