Question

*This question was posted earlier but I want to make sure my code differs from the...

*This question was posted earlier but I want to make sure my code differs from the other answer*

CS50 Assignment 3 and Assignment 4 (Both Due By Sunday March 29 11:59 PM) Assignment 3: Bank Application In a recent lecture, we discussed getting input from the user by using the scanf() function. For your reference, I have included a simple example of using scanf() below: scanf() example: #include int main(void) { int number; printf("Enter a number: "); //prompt the user for a number scanf("%d", &number); //store the number the user enters into number printf("%d\n", number); //print out the number that the user entered return 0; } Once you have a good understanding of scanf, please use it to simulate a bank account program.

The program should behave as follows: • Upon startup, the program should ask the user how much money they want to start off with in their bank account as shown here: Welcome to SMC Banking! Thanks for opening an account with us! How much money would you like to start with: • The rest of the examples assume the user entered $1000 for the initial amount. Your program should then prompt the user as so.

Please select from an option below 1. Withdraw 2. Deposit 3. View Balance Enter option: • The user should then enter a 1, 2, or 3 If the user enters a 1 (Withdraw) • Ask them for the amount of money they want to withdraw as shown below: You currently have $1000 Enter amount to withdraw: • If they enter a value less than they currently have in the bank account, subtract it from the amount they have and show them their new balance and end the program as shown below (assume user entered $200 to withdraw): You withdrew $200 Would you like a receipt (y or n): • If user enters y, show them their balance and wish them a great day and end the program Your current balance is $800 Have a great day! • If the user enters n, simply wish them a great day WITHOUT showing them their balance and end the program Have a great day! • HOWEVER IF THE USER TRIES TO WITHDRAW MORE THAN THEY HAVE IN THEIR ACCOUNT, show them the following message and end the program: You have tried to withdraw more than you have, sorry :( Have an OK day! If the user enters a 2 (Deposit) • Show them their current balance and let them enter the amount of money they want to deposit You currently have $1000 Enter amount to deposit: • Then ask if they want a receipt (assume the user deposited $200) Would you like a receipt (y or n): • If user enters y, show them their balance and wish them a great day and end the program Your current balance is $1200 Have a great day! • If the user enters n, simply wish them a great day WITHOUT showing them their balance and end the program Have a great day! If the user enters a 3 (View Balance) • Show the user’s balance Your current balance is $1000 • Ask them if they would like to withdraw, deposit, or quit Please choose an option below: 1. Withdraw 2. Deposit 3. End Transaction Enter option: • If they enter 1 or 2, follow the steps from above for Withdrawing or Depositing, • if they enter 3, simply wish them a great day! Have a great day!

Hints for Assignment 3 It can be helpful to structure your program in a way that matches the assignment description. For example, each option (withdraw, deposit, view balance) have different flows to them that are almost independent but still share some similarities. Seems kind of modular. If I recall correctly, aren’t functions in C also modular…� ? Also, I use the word If A LOT in the assignment description, have we seen if before in C…� ?

Assignment 4: Input Validation Using Loops Input validation is the process of checking the user’s input for a valid value before trying to use the data entered by the user. For example, in the Bank Application above, what should happen if the user tries to enter option 4 which doesn’t exist (Just to remind you, 1 is Withdraw, 2 is Deposit, 3 is View Balance) For this assignment, you should add input validation to your Bank Application such that it makes sure that the user enters only a valid value at each step. Your program should also repeatedly ask the user to enter a value until they enter a valid value. For example, when asking to choose an option, your program should work as follows Please select from an option below 1. Withdraw 2. Deposit 3. View Balance Enter option: 4 Invalid Option Selection Please select from an option below 1. Withdraw 2. Deposit 3. View Balance Enter option: -2 Invalid Option Selection Please select from an option below 1. Withdraw 2. Deposit 3. View Balance Enter option: 3 • Your program should then resume execution as if the user entered 3 correctly the first time (basically start executing the code for View Balance) • You should also perform input validation in ALL places where the user can enter a value as shown below in different examples Example (when selecting an option after viewing balance) Please choose an option below: 1. Withdraw 2. Deposit 3. End Transaction Enter option: 4 Invalid Option Selection Please choose an option below: 1. Withdraw 2. Deposit 3. End Transaction Enter option: -5 Invalid Option Selection Please choose an option below: 1. Withdraw 2. Deposit 3. End Transaction Enter option: a Invalid Option Selection Please choose an option below: 1. Withdraw 2. Deposit 3. End Transaction Enter option: 2 Example (When trying to Withdraw more than I have) You currently have $1000 Enter amount to withdraw: 2000 Insufficient Funds You currently have $1000 Enter amount to withdraw: -5 Invalid Amount You currently have $1000 Enter amount to withdraw: $200 Example (When trying to Deposit Negative Value) You currently have $1000 Enter amount to deposit: -5 Invalid amount Enter amount to deposit: 25 Example (When entering y or n ) You currently have $1000 Would you like a receipt (y or n): b Invalid entry Would you like a receipt (y or n): 97 Invalid entry Would you like a receipt (y or n): y

Hints for Assignment 4 Input validation seems like it repeats asking the user for inputs. Is there something we can use in C that repeats code execution…� ? (The answer is YES)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Assignment 3:

// C program to simulate the banking operation
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
// function declaration
void withdraw(int balance);
void deposit(int balance);
void input_receipt(int balance);

int main()
{
int balance;
int choice;

printf("Welcome to SMC Banking!\nThanks for opening an account with us!");
printf("\nHow much money would you like to start with: ");
scanf("%d",&balance); // input of start balance

// display the menu
printf("\nPlease select from an option below:\n1. Withdraw\n2. Deposit\n3. View Balance");
printf("\nEnter option: ");
scanf("%d",&choice); // input of menu choice

if(choice == 1) // withdraw
{
withdraw(balance);

}
else if(choice == 2) // deposit
{
deposit(balance);

}
else // view balance
{
printf("Your current balance is $%d",balance);
printf("\nPlease select from an option below:\n1. Withdraw\n2. Deposit\n3. End Transaction");
printf("\nEnter option: ");
scanf("%d",&choice);

if(choice == 1)
{
withdraw(balance);

}
else if(choice == 2)
{
deposit(balance);

}else
printf("Have a great day!");
}

return 0;
}

// function to withdraw amount from balance
void withdraw(int balance)
{
int amount;

printf("You currently have $%d. Enter amount to withdraw: ",balance);
scanf("%d",&amount); // input of amount to withdraw


// validate the amount
if(amount < 0 || amount > balance)
printf("You have tried to withdraw more than you have, sorry :(\nHave an OK day!");
else
{
balance -= amount;
printf("You withdrew $%d",amount);
input_receipt(balance);
}
}

// function to deposit amount in balance
void deposit(int balance)
{
int amount;
printf("You currently have $%d. Enter amount to deposit: ",balance);
scanf("%d",&amount);

balance += amount;
printf("You deposited $%d",amount);
input_receipt(balance);
}

// function to print the receipt if the user wants
void input_receipt(int balance)
{
char receipt;
printf("\nWould you like a receipt (y or n): ");
scanf(" %c",&receipt);

if(tolower(receipt) == 'y')
{
printf("Your current balance is $%d",balance);
}
printf("\nHave a great day!");
}

//end of program

Output:

Assignment 4:

// C program to simulate the banking operation
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
// function declaration
void withdraw(int balance);
void deposit(int balance);
void input_receipt(int balance);

int main()
{
int balance;
int choice;

printf("Welcome to SMC Banking!\nThanks for opening an account with us!");
printf("\nHow much money would you like to start with: ");
scanf("%d",&balance); // input of start balance


while(balance < 0)
{
printf("Balance cannot be negative.");
printf("\nHow much money would you like to start with: ");
scanf("%d",&balance);
}

// display the menu
printf("\nPlease select from an option below:\n1. Withdraw\n2. Deposit\n3. View Balance");
printf("\nEnter option: ");
scanf("%d",&choice); // input of menu choice

while(choice < 1 || choice > 3)
{
printf("Invalid option");
printf("\nEnter option: ");
scanf("%d",&choice);
}

if(choice == 1) // withdraw
{
withdraw(balance);

}
else if(choice == 2) // deposit
{
deposit(balance);

}
else // view balance
{
printf("Your current balance is $%d",balance);
printf("\nPlease select from an option below:\n1. Withdraw\n2. Deposit\n3. End Transaction");
printf("\nEnter option: ");
scanf("%d",&choice);

while(choice < 1 || choice > 3)
{
printf("Invalid option");
printf("\nEnter option: ");
scanf("%d",&choice);
}

if(choice == 1)
{
withdraw(balance);

}
else if(choice == 2)
{
deposit(balance);

}else
printf("Have a great day!");
}

return 0;
}

// function to withdraw amount from balance
void withdraw(int balance)
{
int amount;

printf("You currently have $%d. Enter amount to withdraw: ",balance);
scanf("%d",&amount); // input of amount to withdraw

while(amount < 0 || amount > balance)
{
printf("Invalid Amount");
printf("\nYou currently have $%d Enter amount to withdraw: ",balance);
scanf("%d",&amount);
}

// validate the amount
balance -= amount;
printf("You withdrew $%d",amount);
input_receipt(balance);
}

// function to deposit amount in balance
void deposit(int balance)
{
int amount;
printf("You currently have $%d. Enter amount to deposit: ",balance);
scanf("%d",&amount);

while(amount < 0)
{
printf("Invalid Amount");
printf("\nYou currently have $%d Enter amount to deposit: ",balance);
scanf("%d",&amount);
}

balance += amount;
printf("You deposited $%d",amount);
input_receipt(balance);

}

// function to print the receipt if the user wants
void input_receipt(int balance)
{
char receipt;
printf("\nWould you like a receipt (y or n): ");
scanf(" %c",&receipt);


while(tolower(receipt) != 'y' && tolower(receipt) != 'n')
{
printf("Invalid entry.\nWould you like a receipt (y or n): ");
scanf(" %c",&receipt);
}

if(tolower(receipt) == 'y')
{
printf("Your current balance is $%d",balance);
}
printf("\nHave a great day!");
}

//end of program

Output:

Welcome to SMC Banking! Thanks for opening an account with us! How much money would you like to start with: -2000 Balance can

Add a comment
Know the answer?
Add Answer to:
*This question was posted earlier but I want to make sure my code differs from the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help modifying this code please ASAP using C++ Here is what I missed on this code below Here are the instruction...

    I need help modifying this code please ASAP using C++ Here is what I missed on this code below Here are the instructions Here is the code Produce correct70 pts O pts Full Marks No Marks results and statisfy requirements view longer Comments 1. Your display amount is not readable 2. I withdraw more than my balance, but I didn't see any error message description Documentations10 pts 0 pts Full Marks No Marks : comment i code and block comment...

  • Trying to figure out how to complete my fourth case 4 (option exit), write a function...

    Trying to figure out how to complete my fourth case 4 (option exit), write a function that display a “Good Bye” message and exits/log out from user’s account displaying a menu (sign in, balance, withdraw and exit.. #include<iostream> #include <limits> using namespace std; void printstar(char ch , int n); double balance1; int main() { system("color A0"); cout<<"\n\t\t ========================================="<< endl; cout<<"\t\t || VASQUEZ ATM SERVICES ||"<< endl; cout<<"\t\t ========================================\n\n"<< endl; int password; int pincode ; cout<<" USERS \n"; cout<<" [1] -...

  • I have updated my previously posted C++ question to ensure that I have included all of...

    I have updated my previously posted C++ question to ensure that I have included all of the necessary documentation in order to complete Part 2 - Bank Account of the assigned lab in its entirety. Please note that the lab below is somewhat lengthy, and I do not expect you to answer each question! If coding the full lab is too much to ask, please focus soley on Part 2 of the lab, titled Bank Account and the subsections that...

  • Create a class named UserAccounts that defines an array of 8 accounts as an instance variable....

    Create a class named UserAccounts that defines an array of 8 accounts as an instance variable. In the default constructor of this class, write a loop that creates 8 accounts with ids 1 through 7 and initial balance of $50 for each account. Store these accounts in the array. When the program runs, it asks the use to enter a specific id. When the user enters a correct id, the system displays a menu as shown in the sample run...

  • Question 3.1 Draw the class diagram for the ATM program in Question 2.1. Please find attached...

    Question 3.1 Draw the class diagram for the ATM program in Question 2.1. Please find attached the scenario in the photos. this is for programming logic and design Scenario A local bank intends to install a new automated teller machine (ATM) to allow users (i.e., bank customers) to perform basic financial transactions (see below figure). Each user can have only one account at the bank. ATM users should be able to do the following; View their account balance. Withdraw cash...

  • Textual menus work by showing text items where each item is numbered. The menu would have...

    Textual menus work by showing text items where each item is numbered. The menu would have items 1 to n. The user makes a choice, then that causes a function to run, given the user made a valid choice. If the choice is invalid an error message is shown. Whatever choices was made, after the action of that choice happens, the menu repeats, unless the menu option is to quit. Such kind of menus are displayed from the code under...

  • Assignment 10 Assignments Guidelines This assignment has ONE question. Make sure you answer it an...

    IN Visual Basic please share also Desiger.vb Assignment 10 Assignments Guidelines This assignment has ONE question. Make sure you answer it and include comments. You will lose-10% ofyour grade in this assignment if the comments are missing Bill Calculator You need to design an application that user can enter his/her name, select their level: graduate vs. undergraduate, then they would enter the bill amount. See picture belovw Bill Calculator Please enter your Full name O Graduate Student Undergraduate Student Total...

  • I would love it if someone would make this code from scratch.. not copied from somewhere...

    I would love it if someone would make this code from scratch.. not copied from somewhere else. I just want this explained and how it is done. The extra credit would be nice too.. This is C++ ASSIGNMENT: A7 - Student Line Up "Student Line Up", page 295, Number 14 A teacher has asked all students to line up single file according to their first name. For example, in one class Amy will be in the front of the line...

  • Programming Assignment #2 EE 2372 MAKE SURE TO FOLLOW INSTRUCTIONS CAREFULLY, IF YOU HAVE ANY DOUBTS...

    Programming Assignment #2 EE 2372 MAKE SURE TO FOLLOW INSTRUCTIONS CAREFULLY, IF YOU HAVE ANY DOUBTS PLEASE EMAIL ME! This programming assignment will just be a small extension to programming assignment 1. In the first assignment each of the functions could only take a predefined number of inputs. This time the objective is to be able to complete these functions with any amount of inputs. The user will pass arguments through the command line (this means you will have to...

  • answer in c++ code. use comments when writing the code to know what's your thinking. there is three pictures with information Write a C++ console program that defines and utilizes a cl...

    answer in c++ code. use comments when writing the code to know what's your thinking. there is three pictures with information Write a C++ console program that defines and utilizes a class named ATM. This program will demonstrate a composition relationship among classes because an ATM can contain many Accounts. You will need to reuse/modify the Account class you created in Assignment 10. The default constructor for the ATM class should initialize a private vector of 10 accounts, each with...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT