## C programming Project

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

### C programming Project

Hello techreport.
I am doing an extra credit assignment for my Computer Science class, and since so my professor wont help us or give us hints. Firstly, this is an introductory course to C programming and I'm pretty sure that my fundamentals will not be the best and I definitely know this is not the best way to solve this problem. But this is what works for me.

SO, I am stuck with my program. Heres what the problem is.

Calculate the value of a retirment portfolio by querying the user for the following information: To save time and space I will omit this part. This is not the part that is troubling me. Ok now, heres the goods.
My program using Borland 5.02:
/*Extra credit program: Calculate the value of a retirement portfolio
Spencer Alessi
March 2009
Version 1
Language C Borland 5.02
*/

#include <stdio.h>
#include <conio.h> //getch

//function prototypes
double initial_sal(void);
double salary_inc(void);
double salary_perc(void);
double rate(void);
double starting_year(void);
double finishing_year(void);
void calc_print(double initial_sal, double salary_inc, double salary_perc,
double rate, double starting_year, double finishing_year);

int main (void)
{ double yr, salary, starting, contributions, earnings, closing;
initial_sal();
salary_inc();
salary_perc();
rate();
starting_year();
finishing_year();
calc_print(yr, salary, starting, contributions, earnings, closing);

getch();
return 0;
}

double initial_sal(void)
/*function to get the initial starting salaray
Spencer Alessi
March 2009
C borland 5.02
*/
{double salary;
printf("Enter the initial starting salary: ");
scanf("%lf", &salary);
return (salary);
}

double salary_inc(void)
/*function to get the projected annual percentage salary increase
Spencer Alessi
March 2009
C Borland 5.02
*/
{double sal_inc;
printf("Enter the projected annual percentage salary increas (.00) form: ");
scanf("%lf", &sal_inc);
return (sal_inc);
}

double salary_perc(void)
/*function to get the percentage of salary to be contributed to
the retirement plan
Spencer Alessi
March 2009
C Borland 5.02
*/
{double sal_perc;
printf("Enter the percentage of the salary to be contributed to the plan: ");
scanf("%lf", &sal_perc);
return (sal_perc);
}

double rate(void)
/*function to get the annual rate of return on the earnings
Spencer Alessi
March 2009
C Borland 5.02
*/
{double rate_ret;
printf("Enter the annual rate of return on the earnings: ");
scanf("%lf", &rate_ret);
return (rate_ret);
}

double starting_year(void)
/*function to get the starting year and finishing year for contributions
Spencer Alessi
March 2009
C Borland 5.02
*/
{double start_year;
printf("Enter the starting year for contributions (example: 1940): ");
scanf("%lf", &start_year);
return (start_year);
}

double finishing_year(void)
/*function to ge the finishing year of contributions
Spencer Alessi
March 2009
C Borland 5.02
*/
{double finish_year;
printf("Enter the finishing year of contributions: ");
scanf("%lf", &finish_year);
return (finish_year);
}

void calc_print(double initial_sal, double salary_inc, double salary_perc,
double rate, double starting_year, double finishing_year)
/*function to calculate and print the value of a retirement portfolio
Spencer Alessi
March 2009
C Borland 5.02
*/
{char junk; //page control
double yr,
salary,
starting,
contribution,
contribution_value,
earnings,
rr,
closing;
printf("Year Salary Starting Contributions Earnings Closing\n");
while (yr >= finishing_year)
{yr=++yr;
salary=initial_sal * salary_inc;
starting=initial_sal * salary_perc;
contribution=starting * salary_inc;
contribution_value=starting + contribution;
earnings=starting * rr;
closing=starting + contribution + earnings;
if((yr%20)==0)
{ printf("Hit enter to continue");
scanf("%c",&junk);
printf("Year Salary Starting Contributions Earnings Closing\n");
}
printf("%f, f%, %f, %f, %f, %f\n", yr,salary,starting,contribution,
earnings,closing);
}

return;
}

When I try to run this, It gives me an error saying illegal use of floating point. When I take the page control if statement out, I get a bunch of never ending numbers for "year" and a bunch of %f printing out instead of the values I want. have fiddled with this for a while and cant figure out what i'm doing wrong. If anyone has any advice, tips, ect. I in no way want someone to finish this program for me, I am merely looking for tips and advice on what I did wrong or what I can fix. I appreciate any help.

Spence
salessi
Gerbil In Training

Posts: 2
Joined: Sun Mar 15, 2009 3:17 pm

### Re: C programming Project

Reposting in code tage for readability :

Code: Select all
`/*Extra credit program: Calculate the value of a retirement portfolioSpencer AlessiMarch 2009Version 1Language C Borland 5.02*/#include <stdio.h>#include <conio.h> //getch//function prototypesdouble initial_sal(void);double salary_inc(void);double salary_perc(void);double rate(void);double starting_year(void);double finishing_year(void);void calc_print(double initial_sal, double salary_inc, double salary_perc,double rate, double starting_year, double finishing_year);int main (void){ double yr, salary, starting, contributions, earnings, closing;initial_sal();salary_inc();salary_perc();rate();starting_year();finishing_year();calc_print(yr, salary, starting, contributions, earnings, closing);getch();return 0;}double initial_sal(void)/*function to get the initial starting salaraySpencer AlessiMarch 2009C borland 5.02*/{double salary;printf("Enter the initial starting salary: ");scanf("%lf", &salary);return (salary);}double salary_inc(void)/*function to get the projected annual percentage salary increaseSpencer AlessiMarch 2009C Borland 5.02*/{double sal_inc;printf("Enter the projected annual percentage salary increas (.00) form: ");scanf("%lf", &sal_inc);return (sal_inc);}double salary_perc(void)/*function to get the percentage of salary to be contributed tothe retirement planSpencer AlessiMarch 2009C Borland 5.02*/{double sal_perc;printf("Enter the percentage of the salary to be contributed to the plan: ");scanf("%lf", &sal_perc);return (sal_perc);}double rate(void)/*function to get the annual rate of return on the earningsSpencer AlessiMarch 2009C Borland 5.02*/{double rate_ret;printf("Enter the annual rate of return on the earnings: ");scanf("%lf", &rate_ret);return (rate_ret);}double starting_year(void)/*function to get the starting year and finishing year for contributionsSpencer AlessiMarch 2009C Borland 5.02*/{double start_year;printf("Enter the starting year for contributions (example: 1940): ");scanf("%lf", &start_year);return (start_year);}double finishing_year(void)/*function to ge the finishing year of contributionsSpencer AlessiMarch 2009C Borland 5.02*/{double finish_year;printf("Enter the finishing year of contributions: ");scanf("%lf", &finish_year);return (finish_year);}void calc_print(double initial_sal, double salary_inc, double salary_perc,double rate, double starting_year, double finishing_year)/*function to calculate and print the value of a retirement portfolioSpencer AlessiMarch 2009C Borland 5.02*/{char junk; //page controldouble yr,salary,starting,contribution,contribution_value,earnings,rr,closing;printf("Year Salary Starting Contributions Earnings Closing\n");while (yr >= finishing_year){yr=++yr;salary=initial_sal * salary_inc;starting=initial_sal * salary_perc;contribution=starting * salary_inc;contribution_value=starting + contribution;earnings=starting * rr;closing=starting + contribution + earnings;if((yr%20)==0){ printf("Hit enter to continue");scanf("%c",&junk);printf("Year Salary Starting Contributions Earnings Closing\n");}printf("%f, f%, %f, %f, %f, %f\n", yr,salary,starting,contribution,earnings,closing);}return;}`
2014 21" iMac w Intel Iris Pro
Graphmaster Gerbil

Posts: 1399
Joined: Sun Jul 08, 2007 10:42 pm
Location: Seattle, WA

### Re: C programming Project

well it's been a few years since I did any proper programming but...

You get final_year with scanf("%lf"... which is where your float is coming from.

you then say yr=final_year

and then use yr=++yr;

which I think you can only do with an int.

try using scanf("%d"... to use integers instead.

EDIT:
also, that yr=++yr; is pretty ugly, you can just use yr++; though a for loop might be better here
Fernando!
cheesyking
Minister of Gerbil Affairs

Posts: 2507
Joined: Sun Jan 25, 2004 7:52 am
Location: That London (or so I'm told)

### Re: C programming Project

Ok, I have cleaned up the loop a little bit and have removed the page control loop for now to avoid more confusion. I am able to compile this and run it, and it prints out all my titles I want, but no values.
Code: Select all
`/*Extra credit program: Calculate the value of a retirement portfolio  Spencer Alessi  March 2009  Version 1  Language C Borland 5.02*/#include <stdio.h>#include <conio.h> //getch//function prototypesdouble initial_sal(void);double salary_inc(void);double salary_perc(void);double rate(void);double starting_year(void);double finishing_year(void);void calc_print(double initial_sal, double salary_inc,  double salary_perc, double rate, double starting_year, double finishing_year);int main (void){ double salary, starting, contributions, earnings, closing;  int yr;  initial_sal();  salary_inc();  salary_perc();  rate();  starting_year();  finishing_year();  calc_print(yr, salary, starting, contributions, earnings, closing); getch(); return 0;}double initial_sal(void)/*function to get the initial starting salaray  Spencer Alessi  March 2009  C borland 5.02*/{double salary;printf("Enter the initial starting salary: ");scanf("%lf", &salary);return (salary);}double salary_inc(void)/*function to get the projected annual percentage salary increase  Spencer Alessi  March 2009  C Borland 5.02*/{double sal_inc;printf("Enter the projected annual percentage salary increas (.00) form: ");scanf("%lf", &sal_inc);return (sal_inc);}double salary_perc(void)/*function to get the percentage of salary to be contributed to  the retirement plan  Spencer Alessi  March 2009  C Borland 5.02*/{double sal_perc;printf("Enter the percentage of the salary to be contributed to the plan: ");scanf("%lf", &sal_perc);return (sal_perc);}double rate(void)/*function to get the annual rate of return on the earnings  Spencer Alessi  March 2009  C Borland 5.02*/{double rate_ret;printf("Enter the annual rate of return on the earnings: ");scanf("%lf", &rate_ret);return (rate_ret);}double starting_year(void)/*function to get the starting year and finishing year for contributions  Spencer Alessi  March 2009  C Borland 5.02*/{double start_year;printf("Enter the starting year for contributions (example: 1940): ");scanf("%lf", &start_year);return (start_year);}double finishing_year(void)/*function to ge the finishing year of contributions  Spencer Alessi  March 2009  C Borland 5.02*/{double finish_year;printf("Enter the finishing year of contributions: ");scanf("%lf", &finish_year);return (finish_year);}void calc_print(double initial_sal, double salary_inc,  double salary_perc, double rate, double starting_year, double finishing_year)/*function to calculate and print the value of a retirement portfolio  Spencer Alessi  March 2009  C Borland 5.02*/{char junk; //page controldouble yr,       salary,       starting,       contribution,       contribution_value,       earnings,       rr,       closing;printf("Year   Salary    Starting      Contributions    Earnings    Closing\n");for (yr=1;yr <= finishing_year;yr++)     {salary=initial_sal * salary_inc;      starting=initial_sal * salary_perc;      contribution=starting * salary_inc;      contribution_value=starting + contribution;      earnings=starting * rr;      closing=starting + contribution + earnings;printf("%lf, %lf, %lf, %lf, %lf, %lf\n", yr,salary,starting,contribution,earnings,closing);}return;}`
salessi
Gerbil In Training

Posts: 2
Joined: Sun Mar 15, 2009 3:17 pm

### Re: C programming Project

Isn't %lf for "long double"? That might mess things up.

Have you tried looking at variables in debug mode?

Functions that ask to enter variable could be reused, the code is terribly long for simple stuff.

Use something like this:

inline void AskVariable(const char *sentence, double *var)
{
printf(sentence);
scanf("%lf", var);
}

and then:

Functions that do basic stuff and are similar should be grouped. No one wants to dog through 1000 lines of code that do same stuff over and over.
Last edited by Madman on Sun Mar 15, 2009 5:43 pm, edited 1 time in total.
Core 2 Duo E6300, MSI P45 NEO-F, Club 3D GTX 260, 4Gb DDR2-800Mhz, Audigy X-Fi Fatal1ty Champ1on ed., 0.5Tb+1Tb Seagate Barracuda 7200.12, 630W AXP, Samsung SyncMaster BX2450, ViewSonic VP171b
Minister of Gerbil Affairs

Posts: 2317
Joined: Tue Apr 01, 2003 4:55 am
Location: Latvia

### Re: C programming Project

cheesyking wrote:EDIT:
also, that yr=++yr; is pretty ugly, you can just use yr++; though a for loop might be better here

Not only that, but using a lot of ++ and -- with one variable in complex expressions is undefined, so it's better to use them in places where they are obvious and simple.

In your case it should work, I think, but in general it's not a good idea to mix expressions, functions and assignments with lotsa ++/--

EDIT: and yea, your functions return the value they read, but they don't assign them to variables, you have uninitialized variables that you pass to CalcStuff.
Core 2 Duo E6300, MSI P45 NEO-F, Club 3D GTX 260, 4Gb DDR2-800Mhz, Audigy X-Fi Fatal1ty Champ1on ed., 0.5Tb+1Tb Seagate Barracuda 7200.12, 630W AXP, Samsung SyncMaster BX2450, ViewSonic VP171b
Minister of Gerbil Affairs

Posts: 2317
Joined: Tue Apr 01, 2003 4:55 am
Location: Latvia

### Re: C programming Project

Thread moved to the Developer's Den from General Software.

Kevin
Kevin