![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Aug 2005
Location: Orlando
Posts: 11
Rep Power: 0
![]() |
function help for a beginner
Hey all, I'm back again with another newbie question. Here's the problem: Write a program that, given a beginning balance in your savings account, calculates the balance at the end of 1 year. The interest is 5.3% compounded quarterly. Show the interest earned and balance at the end of each quarter.
Here's my code: #include <stdio.h>
#include <stdlib.h>
#define INTERESTRT .053
/*Prototype Declarations*/
void getbal (float *bal);
int q1bal (float *bal);
int q2bal (float *balq1);
int q3bal (float *balq2);
int q4bal (float *balq3);
int printone (void);
int main(void)
{
/*Local Definitions*/
float bal;
float balq1;
float balq2;
float balq3;
float balq4;
float temp;
/*Statements*/
getbal (&bal);
q1bal (bal,
&balq1); <---------error
q2bal (balq1,
&balq2);
q3bal (balq2,
&balq3);
q4bal (balq3,
&balq4);
printone (bal,
balq1,
balq2,
balq3,
balq4)
system("PAUSE");
return (0);
}
/*==========================getbal=======================*/
void getbal (float *bal)
{
/*Statements*/
printf("Please enter your starting balance: \n");
scanf("%f", bal);
return;
}/*getbal*/
/*=========================q1bal========================*/
int newbal (float bal,
float *balq1)
{
/*Statements*/
balq1 = bal + (bal * INTRESTRT);
return balq1;
}/*q1bal*/
/*==========================q2bal========================*/
int newbal (float *balq1)
{
/*Statements*/
balq2 = (balq1+(balq1*INTERESTRT));
return balq2
}/*q2bal*/
/*=========================q3bal========================*/
int newbal (float *balq2)
{
/*Statements*/
balq3 = balq2 + (balq2 * INTRESTRT);
return balq3
}/*q3bal*/
/*=========================q4bal========================*/
int newbal (float *balq3)
{
/*Statements*/
balq4 = balq3 + (balq3 * INTRESTRT);
return balq4
}/*q4bal*/
/*=======================printone========================*/
void printone(float bal,
float balq1,
float balq2,
float balq3,
float balq4 )
{
/*Statements*/
printf(" \t\tBALANCE\t\t INTEREST EARNED");
printf("START\t\t%.2f \t\t %.2f ",&bal, (&bal-&bal));
printf("Q1 \t\t%.2f \t\t %.2f ",&balq1, (&balq1-&bal));
printf("Q2 \t\t%.2f \t\t %.2f ",&balq2, (&balq2-&bal));
printf("Q3 \t\t%.2f \t\t %.2f ",&balq3, (&balq3-&bal));
printf("Q4 \t\t%.2f \t\t %.2f ",&balq4, (&balq4-&bal));
return;
}/*printone*/Ok, Ok, stop...I know most of you were about to start typing a rant about recursive loops and inefficient code and such, but...please don't. I know this isn't the prettiest code (or anything resembling it), but that's not as much of a concern to me at the moment. I'll get it pretty when after get it to work. And for those of you wondering why I just don't do it the easy way and employ a loop, well, I haven't gotten to that chapter yet. Seriously though, there's something fundamental that I'm missing and I'd rather learn what it is than go around it with an "easier" way. So I guess what I'm asking for is, given the code you see here, how did I screw up?The specific error I'm getting is "Incompatible type for argument 1 of 'q1bal'" at line 28 (marked in code for clarity.) And of course, I get the same error for q2bal, q3bal, and q4bal. I'm definitely missing something. Help. thanks melee28 |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I don't understand why you have a tendency to tell your potential respondents not to address certain things in advance. You are essentially saying, "I insist on doing it the effed up way to learn, so leave that issue alone." Okay, fine, you want to learn effed up. Not a good choice, but don't cut the valuable comments out, because, when you get through effing up, they'll come in handy.
That said, you define q1bal as accepting one argument, a float, and you're passing it two. Breakin' your own rules. Hop into a nun's outfit and slap your own wrist with a ruler.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Aug 2005
Location: Orlando
Posts: 11
Rep Power: 0
![]() |
DaWei, I am definitely not looking for the "effed up way to learn." Sorry if it came off that way. I was merely trying to be as specific as I could be about the information I was looking for. I didn't want to waste anyone's time. Believe me, I appreciate all the info I get from you guys (even if I don't understand it.)
To the matter at hand though. If I understand you correctly, I should have defined q1bal with two arguments like so: void getbal (float *bal);
int q1bal (float *bal,
float balq1);
int q2bal (float *balq1,
float balq2);
int q3bal (float *balq2,
float balq3);
int q4bal (float *balq3,
float balq4);Which I just did. I'm still getting the same errors though. Did I misunderstand you? melee28 |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I didn't examine your code in detail, I just spotted a discrepancy and pointed it out. When you declare or prototype a function, the compiler uses that to evaluate the correctness of your subsequent invocations. That includes the order of the arguments, of course. If you declare (float, pointer), you must call with (float, pointer), not (pointer, float).
I see your new declarations. I only see your old invocation. There is a discrepancy. Please post the new invocation along with the error message it produces.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
I'll take a stab at a few problems:
- You declare functions like "q1bal", but when you define them you are calling them "newbal1". - Your new declaration of q1bal one seems to have the * in the wrong spot - you should be passing in the balance by value and the q1bal by reference (I think - you could go either way - it is not clear what your intentions were - this is what is in the function definition though). The declaration needs to be the same as the definition. - You are returning values from newBal as "int", but it should be returning a "float". As you don't use the return value at all, I would recommend using a "void" function. (Note that this won't stop your program working"/ |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 4
![]() |
If the chapter you read was on functions then I think you missed the concept. When you prototype a function you are telling the compiler 3 essential things; the return type, the function name and the arguments that will be passed. When you go to make your function, the first line in the definition must match the prototype completely. The return statement MUST return the exact same data type that the prototype stated. Look at the following code for an example.
/* function prototypes */
int func1(int); /* takes 1 int argument and returns an int value */
void func2(float); /* takes 1 float argument and has no return value */
void func3(void); /* takes no arguments and has no return value */
int main(void)
{
int x = 1, y;
float z = 2.0;
/* call functions according to the prototype */
y = func1(x); /* pass 1 int argument and assign the int return value to an int variable */
func2(z); /* pass 1 float argument but there is not return value */
func3(); /* pass 0 arguments and there is no return value */
return 0;
}
int func1(int num) /* definition matches prototype */
{
/* code for the function */
return num; /* value returned must be an int type */
}
void func2(float num) /* definition matches prototype */
{
/* code for the function */
return; /* return no value (return statement can be omitted) */
}
void func3(void) /* definition matches prototype */
{
/* code for the function */
return; /* return no value (return statement can be omitted) */
}Hope that helps clear things up. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|