I'm trying to make a simple banking program that allows the user to choose from a list of 4 options, and then depending on what the user chose, it will do that operation and then ask if the user wants another option. I also have to track what dates are entered, and make sure that an earlier date can't be entered. I have the user inputs set up, but I'm having a hard time finding a way to get the options to work, as well as set up the date checking. I thought about putting the options in a while loop but wasn't sure if that would work. Can anyone offer some help?
#include <stdio.h>
#define begin_amount 2000
int main(void) {
int debit, credit, current_date, debit_sum, credit_sum, option; //initialize variables for withdrawing and depositing
int new_date = 1;
int x = 3;
printf("what option do you want?\n");
scanf("%d", &option);
printf("Deposit\n");
printf("Withdrawl\n");
printf("Print Statement\n");
printf("Interest\n");
//deposits
printf("Please enter todays date?\n"); //ask the user for today's date
scanf("%d", ¤t_date);
printf("how much do you want to credit to your account?\n"); //ask them to input how much they want to deposit
scanf("%d", &credit);
printf("Your new balance is %d\n", begin_amount+credit); //print their new balance
//Withdrawls
printf("Please enter todays date?\n"); //askt he user to enter today's date
scanf("%d", ¤t_date);
printf("how much do you want to debit to from account?\n"); //ask them how much they want to withdraw
scanf("%d", &debit);
printf("Your new balance is %d\n", begin_amount-debit); //print their new balance
//counting amount of withdrawls and deposits
printf("Please enter today's date?\n"); //ask the user to enter today's date
scanf("%d", ¤t_date);
return 0;
}
Well I'd recommend creating functions for all of the options, but if you just want a simple program then just have a while loop with a switch loop inside.
i.e.
while (true) { scanf("%d",&option); switch(option) { case 1: //deposit break; } };
Where you can treat each option as their own case.
Related
I'm writing a function that will give the user an option to choose an item from a list.
When an option is chosen it should then call a dedicated function to ask for the quantity of the item and then output it to a file. Below are the two functions.
void pos2()
{
int choice;
printf("\n Enter The item : ");
scanf("%d", &choice);
switch (choice) {
case 1:
apple();
break;
case 2:
editInventory();
break;
case 3:
printf("\n Returning... \n\n");
printf("Returning in 3 seconds...\n");
Sleep(3000);
system("cls");
printMenu();
default:
system("cls");
printf("\ninvalid choice Try again \n");
printMenu();
}
}
void apple()
{
FILE*out=fopen("pos.txt","w");
int amt;
printf("Apple Choosen\n");
printf("Enter the Amount\n");
scanf("%d",&amt);
fprintf(out,"%d",&amt);
}
In this case, the user is only able to choose 1 at the moment which will ask them to enter the number of apples, and then enter, it would save the value to a text file called pos.txt. When I do enter an amount it appears I'm given the address value or some sort of array in return. This is the output in the text file:
6421716
if anyone can offer assistance or guide me in the right direction that would be appreciated. Thanks in Advance
Great News, #Passerby (https://stackoverflow.com/users/17196203/passerby) commented the solution under my Question.
The issue here was the & in my fprintf(out,"%d",&amt); which was causing the weird number to be outputted. Thanks once again to everyone helping me solve this and a special thanks to #Passerby for the solution.
I'm doing my c assignment and I have faced some problems. One of it is that I can't printf the value that are retrieved from the user. The value retrieved is in for loop and for the output it only shows the latest value that are entered by user. For this project, I can't use array or function. Someone please help me :(
printf("\n\nPlease Enter the number of bookings that you would like to make : ");
scanf("%d" , &noOfBooking );
//for control structures
for(i=1; i<=noOfBooking; i++){
printf("\nBooking %d" , i);
printf("\nSelect Room Type Option: ");
scanf("%d" , &option);
printf("Number of days: ");
scanf("%d" , &days);
printf("Number of person staying in the room: ");
scanf("%d" , &noPerson);
//selection structures
if(option==1)
{
ratePerDay = 75.00;
countSB--;
}
else if(option==2)
{
ratePerDay = 80.00;
countQB--;
}
else if(option==3)
{
ratePerDay = 100.00;
countKB--;
}
else if(option==4)
{
ratePerDay = 150.00;
countFR--;
}
else
printf("You have entered invalid input");
printf("Your Payment Booking are as follows :");
printf("\nRoom Type Option %d", option);
}
The expected output should be like this: -
Please Enter the number of bookings that you would like to make : 2
Booking 1
Select Room Type Option: 1
Number of days : 2
Number of person staying in the room : 1
Total Cost : 150.00
Booking 2
Select Room Type Option: 4
Number of days : 1
Number of person staying in the room : 3
Total Cost : 150.00
Your Payment Booking are as follows :
Room Type Option 1 (Single Bed) Total Cost is RM 150.00
Room Type Option 4 (Family Room) Total Cost is RM 150.00
I was thinking about using structs and the switch/case statement to solve your problem as they are not arrays or functions and you could store all the required information and afterwards handle the if/else if statements. For example:
// structs
// arguments related to your code
struct Booking {
int option;
int days;
int noPersons;
int num_of_Booking;
};
// switch-case
switch(option){
case 1:
ratePerDay = 75.00;
countSB--;
break; // optional - to terminate the switch statement
case 2:
...
.
.
case x:
...
default: // it works like the last else if your code
printf("You have entered invalid input");
As Paul said please provide some additional information on what you are trying to achieve in order for me to be more specific.
I hope that this helps you.
You can put every value in the same variable, for example, you can put the number 9 between every value that entered by the user.
For example if the user entered the options (1,3,2,2,4,3), the option value can be save in this way: option -> 193929294939
and than you will know every value that the user has entered.
New to C and am attempting to make a Calculator for a homework assignment. However, I am running into some problems
So far I have only attempting testing the add option but am still coming up with an Input/Output error when attempting to do so.
I am attempting to make a calculator that looks something like this:
Enter Number One:
Enter Number Two:
1-Add
2-Subtract
3-Multiply
4-Divide
5-Mod
Enter your choice:
Result=
int choice, result;
float x,y ;
int main() {
printf("Enter Number One:",&x);
scanf("%f",&x);
printf("Enter Number Two:",&y);
scanf("%f",&y);
printf("1-Add\n2-Subtract\n3-Multiply\n4-Divide\n5-Mod\n");
printf("Enter your choice:",&choice);
scanf("%d",&choice);
if (choice==1) {
result=x+y;
printf("Result=\n%f+%f=%f",x,y,result);
}
}
Enter Number One:1
Enter Number Two:2
1-Add
2-Subtract
3-Multiply
4-Divide
5-Mod
Enter your choice:1
Result=
0.000000+0.000000=0.000000read from master failed
: Input/output error
RUN FAILED (exit value 1, total time: 1s)
Before going on, I'd like to say that this is my first time here and I don't know how things work yet so please pardon any errors on my part.
When compiled,(source code below) everything works fine except for the content of the float disp which is equal to -1.#QNAN0. Any help on this? Thanks in advance. Some parts of the code are not complete like the switch-case structure. Please temporarily that(Unless it affects the result).
The source code for the C program:
#include <stdio.h>
#include <stdlib.h>
float moneyup(float m);
int main()
{
char name[20];
char x;
int y;
float disp;
int hunger;
printf("\t\t**********************************************\n");
printf("\t\t* *\n");
printf("\t\t* How To Get Rich Quick! *\n");
printf("\t\t* *\n");
printf("\t\t**********************************************\n");
printf("\nThis is an experimental command line interface game made by NayNay AKA Nathan\n");
printf("\nPlease pardon the poor user interface.");
for(;;)
{
printf("\nPlease enter your name(one only)");
scanf("%s", &name);
printf("\nThe name you entered is %s. Is this correct? (type y/n for yes or no)\n");
fflush(stdin);
x=getchar();
if(x=='y') /*This part with the for loop is used to get the name of the*/
{ /*user and confirm the correctness of that name. If the name is*/
printf("Okay! Moving on..."); /*wrong, the user has the option to change it. Bulletproofing used*/
break; /*here*/
}
else if(x=='n')
{
printf("Alright let's try again.");
continue;
}
else
{
printf("Let's try this again.");
continue;
}
}
printf("\nOkay %s, Let's get this story started",name);
printf("\n\nOne sad dreary morning, %s got up from sleep and went to the kitchen to get breakfast.");
printf("\nUnfortunately for him his pantry only contained a bunch of cockroaches going at it and laying their eggs everywhere");
printf("\nHe then checked his pockets and pulled out his last 5-dollar bill. That was all he had left,");
printf("\nHe bought a sandwich for $2 and decides to start a business with $3 as capital");
printf("\n\nChoose how to start");
printf("\n1. Begging.");
printf("\n2. Mow lawns.");
printf("\n3. Apply for post of newspaper boy.");
fflush(stdin);
y=getchar();
switch(y)
{
case '1':
printf("You begged for 6 hours and got $5.25\n");
disp=moneyup(5.25);
printf("You now have $%f\n",disp);
}
return 0;
}
float moneyup(float m)
{
float money;
money=(float)money+m;
return(money);
}
The variable money is uninitialized in the function moneyup when used in expression
money=(float)money+m;
I recently programmed this code (C) for a pretty simple calculator in Xcode.
It works mostly but does not display the answer for the sum/s.
The code is as follows:
#include <stdio.h>
int main()
{
//int's & chars-----------------
char SumMethod;
int firstnumber, secondnumber;
//int's & chars------------------
puts("Calculator v0.6");
printf("Please input first number: "); //Prompts 'firstnumber' input
scanf("%d", &firstnumber); //Scan's 'firstnumber' input and saves to '&firstnumber'
printf("Please input second number: "); //Prompts 'secondnumber' input
scanf("%d", &secondnumber); //Scan's 'secondnumber' input and saves to '&secondnumber
printf("d" "Please select Method: +(a), -(b), *(c), /(d): ");
scanf("%c", &SumMethod); //Scan's 'SumMethod' input
if(SumMethod=='a') {
printf ("%d",firstnumber + secondnumber); //This section detects the SumMethod and outputs the corrisponding sum
}
else if (SumMethod=='b'){
printf("%d",firstnumber-secondnumber);
}
else if (SumMethod=='c') {
printf("%d",firstnumber * secondnumber);
}
else if (SumMethod=='d') {
printf("%d",firstnumber / secondnumber);
}
}
For the solution i'm looking for a response that doesn't drastically change the code as how it is now is what I understand and because i'm relatively new to coding in general however if there is an obvious fix using useful common tools that would be much appreciated.
Thanks, Xenon
Your problem is that your comparisons are wrong. You need to compare your input to the LETTER a and not the (undefined and essentially random) values of the variables a,b,c,d.
so it is
if(SumMethod=='a')
instead of
if(SumMethod==a)
You don't need your variables a,b,c,d at all.