Wager must be less than balance to continue C - c

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.

Related

Need help solving a issue involving 'function definition is not allowed here' in c language

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++
}
}

How can I get value from multiple argument in one function and print it in main function in c?

currently i study in c programming language but i have a problem, is there i can get one value from multiple argument in one function and print it in main function in c?? i already create a function that come out with two argument, but somehow i need to print just one argument that only appear one process that i create more than one operation in main function.
this is my code:
#include<stdio.h>
float bakeryShop(float price_a, float price_b)
{
float discountA, discountB;
discountA = price_a - (price_a*45/100);
discountB = price_b - (price_b*60/100);
printf("%.2f", discountA);
return (discountA);
printf("%.2f", discountB);
return (discountB);
}
main()
{
char name_item_a[200];
char name_item_b[200];
float price_a, price_b;
float discountA, discountB, result;
printf("Enter item A: ");
scanf("%s", &name_item_a);
printf("Enter price for item A: ");
scanf("%f", &price_a);
printf("Enter item B: ");
scanf("%s", &name_item_b);
printf("Enter price for item B: ");
scanf("%f", &price_b);
printf("%s\n", name_item_a);
bakeryShop(price_a, price_b);
printf("%.2f", result);
return price_a;
printf("%s\n", name_item_b);
result = bakeryShop(price_a, price_b);
printf("%.2f", result);
return price_b;
}
if you can see in the bakeryShop function, i create two process which is discountA and discountB
but how to display only discount A in main function ??
because the output should be like this,
ITEM A
Name:
Original Price: RM
Price after 45% discount: RM
ITEM B
Name:
Original Price:
M Price after 60%
discount: RM
but i do not know how to get only one process discount A seperate with another process discount B
You can not return 2 variables from a function like this.
float bakeryShop(float price_a, float price_b)
{
return (discountA);
return (discountB);
}
This will only return the first variable.
As price_a and price_b are not connected - you can just take only 1 argument in the function. Then calling the function twice from the main method. Something like this -
float bakeryShop(float price)
{
float discount;
discount = price - (price*45/100);
return (discount);
}
int main(){
int price1, price2;
scanf("%d, %d", &price1, &price2);
float discount1 = bakeryShop(price1);
float discount2 = bakeryShop(price2);
printf("%.2f", discount1);
printf("%.2f", discount2);
}
I do believe you want to define the function like this:
float bakeryShop(float price, float discount_percent)
{
float discount;
discount = price - (price*discount_percent/100);
printf("%.2f", discount);
return discount;
}
and then use it like:
printf("%s\n", name_item_a);
result = bakeryShop(price_a, 45);
printf("%.2f", result);
printf("%s\n", name_item_b);
result = bakeryShop(price_b, 60);
printf("%.2f", result);
Because what you did in the code above doesn't work in C at all - you cannot return multiple times from a function. If you return, you return from function once, and that's it.
So, use this function for every item separately, and it'll be usefull and meaningfull in terms of C.
Your question is kind of confusing, but if I understand it correctly, then you're basically trying to calculate the discounted price of two "items" based on user input, where each item has a unique discount (price/45*100 for item A, price/60*100 for item B) - and you're stuck on trying to return the discount for both items from the function that's supposed to calculate it.
I'd approach the problem differently. Since both items A and B are defined by the same set of information (a name, a price, and a later calculated discounted price) then why not group that information together into an "Item" struct?
Another thing, since the discount is specific to each item, rather than making a function that takes multiple items as arguments, and calculates the discount for each of them separately (spawning the problem of returning multiple values, and nullifying the point of a function as you'll have to expand or contract the function dependiong on how many items you need to calculate in main) why not make a function that calculates the discount of a single item at a time, with the discount specified as an argument?
#include <stdio.h>
struct Item {
char Name[200];
float Price;
float DiscountedPrice;
};
void ApplyDiscount(Item* item, float amount) {
item->DiscountedPrice = (item->Price * amount) / 100.0;
}
// Or, if you haven't studied pointers yet..
Item ApplyDiscount(Item item, float amount) {
item.DiscountedPrice = (item.Price * amount) / 100.0;
return item;
}
int main() {
Item item_a, item_b;
printf("Item A name: ");
scanf("%s", &item_a.Name);
printf("Item A price: ");
scanf("%f", &item_a.Price);
printf("Item B name: ");
scanf("%s", &item_b.Name);
printf("Item B price: ");
scanf("%f", &item_b.Price);
ApplyDiscount(&item_a, 45);
ApplyDiscount(&item_b, 60);
printf("%s: %.2f\n", &item_a.Name, item_a.DiscountedPrice);
printf("%s: %.2f\n", &item_b.Name, item_b.DiscountedPrice);
}
If you'd only like to know how to return multiple values from a function, there are multiple ways. One such way would be to write the values through pointers passed as arguments, like so
void ApplyDiscount(float item_a_price, float item_b_price, float* item_a_result, float* item_b_result) {
*item_a_result = (item_a_price * 45.0) / 100.0;
*item_b_result = (item_b_price * 60.0) / 100.0;
}
int main() {
float item_a_price = 180;
float item_b_price = 42;
float item_a_result, item_b_result;
ApplyDiscount(item_a_price, item_b_price, &item_a_result, &item_b_result);
}
But I wouldn't really advise this, since it can get pretty messy pretty quickly.
You could also just group the results together in a struct, but if you're doing it like that anyway, then you may as well just make an "Item" type like I previously suggested.
struct Results {
float ResultA;
float ResultB;
};
Results ApplyDiscount(float item_a_price, float item_b_price) {
Results results;
results.ResultA = (item_a_price / 45.0) * 100.0;
results.ResultB = (item_b_price / 60.0) * 100.0;
return results;
}
int main() {
float item_a_price = 180;
float item_b_price = 42;
float item_a_result, item_b_result;
Results results = ApplyDiscount(item_a_price, item_b_price);
item_a_result = results.ResultA;
item_b_result = results.ResultB;
}

deposit() function of this checking account C program

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);
}

Cash Register Program - C

Good day! In a program I am writing for school we must make a cash register type program, seems simple enough, but for the life of me I can not get it to work. After taking in the number of products bought, then the price of all, the program must ask for the cash to pay, then give back the change. BUT the change must be given in amount of loonies back (or $1 bills), and then just the remaining cents. Help? I've gotten the loonies to work (somewhat) but I don't know how to do the change back.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int itemNum, justChange;
double prodPrice, tax, cashGiven, change, purchasePrice, changeGiven, changeBack, cashBack;
float totalPrice;
//Num of items
printf ("Number of items: ");
scanf("%d", &itemNum);
//Price of items
printf("Please enter price of items: ");
scanf("%lf", &prodPrice);
//Math Stuff
purchasePrice = itemNum*prodPrice;
tax = purchasePrice * 0.13;
totalPrice = purchasePrice*1.13;
//find change alone
//justChange = totalPrice
//Price Output
printf("Purchase price is: %.2lf \n",purchasePrice );
printf("TAX (HST 13%): %.2lf\n",tax );
printf("Total price is: %.2lf \n",totalPrice );
printf("Please Enter Cash: ");
scanf("%lf", &cashGiven);
printf("Please Enter Change: ");
scanf("%lf", &changeGiven);
//MAth stuuff again
double endCash;
double loony;
int yoloswag;
endCash = cashGiven - totalPrice;
loony = endCash/1;
loony = loony--;
if (loony<0)
printf ("Loonies: 0");
else
printf("Loonies: %.0lf \n",loony );
printf("change: %d ", totalPrice-floor(totalPrice) );
return 0;
}
Create an array with possible change values;
double cashValues[6] = {1, 0.5, 0.2, 0.1, 0.05, 0.01};
Then create a for loop in which you try to subtract the possible change values from the difference until the difference is zero.
double difference;
difference = cashGiven - totalPrice;
while (difference != 0) {
for(int i=0; i<6; i++) {
if(cashValues[i] <= difference) {
difference -= cashValues[i];
printf("%f \n", cashValues[i]);
i=0;
}
}
}

function keep returning 0 and parameters not read

I have this code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
float sinfa(num1, num2)
{
float fc;
float powers;
if(num1 == ""){
powers = pow(num2,4);
}else{
powers = pow(num1,4);
}
fc = sin(num1-powers+1);
return (fc);
}
float tp(fa,fb,num1,num2)
{
float p;
float fm2 = fa*num2;
float fm1 = fb*num1;
p = (fm2-fm1)/(fa-fb);
printf("%f",fa);
return (p);
}
float main()
{
double num1;
double num2;
float fa;
float fb;
float p1;
clrscr();
printf("Enter number 1: \n");
scanf("%d", &num1);
getch();
printf("Enter number 2: \n");
scanf("%d", &num2);
getch();
clrscr();
fa = sinfa(num1);
printf("%f \n",fa);
getch();
fb = sinfa(num2);
printf("%f",fb);
getch();
clrscr();
p1 = tp(fa,fb,num1,num2);
printf("%f",p1);
getch();
}
i kept getting 0 from the function tp, and the parameters does not enter when i sent them any ideas why? since for sinfa the parameters are sent and it returns a value
thank you
You need to give types to the function parameters.
float tp(float fa,float fb,int num1,int num2)
Otherwise they are considered int and that leads to confusing effects.
Similarly you should fix
float sinfa(int num1, int num2)
This wont cause a problem, but it is always good to be explicit about what you mean.
always keep one thing in mind, make the habit of mentioning data type
of variables at the time of function declaration & function
defination. here you have not mentioned type that means it is
considering it as default dat typa of int.
float tp(fa,fb,num1,num2)
^^missing data type at all parameter, mention data type

Resources