You will create a program which tracks the balance and number of transactions for a gift card.
Question:
You will create a program which tracks the balance and number of transactions for a gift card.
Display a prompt to the Terminal user and scan in (using the scanf( ) command) the initial value of the gift card. Scan this value as a float, into a variable called “size”.
Check to see if the user enters a gift card size greater or equal to $20.00. If they do not, display an error prompt which asks them to re-enter a value and then rescan. You must use a while-loop; you should keep prompting the user for a valid gift card value until one is obtained. Store the new value into “size”.
After your loop is finished (and “size” has a valid value), create another float variable called “bal”, which will be used to represent the current balance of the gift card. Initialize “bal” by setting it equal to “size”.
Create a custom function which will compute the new balance of the card, when given the previous balance and the charge amount. The new balance will simply be the difference of the two inputs.
The function should have two inputs (both floats) and should have a float output. Write your function prototype near the top of your C-file, above the “main” and write your function code below “main”.
Create two additional variables: “chrg” (a float) and “trans” (an int). These variables will be used to represent a charge amount to the card and count the number of transactions, respectively. Next, set up a while-loop to repeatedly do the following tasks:
Display, to the Terminal, the current balance on the gift card by printing out the “bal” variable. Print the value with a precision of 2 and place a dollar symbol ($) before it.
On a separate line, prompt the user to enter a value for next transaction charge value. Scan in the transaction charge dollar amount (as a float) and store it into the “chrg” variable.
The current transaction number (the value of “trans”) should be printed in the prompt and should start at 1 on the first loop pass. The value of “trans” is essentially a while-loop counter.
Update the balance of the card (the “bal” variable), using your function from part (c). Pass into the function the current balance and the charge amount from (ii) and use the returned function value to update the balance of the card (overwrite the value stored in “bal”).
Increment the transaction counter variable, “trans”, by one.
Your while-loop should stop when the balance of the card (“bal”) is less than or equal to zero. You do NOT need to check user input for the “chrg” value; assume the inputs are always valid and positive.
After the balance of your card reaches zero (and the loop above exits), print, to the Terminal, the remaining balance due (with a $ sign and a precision of 2). This should be the negative value of “bal” after the above while-loop exits. Do NOT use the absolute-value function, abs( ).
Open a text file for write-access, called “card.txt”. Verify the file opens correctly by checking for a NULL file pointer. Display an error message and return a value of -1 if unsuccessful.
Next, print the original size of the gift card (stored in variable “size”) to the text file with a “$” symbol and a precision of 2. On the second line of the text file, print the number of transactions which occurred on the gift card. You may use simple newline characters () when printing. Close the file when finished.
#include "stdio.h"
float updateCard(float, float);
int main(void)
{
//part a
printf("Enter the size of the gift card ($): ");
float size;
scanf("%f",&size);
// Code is not shown - Please complete it
return 0;
}
Intermediate Accounting
ISBN: 978-1118742976
16th edition
Authors: Donald E. Kieso, Jerry J. Weygandt, Terry D. Warfield