I was trying to ask the user if he/she wants another cupcake only if the cost of the first cupcake is less than their budget (maxPrice), but that does not work. It prints nothing after receiving user input. The error occurs where the program states: "if(anotherCupcake == "yes")" and it also occurs when it states, "anotherCupcake == "no".
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main(int argc, char**argv)
{
float maxPrice;
char flavor[20];
char flavor2[20];
float price;
double price2;
char anotherCupcake[20];
double newPrice;
// int idxToDel = 2;
//memove(&word[idxToDel], &word[idxToDel + 1], strlen(word) - idxToDel);
printf("Please enter starting budget: ");
scanf("%f", &maxPrice);
printf("Enter the flavor of cupcake 1: ");
scanf("%s", &flavor);
printf("Enter the price of %s cupcake: $", flavor);
scanf("%f", &price);
//determining if you have enough money for a second cupcake
if(price == maxPrice)
{
printf("\nOops! No more money left to spend on anymore cupcakes...enjoy the %s one! Bye!", flavor);
}
else if(price > maxPrice)
{
newPrice=maxPrice-price;
printf("\nWow, you overspent on that first cupcake...you owe $%.2lf. Bye!", fabs(newPrice)*1.0);
}
else
{
printf("Ok, looks like you you have $%.2f left to spend on cupcakes... Would you like to get another one?", maxPrice - price);
scanf("%s", &anotherCupcake);
//figuring out what second cupcake you want
if(anotherCupcake == "yes")
{
printf("Enter the flavor of cupcake 2: ");
scanf("%s", &flavor2);
printf("Enter the price of cupcake 2 $: ");
scanf("%f", &price2);
printf("Done! Enjoy your %s and %s cupcakes! Bye!", flavor, flavor2);
}
if(anotherCupcake == "no")
{
printf("Ok then...Enjoy that one %s cupcake! Bye!", flavor);
}
}
return 0;
}
try use string compare to compare values of two string. It may work.
if(strcmp("yes", anotherCupcake) == 0)
{
printf("Enter the flavor of cupcake 2: ");
scanf("%s", &flavor2);
printf("Enter the price of cupcake 2 $: ");
scanf("%f", &price2);
printf("Done! Enjoy your %s and %s cupcakes! Bye!", flavor, flavor2);
}
Related
My program checks to see if the max price is bigger than the price you spent. If the max price is bigger than the price you spent it turns the absolute value of two floats; but when I use fabs(), returns "Wow, you overspent on that first cupcake...you owe $0.00. Bye!".
I also tried abs(), but that didn't work.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main(int argc, char**argv)
{
float maxPrice;
char flavor[20];
char flavor2[20];
float price;
double price2;
char anotherCupcake[20];
double newPrice;
newPrice=maxPrice-price;
// int idxToDel = 2;
//memove(&word[idxToDel], &word[idxToDel + 1], strlen(word) - idxToDel);
printf("Please enter starting budget: ");
scanf("%f", &maxPrice);
printf("Enter the flavor of cupcake 1: ");
scanf("%s", &flavor);
printf("Enter the price of %s cupcake: $", flavor);
scanf("%f", &price);
//determining if you have enough money for a second cupcake
if(price == maxPrice)
{
printf("\nOops! No more money left to spend on anymore cupcakes...enjoy the %s one! Bye!", flavor);
}
else if(price > maxPrice)
{
printf("\nWow, you overspent on that first cupcake...you owe $%.2lf. Bye!", fabs(newPrice)*1.0);
}
else
{
printf("Ok, looks like you you have $.2lf left to spend on cupcakes... Would you like to get another one?", maxPrice - price);
scanf("%s", &anotherCupcake);
if(anotherCupcake == "yes")
{
printf("Enter the flavor of cupcake 2: ");
scanf("%s", &flavor2);
printf("Enter the price of cupcake 2 $: ");
scanf("%f", &price2);
printf("Done! Enjoy your %s and %s cupcakes! Bye!", flavor, flavor2);
}
else
{
printf("Ok then...Enjoy that one %s cupcake! Bye!", flavor);
}
}
return 0;
}
You have to calculate newPrice with current values of
maxPrice-price, not at the beginning when neither maxPrice nor
price are initialized. Like that:
newPrice = maxPrice - price;
printf("\nWow, you overspent on that first cupcake...you owe $%.2lf. Bye!\n", fabs(newPrice)*1.0);
It'll work like that:
$ ./main
Please enter starting budget: 10
Enter the flavor of cupcake 1: 1
Enter the price of 1 cupcake: $18
Wow, you overspent on that first cupcake...you owe $8.00. Bye!
As a sidenote, you didn't specify what compiler you're using but the minimum set of flags you should use with gcc/clang is -Wall -Wextra -pedantic and fix all warnings.
I want to create a menu that shows:
Client details
Property details
Exit
#include <stdio.h>
#include <conio.h>
void main() {
char L,F,H;
float CID,Aoc,Pte,Cost_per_sqft;
int dicnt,age,ch;
printf("Enter the Client ID\n");
scanf("%f", &CID);
printf("Enter the age of client\n");
scanf("%f", &Aoc);
if (age >=60) {
printf("The client is eligible for a discount\n");
} else if (age<60) {
printf("The client is not eligible for a discount\n");
} {
printf("Select Porperty type\nF=Flat\nL=Land\nH=House\n");
scanf("%f", &Pte);
}
printf("Please select the menu option\n");
printf("1.Client ID\n");
printf("2.Property details\n");
printf("3.Exit\n");
scanf("%d", &ch);
switch(ch) {
case 1:
printf("Client ID %f", CID);
printf("Age of client %f", Aoc);
}
}
It's not letting me enter the option to open a menu, also the age else is doesn't work because age => 60 is also showing not eligible for discount. The switch case doesn't work either.
Problem 1 is that you have defined two variables, float Aoc and int age, then attempt to use them interchangeably. Also, the first time age is referenced ( here: if (age >=60) ) it is uninitialized, which contributes to the problems you have described.
Addressing the following will fix the if-else statement for age, and will allow the menu to appear...
Since age is typically a non-float value, i.e. 45 or 50, but never expressed as 45.5.
Suggest replacing, Aoc everywhere it exists with age, (modifying the format specifiers accordingly), and finally, initialize age before use.
Problem 2 is here:
...
{ printf("Select Porperty type\nF=Flat\nL=Land\nH=House\n");
scanf("%f", &Pte);
You are prompting user to input a char value, then attempt to read it in into a float variable Pte. Suggest if desiring to read in as a char, use a " %c" format specifier, and change float Pte to char Pte.
(Note space in format specifier, it is there for this reason.)
{ printf("Select Porperty type\nF=Flat\nL=Land\nH=House\n");
scanf(" %c", &Pte);//note space in front of %c to consume newline
Working code adapted from your original:
void main()
{
char L,F,H;
float CID,Aoc;/*Pte*/
float Cost_per_sqft;
int dicnt,age,ch;
char Pte;
printf("Enter the Client ID\n");
scanf("%f", &CID);
printf("Enter the age of client\n");
scanf("%d", &age);
if (age >=60)
{
printf("The client is eligible for a discount\n");
}
else if (age<60)
{
printf("The client is not eligible for a discount\n");
}
{ printf("Select Porperty type\nF=Flat\nL=Land\nH=House\n");
scanf(" %c", &Pte);
}
printf("Please select the menu option\n");
printf("1.Client ID\n");
printf("2.Property details\n");
printf("3.Exit\n");
scanf("%d", &ch);
switch(ch)
{
case 1:
printf("Client ID %f\n", CID);
printf("Age of client %d", age);
break;
case 2:
;//add content
break;
case 3:
;//add content
break;
}
}
I am having problem with appending more records to this file. It allows me add just one record but I cannot add more than one record. And cannot figure out what is going wrong with it?
void new_customer()
{
char ch;
int flag=0;
FILE *fp;
fp=fopen("DataFile.txt", "a+");
printf("Enter today's date (dd/mm/yyyy) : ");
scanf(" %d/%d/%d", &add.deposit.day, &add.deposit.month, &add.deposit.year);
printf("Enter Account Number : ");
fflush(stdin);
scanf("%ld", &check.account_number);
while(fscanf(fp, "%ld %s %s %s %s %s %d %d/%d/%d %d %d/%d/%d %c", &add.account_number, add.customer_name, add.father_name, add.address, add.Nationality, &add.p_number, &add.age, &add.dob.day, &add.dob.month, &add.dob.year, &add.amount, &add.deposit.day, &add.deposit.month, &add.deposit.year, &add.account_type)!=EOF)
{
if(check.account_number==add.account_number)
{
printf("Account number already taken. Please contact administrator.\nPress enter to continue.");
getch();
system("cls");
main();
}
}
add.account_number=check.account_number;
printf("Enter name : ");
fflush(stdin);
gets(add.customer_name);
printf("Enter Father's name : ");
fflush(stdin);
gets(add.father_name);
printf("Enter your age : ");
fflush(stdin);
scanf("%d", &add.age);
printf("Enter Date of birth (dd/mm/yyyy) : ");
scanf("%d/%d/%d", &add.dob.day, &add.dob.month, &add.dob.year);
printf("Enter Phone Number : ");
fflush(stdin);
gets(add.p_number);
printf("Enter Nationality : ");
fflush(stdin);
gets(add.Nationality);
printf("Enter Address : ");
fflush(stdin);
gets(add.address);
printf("Enter Account Type:\nPress S for Savings, \nPress C for Current, \nF for Fixed : ");
fflush(stdin);
scanf("%c",&add.account_type);
while(flag!=1)
{
if (add.account_type=='S'|| add.account_type=='s'||add.account_type=='C'||add.account_type=='c'||add.account_type=='F'||add.account_type=='f')
{
flag=1;
}
else
{
printf("\nWrong Input. Input Again : ");
fflush(stdin);
scanf("%c", &add.account_type);
flag=0;
}
}
printf("Deposit Amount : ");
fflush(stdin);
scanf("%d", &add.amount);
fprintf(fp, "%ld %s %s %s %s %s %d %d/%d/%d %d %d/%d/%d %c\n", add.account_number, add.customer_name, add.father_name, add.address, add.Nationality, add.p_number, add.age, add.dob.day, add.dob.month, add.dob.year, add.amount, add.deposit.day, add.deposit.month, add.deposit.year, add.account_type);
printf("\nAccount Created Successfully!!\n");
fclose(fp);
while(1)
{
printf("Return to Main Menu? Y/N : ");
fflush(stdin);
scanf("%c", &ch);
if(ch=='Y' || ch=='y')
{
system("cls");
main();
}
else if(ch=='N' || ch=='n')
{
exit(0);
}
else
{
printf("\nWrong input. Try Again!\n");
}
}
}
This is just a function to a big program. I am attaching just the part which includes file handling. If you want I can attach more code.
Here I am adding the main driver code
#include<windows.h>
#include<stdio.h>
#include<stdlib.h>
#include "E:\Projects\C Language\Bank-Management-System\File Containing Functions.c"
int menu(void);
int gotoxy(int x, int y)
{
COORD c;
c.X=x;
c.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
int menu()
{
int i, a;
gotoxy(40,0);
printf("BANK MANAGEMENT SYSTEM\n");
gotoxy(43, 3);
printf(":: MAIN MENU ::\n");
gotoxy(40, 5);
printf("[1] Create a new account");
gotoxy(40, 6);
printf("[2] Update information of existing account");
gotoxy(40, 7);
printf("[3] Transactions");
gotoxy(40, 8);
printf("[4] Check details of existing account");
gotoxy(40, 9);
printf("[5] Remove existing account");
gotoxy(40, 10);
printf("[6] View Customer List");
gotoxy(40, 11);
printf("[7] Exit\n");
gotoxy(40, 15);
printf("Enter your choice : ");
scanf("%d", &a);
return a;
}
int main()
{
int choice;
choice=menu();
switch(choice)
{
case 1:
{
system("cls");
new_customer();
break;
}
case 3:
{
system("cls");
transaction();
break;
}
case 7 :
{
system("cls");
printf("Thank You for using our services!!");
exit(0);
}
default:
{
printf("Wrong Input!!\n");
getch();
system("cls");
menu();
}
}
getch();
return 0;
}
Link to see input : https://pasteboard.co/Jt3xWrP.jpg
Here is the file after first input : https://pasteboard.co/Jt3yYA9.jpg
Another input :https://pasteboard.co/Jt3yHSC.jpg (this is where it gets stuck forever and doesn't let me add another record)
Text stored inside file :
123 John Papa John 15, Yemen Road, Yemen USA 12345678 22 11/2/0 2000 27/9/2020 S
So to start with the file holds:
123 John Papa John 15, Yemen Road, Yemen USA 12345678 22 11/2/0 2000 27/9/2020 S
And you try to scan it with
fscanf(fp, "%ld %s %s %s %s %s %d %d/%d/%d %d %d/%d/%d %c"
That is:
1 number
5 strings
1 number
...
As you can see, the file doesn't match that.
123 John Papa John 15, Yemen Road, Yemen USA 12345678 22 11/2/0 2000 27/9/2020 S
^^^ ^^^ ^^^ ^^^ ^^^ ^^^ ^^^
ok ok ok ok ok ok Not ok, so stop here
The next fscanf will not match anything (it expects a number but the file has "Road") so nothing is read.
In other words, you are stuck in the while forever.
I'll recommend reading the file in a line by line manner using fgets.
Then you can (in principle) use sscanf afterwards. But notice that %s reads a single word and your code allow multiple words for a single entry! In other words - your file can't be parsed using %s.
So consider another file format. For instance, you could use 1 line for each item in a record. That will make parsing much easier.
Basically I'm a beginner coder and this is what I wrote:
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("COLOR 0A");
char playerName[13];
char playerGender;
int playerAge;
printf("Please input your name and age!\nName: ");
scanf("%s", playerName);
printf("Age (from 18 to 50): ");
scanf("%d", &playerAge);
label:
if(playerAge > 18 && playerAge < 50)
{
printf("What gender are you, M(male) or F(female): ");
scanf("%c", playerGender);
gender:
if(playerGender == 'M' || playerGender == 'F'){
printf("Okay, so your name is %s, you're %d years old and you're a %s.", playerName, playerAge, playerGender);
}else{
printf("Try again.\n\n"
"What gender are you, M(male) or F(female): ");
scanf("%c", playerGender);
goto gender;
}
}else{
printf("Wrong, try again.\n"
"Age (from 18 to 50): ");
scanf("%d", &playerAge);
goto label;
}
return 0;
}
When I put the required age to continue, it crashes on the scanf for the playerGender. Right after it shows me the question about my gender? Where is my mistake?
try:
scanf("%c", &playerGender);
instead of
scanf("%c", playerGender);
as scanf takes a pointer and not a reference to the variable you are trying to fill.
I am also beginner but I think that you need to write thisscanf("%s", playerName); like thisscanf("%12s", playerName);
BTW let me know if that works.
This is a program that asks the user to input Shipping information about selling bikes, pretty lame. At the end when it prints out the number of bikes order, and the total cost, the numbers get screwed up. The previously entered amounts seem to be sticking in the memory. How do I fix this? If that is not the problem I would not mind being told so :)
#include <stdio.h>
#include <math.h>
//structure
typedef struct
{char cust_name[25];
char add_one[20];
char add_two[20];
}ORDER;
ORDER order;
int main(void){
fflush(stdin);
system ( "clear" );
//initialize variables
double number_ordered = 0;
double price;
char bike;
char risky;
double m = 359.95;
double s = 279.95;
//inputs for order
printf("Enter Customer Information\n");
printf("Customer Name: ");
scanf(" %[^\n]s", &order.cust_name);
printf("\nEnter Street Address: ");
scanf(" %[^\n]s", &order.add_one);
printf("\nEnter City, State, and ZIP: ");
scanf(" %[^\n]s", &order.add_two);
printf("\nHow Many Bicycles Are Ordered: ");
scanf(" %d", &number_ordered);
printf("\nWhat Type Of Bike Is Ordered\n M Mountain Bike \n S Street Bike");
printf("\nChoose One (M or S): ");
scanf(" %c", &bike);
printf("\nIs The Customer Risky (Y/N): ");
scanf(" %c", &risky);
system ( "clear" );
//print order
printf("\n**********Shipping Instructions**********");
printf("\nTo: %s\n %s\n %s", order.cust_name, order.add_one, order.add_two);
if (bike == 'M' || bike == 'm')
printf("\n\nShip: %d Mountain Bikes", number_ordered);
else
printf("\n\nShip: %d Street Bikes", number_ordered);
if (bike == 'M' || bike == 'm')
price = number_ordered * m;
else
price = number_ordered * s;
if (risky == 'Y' || risky == 'y')
printf("\nBy Freight, COD %d\n", price);
else
printf("\nBy Freight, And Bill The Customer %d\n", price);
printf("*****************************************\n");
return 0;
}
You are printing number_ordered and price, which are doubles, using %d. %d is only for integer types. Use %lf to printf or scanf doubles.
The formats for both your scanf and your printf are wrong, so you're neither reading nor writing your values properly.