I am wondering if it is possible to do multiple return values from another function. So far i'v not seen any more than one being called (e.g return value; ) at the end of a function being called. It is possible to do multiple returns throughout a function? I'll give an example what I mean. Anything in red isn't part of my code, but just an idea of how it would work.
No you cannot return more than one thing from a function. Neither can you, as other people have put it, return an array. What you do with arrays is pass them in, like this:
Code:
int menu (int *myarray, size_t size);
int myarray[] = { 0, /*principal */, 0 /* term */, 0 /* rate */ };
menu(myarray, 3);
Then menu would change the three values and when it returns those values are available in main(). The truth is, the variables never really left main() like they would at other times. You changed the variables through a pointer. You could also use multiple pointers for month, rate, and principle if you wanted.
But I think menu() has more problems than that.
Looking here:
Code:
switch (choice)
{
case 'a':
printf("What is the principal you wish to enter?:\n");
principal = getchar();
return principal;
break;
case 'b':
printf("What is the annual interest rate?:\n");
rate = getchar();
return rate;
break;
case 'c':
printf("How many months in which does the loan need to be paid?:\n");
months = getchar();
return months;
I see that you're asking the user for input on his loan, but none of that data is being read exactly. It's highly unlikely that the loan data will fit in the integer that getchar returns. Read here:
Return Value
fgetc(), getc() and getchar() return the character read as an unsigned char cast to an int or EOF on end of file or error.
So unless the amounts are always ridiculously small... you know what, let's just admit you need numbers.
Additionally, I think menu is responsible for far too much, or is at least poorly named. It should probably call at least one other function to calculate the loans and print them. I hope you're going in that direction and not trying to do everything in one function.
HTH
EDIT: You'll have to excuse me, it takes time to write posts, and sometimes things move along without me.
I just need to know how to manually add a value into each part of an array (i.e principal into array[0], rates into array[1] and months into array[2]. Any idea how to do that, if you can (which I assume you can)?
You would read input into a normal variable first and then do something like:
Code:
array[0] = principal;
array[1] = rate;
array[2] = months;
whiteflags.