I am writing a program in C to simulate a checking account. There are codes for transactions, I = Initial Balance, D = Deposit, and C = Check(you write a check to someone, like a withdrawal). There is a monthly fee of $3.00 to maintain the account, $0.06 fee for every check cashed, %0.03 for every deposit made, $5.00 overdraft fee whenever a check cashed brings the balance below $0.00.
I am having trouble completing the functions. If you don't think helping a bit with all of them is ok then please just help with the deposit() function. I am only a couple months into C and we just got into functions. Here is my unfinished code. Thanks for any help.
#include <stdio.h>
void outputHeaders (void);
void initialBalance (double iBalance);
void deposit(double amount, double balance, double service, int numDeposit,double amtDeposit);
void check(char code, double amtCheck, double balance);
void outputSummary ();
int main (void)
{
char code;
double amount, service, balance;
double amtCheck, amtDeposit, openBalance, closeBalance;
int numCheck, numDeposit;
amount = 0.0;
service = 0.0;
balance = 0.0;
amtCheck = 0.0;
amtDeposit = 0.0;
openBalance = 0.0;
closeBalance = 0.0;
numCheck = 0;
numDeposit = 0;
outputHeaders();
printf("Enter the code of transaction and the amount: ");
scanf("%c %lf\n", &code, &amount);
if (code == 'I')
{
initialBalance(amount, &balance, &service, &numDeposit, &amtDeposit);
}
else if (code == 'D')
{
deposit (amount, &balance, &service, &numDeposit);
}
else
{
check(amount, &balance, &service, &numCheck, &amtCheck);
}
getchar(); getchar();
return 0;
}
void outputHeaders (void)
{
printf("Transaction Deposit Check Balance\n"
"-------------- -------- ------ -------");
}
void initialBalance (double amount, double *balance, double *service, int *numDeposit, double *amtDeposit)
{
}
void deposit (double amount, double *balance, double *service, int *numDeposit, double *amtDeposit)
{
*balance = *balance + *amtDeposit;
*numDeposit++; //need to keep track of amount of deposits
*service = *service - 0.03; //service charge
printf("Deposit %lf %lf\n", *amtDeposit, *balance);
}
void check (double amount, double *balance, double *service, int *numCheck, double *amtCheck)
{
}
void outputSummary (int *numDeposit, double *amtDeposit, int *numCheck, int *amtCheck, double *openBalance, double *service, double *closeBalance)
{
}
I have seen only deposit(); function when you declared/defined In your function you are using five arguments but calling time used only four argument.
so if you are calling like deposit (amount, &balance, &service, &numDeposit);
Then change your definition/declaration like this
void deposit (double amount, double *balance, double *service, int *numDeposit)
{
*balance = *balance + amount;
*numDeposit++; //need to keep track of amount of deposits
*service = *service - 0.03; //service charge
//I think service change need to reduce from main balance so
*balance = *balance - 0.03;
printf("Deposit %lf balance %lf\n", amount, *balance);
}
Related
Tried running this in C but it keeps saying "function definition is not allowed here" when referring to '{'. This is the full code that im trying to run, its called a "greedy algorithm" so im still learning how to make it. The program is supposed to give you your exact change back then let you know how many coins of which type will be needed.
#include <cs50.h>
#include <math.h>
int main()
{
int len = 4;
double cost;
double pay;
{
//promt user for cost
printf("Enter cost: ");
scanf("%lf", &cost);
//prompt user for pay amount
printf("Enter pay: ");
scanf("%lf", &pay);
double change = pay - cost;
if(pay > cost)
printf("Trasncation complete\nHeres your change: %f\n", change);
else if(pay == cost)
printf("Transaction complete.\n");
else if(pay < cost)
printf("insufficient funds\nStill need: %f", cost - pay);
//greddy algorithm
}
void greedy(double change)
{
int i=o;
double number;
double coin[len] = [0.01, 0.05, 0.10, 0.25];
while(i>len)
{
if(coin[i] <= change)
{
number = change / coin[i];
printf("%f of %f is needed", number, change);
change = change * coin[i];
}
i++
}
}
}
You have to define your greedy() function outside of the main function like so:
#include <cs50.h>
#include <math.h>
int main()
{
int len = 4;
double cost;
double pay;
{
//promt user for cost
printf("Enter cost: ");
scanf("%lf", &cost);
//prompt user for pay amount
printf("Enter pay: ");
scanf("%lf", &pay);
double change = pay - cost;
if(pay > cost)
printf("Trasncation complete\nHeres your change: %f\n", change);
else if(pay == cost)
printf("Transaction complete.\n");
else if(pay < cost)
printf("insufficient funds\nStill need: %f", cost - pay);
}
}
// greddy algorithm
void greedy(double change)
{
int i=o;
double number;
double coin[len] = [0.01, 0.05, 0.10, 0.25];
while(i>len)
{
if(coin[i] <= change)
{
number = change / coin[i];
printf("%f of %f is needed", number, change);
change = change * coin[i];
}
i++
}
}
I'm trying to make a simple program with C that gets name, hourly rate and hours for input and calculates gross pay, tax, net pay etc, but I got stuck as I was making a function that prints out results.
I created two loops: one loop that generates overtime, gross pay, tax and net pay. Another loop that prints out the generated data. First one works fine. It generates data just fine, but as I run the program, it simply ignores the second loop and just finish the program.
It is just a simple void function that has no return value, and yet I'm stuck. I used pass by reference. If you know what's wrong, please let me know.
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#define size 5
char name[size][20];
float rate[size];
float hours[size];
int i = 0;
void input();
float getbasepay(float *rt, float *hr);
float getovtime(float *rt, float *hr);
float getgross(float *base, float *ovt);
float gettax(float *gross);
float getnetpay(float *gross, float *tax);
void printout(char name, float rate, float hours, float base, float ovt, float gross, float tax, float net);
void input()
{
for (i = 0; i < size; i++)
{
puts("\ntype name: (type -1 to quit) \n");
scanf_s("%19s", &name[i], 20);
if (strcmp(name[i], "-1") == 0) { break; }
puts("\ntype hourly rate: (type -1 to quit) \n");
scanf_s("%f", &rate[i]);
if (rate[i] == -1) { break; }
puts("\ntype hours worked: (type -1 to quit) \n");
scanf_s("%f", &hours[i]);
if (hours[i] == -1) { break; }
}
return ;
}
float getbasepay(float *rt, float *hr)
{ float base;
float baseo;
float excessive = (*hr - 40);
if (*hr <= 40)
{
base = (*rt) * (*hr);
return base;
}
if (*hr >= 40)
{
baseo = (*hr - excessive) * (*rt);
return baseo;
}
}
float getovtime(float *rt,float *hr)
{
float time;
float ovt;
if (*hr >= 40)
{
time = (*hr - 40);
ovt = time * (*rt) *1.5;
return ovt;
}
else
{
return 0;
}
}
float getgross(float *base, float *ovt)
{
float gross = *base + *ovt;
return gross;
}
float gettax(float *gross)
{
float tax = *gross * 0.20;
return tax;
}
float getnetpay(float *gross, float *tax)
{
float net;
net = *gross - *tax;
return net;
}
void printout(char *name, float *rate, float *hours, float *base, float * ovt, float *gross, float *tax, float *net)
{
printf("\nPay to: %s \n", *name);
printf("Hourly rate: %f \n", *rate);
printf("Hours: %f \n", *hours);
printf("Base pay: $%f \n", *base);
printf("Overtime: $%f \n", *ovt);
printf("Gross Pay: $%f \n", *gross);
printf("Tax: $%f \n", *tax);
printf("Net Pay: $%f \n", *net);
}
int main(void)
{
int o, z;
float overtime[size] = { 0,0,0,0,0, };
float basepay[size] = { 0,0,0,0,0, };
float tax[size] = { 0,0,0,0,0, };
float grosspay[size] = { 0,0,0,0,0, };
float netpay[size] = { 0,0,0,0,0, };
float sum;
input();
for (o = 0; o < i; o++)
{
basepay[o] = getbasepay(&rate[o], &hours[o]);
overtime[o] = getovtime(&rate[o], &hours[o]);
grosspay[o] = getgross(&basepay[o], &overtime[o]);
tax[o] = gettax(&grosspay[o]);
netpay[o] = getnetpay(&grosspay[o], &tax[o]);
}
for (z = 0; z > i; z++)
{
printout(&name[z], &rate[z], &hours[z], &basepay[z], &overtime[z], &grosspay[z], &tax[z], &netpay[z]);
}
}
Your code doesn't compile:
gcc -o main main.c
Answers:
error: conflicting types for ‘printout’
And indeed, the signature in the declaration is different than the one you
wrote in the implementation (for example char *name instead of char name etc.)
So I am working on a game of craps, one aspect of the game is that the wager must be less than or equal to balance to continue (so you don't bet more than you have). Right now I have my full game I am working on, but that would be too much code to post, so I wrote out essentially what I am trying to do.
The code I am posting assumes it is a loss, but it does not subtract correctly for some reason. And in my actual game, it ignores the while function completely ie. if my balance is 20 and I bet 22 it gives me -2. I can not figure out how to make this work correctly..
#include<stdio.h>
int main(void)
{
double balance = 0.0, wager = 0.0;
balance = get_balance();
wager = get_wager();
checking_wager(balance, wager);
loss(balance, wager);
return 0;
}
double get_balance(void)
{
double balance = 0.0;
balance = printf("Enter your balance: ");
scanf("%lf", &balance);
return balance;
}
double get_wager(void)
{
double wager = 0.0;
wager = printf("Enter your wager: \n");
scanf("%lf", &wager);
return wager;
}
double checking_wager(double balance, double wager)
{
while (wager > balance)
{
printf("The wager is invalid\n");
wager = get_wager();
}
return 0;
}
double loss(double balance, double wager)
{
balance -= wager;
printf("Balance: %.2f", balance);
return balance;
}
If anyone has any suggestions on how I can make this work, I would really appreciate it. Thanks :)
I reformat you code, now it should work.
int main(void)
{
double balance = 0.0, wager = 0.0;
balance = get_balance();
wager = get_wager();
checking_wager(balance, &wager);
loss(balance, wager);
return 0;
}
double get_balance(void)
{
double balance = 0.0;
balance = printf("Enter your balance: ");
scanf("%lf", &balance);
return balance;
}
double get_wager(void)
{
double wager = 0.0;
wager = printf("Enter your wager: \n");
scanf("%lf", &wager);
return wager;
}
double checking_wager(double balance, double *wager)
{
while (*wager > balance)
{
printf("The wager is invalid\n");
*wager = get_wager();
}
return 0;
}
double loss(double balance, double wager)
{
balance -= wager;
printf("Balance: %.2f", balance);
return balance;
}
The changes I've done, is first replaced the returntypes form
int
to
double
Second and more importent, I've change the second paramter int this function
int checking_wager(double balance, double wager)
to a pointer to double
double checking_wager(double balance, double *wager)
The reason this, to return the new wager from the function.
The problem occurs from the definition of your checking_wager() function.
So from my assumption, you passed two parameters which are balance and wager, and hopefully to reinput value of wager in case it's greater than balance.
However, your code doesn't do like you'd intended, because you defined the wager as a new local variable, which doesn't share the same location as wager from main function.
So, the easiest fix is to change checking_wager()'s wager into a pointer, so that it can refers and changes value from main()'s wager.
int checking_wager(double balance, double *wager)
{
while (*wager > balance)
{
printf("The wager is invalid\n");
*wager = get_wager();
}
return 0;
}
So, to call it from main function, you can basically give main function's wager pointer into the checking_wager() function.
checking_wager(balance, &wager);
Hopefully that will helps.
The guys just helped me with a problem, got solved 100%, but now I have another one.
I compile my files and everything without problems, but when i want to run the program nothing happens.
enter.c : http://pastebin.com/GGzVeAhw
simple_interest.c: http://pastebin.com/XdESrxSk
This is how my header file looks like:
int enter(void);
double Calc_Interest(double principal, double rate, int term);
double Calc_Amount(double principal, double interest);
double Calc_Payments(double total, int term);
void Display_Results(double principal, double interest, double total, double mPay);
PS This forum is really helpful, especially for a beginner like me.
You have a bunch of prototypes in main(), but no function call.
It should be:
int main(void)
{
enter();
return 0;
}
You seem really confused about how to declare and use functions, perhaps you need some tutorial?
simple_interest.h
int enter(void);
double Calc_Interest(double principal, double rate, int term);
double Calc_Amount(double principal, double interest);
double Calc_Payments(double total, int term);
void Display_Results(double principal, double interest, double total, double mPay);
simple_interest.c
//Implement all functions in this file
#include <stdio.h>
double Calc_Interest(double principal, double rate, int term)
{
double interest;
interest = principal * (rate/100) *term;
return interest;
}
double Calc_Amount(double principal, double interest)
{
double total;
total = principal + interest;
return total;
}
double Calc_Payments(double total, int term)
{
double mPay;
mPay= total / (12 * term);
return mPay;
}
void Display_Results(double principal, double interest, double total, double mPay)
{
printf("\n");
printf("Personal Loan Statement\n");
printf("\n");
printf("Amount Borrowed: %.2f\n" , principal);
printf("Interest on Loan: %.2f\n" , interest);
printf("--------------------------------------------------------------------------------\n");
printf("Amount of Loan: %.2f\n" , total);
printf("Monthly Payment: %.2f\n" , mPay);
}
sample.c
#include <stdio.h>
#include "simple_interest.h"
int enter(void)
{
double principal, rate, interest, total, mPay;
int term;
printf("--------------------------------------------------------------------------------\n");
printf("This program calculates the monthly payments due from a personal loan\n");
printf("--------------------------------------------------------------------------------\n");
printf("Enter amount of the loan: ");
scanf("%lf", &principal);
printf("Enter current annual interest rate in \n");
scanf("%lf", &rate);
printf("Enter the number of years of the loan: ");
scanf("%d", &term);
interest = Calc_Interest(principal, rate, term);
total = Calc_Amount(principal, interest);
mPay = Calc_Payments(total, term);
Display_Results(principal, interest, total, mPay);
return 0;
}
int main(void)
{
enter();
return 0;
}
compile like this way
gcc sample.c simple_interest.c
./a.out
Output shows all 0's. The gross and OT is not calculating..
#include <stdio.h>
#define STD_HOURS 40.0
#define OT_RATE 1.5
#define SIZE 5
void read_hours(float worked_hours[], long int clockNumber[]);
void calculate_gross(float wage[], float worked_hours[], float gross);
void calculate_gross_ot(float wage[], float worked_hours[]);
void printFunction(long int clockNumber[], float wage[], float worked_hours[], float OT[], float gross);
int i;
int main()
{
long int clockNumber [SIZE] = {98401, 526488, 765349, 34645, 127615};/* employee ID */
float gross = 0.0; /* gross */
float OT [SIZE] = {}; /* overtime */
float wage [SIZE] = {10.6, 9.75, 10.5, 12.25, 8.35}; /* hourly wage */
float worked_hours [SIZE] = {}; // Hours worked
read_hours(worked_hours, clockNumber);
if (worked_hours[i]>STD_HOURS)
{
calculate_gross_ot(wage, worked_hours);
}
else
{
calculate_gross(wage,worked_hours, gross);
}
printFunction(clockNumber, wage, worked_hours, OT, gross);
return(0);
}
void read_hours(float worked_hours[], long int clockNumber[])
{
for (i=0; i<SIZE; i++)
{
printf("\n Enter Hours for Emlployee ID: %ld\n", clockNumber[i]);
scanf ("%f", &worked_hours[i]);
}
}
void calculate_gross(float wage[], float worked_hours[], float gross)
{
for(i=0; i<SIZE; i++)
gross=(wage[i]*worked_hours[i]);
}
void calculate_gross_ot(float wage[], float worked_hours[])
{
float gross;
float OT[SIZE];
for (i=0; i<SIZE; i++)
{
/* calculating overtime hours */
OT[i]=worked_hours[i]-STD_HOURS;
/* calculating gross pay with overtime */
gross = (STD_HOURS*wage[i]) + (OT[i]*OT_RATE*wage[i]);
//}
}
}
void printFunction(long int clockNumber[], float wage[], float worked_hours[], float OT[], float gross)
{
/* creating a table for the output */
printf("------------------------------------------------\n");
printf("%7s","Clock#");
printf("%7s","Wages");
printf("%7s","Hours");
printf("%7s","OT");
printf("%7s\n","Gross");
printf("------------------------------------------------\n");
for (i=0; i<SIZE; i++)
{
/* printing the results */
printf("%6ld", clockNumber[i]);
printf("%10.2f",wage[i]);
printf("%6.1f", worked_hours[i]);
printf("%6.1f", OT[i]);
printf("%10.2f",gross);
printf("\n");
}
}
The program is used to calculate gross with and without OT hours. The output is showing all 0's for the gross and OT. Please help to figure out where is the mistake.
In the first case, you are passing gross by value.
In the second case, you are not passing it at all (the function has a local function called gross).
In both cases, whenever the respective function changes gross, this change does not propagate to the caller.
You need to either pass gross by pointer, or return the value from the function using the return statement (and changing the return type appropriately).
Pass OT to your calculate_gross_ot() function from main().
...
calculate_gross_ot(wage, worked_hours, OT);
...
void calculate_gross_ot(float wage[], float worked_hours[], float OT[])