Problem: Scholarship Endowment Fund Part 2 (fund2.c) - c

Problem: Scholarship Endowment Fund Part 2 (fund2.c)
How do I return back to main menu and add a counter for the total number of donations and investment made, here's what i got?
Then, your program should allow the user the following options:
Make a donation
Make an investment
Print balance of fund
Quit
#include <stdio.h>
#include <stdlib.h>
//main functions
int main() {
// variables
int donation, investment, fund, total, tdonations, tinvestments;
// prompt user
printf("Welcome!\n");
printf("What is the initial balance of the fund\n");
scanf("%d", &fund);
int ans;
printf("What would you like to do?\n");
printf("\t1- Make a Donation\n");
printf("\t2- Make an investment\n");
printf("\t3- Print balance of fund\n");
printf("\t4- Quit\n");
scanf("%d", &ans);
if (ans == 1) {
printf("How much would you like to donate?\n");
scanf("%d", &donation);
}
if (ans == 2) {
printf("How much would you like to invest?\n");
scanf("%d", &investment);
return main();
}
if (ans == 3) {
total = donation + fund - investment;
if (total < fund) {
printf("You cannot make an investment of that amount\n");
return main();
}
else {
printf("The current balance is %d\n", total);
printf(" There have been %d donations and %d investments.\n", tdonations, tinvestments);
}
}
if (ans == 4) {
printf("Type 4 to quit\n");
}
else {
printf("Not a valid option.\n");
}
//switch
switch (ans) {
case 1:
printf("How much would you like to donate?\n");
scanf("%d", &donation);
return main();
case 2:
printf("How much would you like to invest\n");
scanf("%d", &investment);
return main();
case 3:
printf("The current balance is %d\n", total);
printf(" There have been %d donations and %d investments.\n", tdonations, tinvestments);
return main();
case 4:
break;
}
return 0;
}

You should put in a loop the part that you want to be repeated, using a varaible to decide when to stop.
here is an example, also in the switch costruct you should usa a default option, maybe to warn the user that the option they inserted is not valid.
int main() {
// variables
int donation, investment, fund, total, tdonations, tinvestments;
// prompt user
printf("Welcome!\n");
printf("What is the initial balance of the fund\n");
scanf("%d", &fund);
//loop until the user decide so
int exit = 0;
while(exit == 0){
int ans;
printf("What would you like to do?\n");
printf("\t1- Make a Donation\n");
printf("\t2- Make an investment\n");
printf("\t3- Print balance of fund\n");
printf("\t4- Quit\n");
scanf("%d", &ans);
//switch
switch (ans) {
case 1:
printf("How much would you like to donate?\n");
scanf("%d", &donation);
break;
case 2:
printf("How much would you like to invest\n");
scanf("%d", &investment);
break;
case 3:
total = donation + fund - investment;
if (total < fund) {
printf("You cannot make an investment of that amount\n");
}
else {
printf("The current balance is %d\n", total);
printf(" There have been %d donations and %d investments.\n", tdonations, tinvestments);
}
break;
case 4:
exit = 1;
break;
default:
printf("Incorrect option\n");
break;
}
}
return 0;
}

Related

In checkout function g_tot calculation brings garbage value

In this code at checkout function g_tot calculation brings garbage value. I think its because I'm calculating two variables from another two functions, but I don't know how to fix it. There's another error in restaurant function in if condition if I enter value more than 8 it'll bring garbage value to tot. But the most important one is
#include<stdio.h>
#include<conio.h>
// Global variables
int room,answr,days=0;
char name[20],choose;
int i=0,tot=0,p_tot=0,g_tot=0,z=0;
int p_price[2][5]={4000,10000,20000};
void screenheader()
{
printf("\n ::::::::::::::::::::::::::::::::::::::");
printf("\n :: ::");
printf("\n :: ############################ ::");
printf("\n :: # # ::");
printf("\n :: # WELCOME # ::");
printf("\n :: # TO # ::");
printf("\n :: # SURF HOTEL # ::");
printf("\n :: # # ::");
printf("\n :: ############################ ::");
printf("\n :: ::");
printf("\n ::::::::::::::::::::::::::::::::::::::");
}
void check_in()
{
int contact_No[20],NIC[10];
char first_name[10],last_name[10],Country[10];
system("cls");
screenheader();
printf("\n1. Packages");
printf("\n2. Room Allocation");
printf("\n3. Back\n\n");
scanf(" %d",&answr);
switch(answr)
{
case 1:{
system("cls");
printf("\n\n\nPer 2 Persons");
printf("\n\t\tPackage Name >>>> Couple");
printf("\n\t\tRs.4000/= per day");
printf("\n\t\tBed >>>> 1");
printf("\n\t\t *Tv Available");
printf("\n\n\n\n\t\t\nPer 4 Persons\n\t\tPackage Name >>>>
Family");
printf("\n\t\tRs.10,000/= per day");
printf("\n\t\tBed >>>> 2");
printf("\n\t\t*Tv Available \n\t\t*A/C \n\t\t*WIFI");
printf("\n\n\n\nPer 8 Persons\n\t\tPackage Name >>>> Deluxe");
printf("\n\t\tRs.20,000/= per day");
printf("\n\t\tBed >>>> 3 Large ");
printf("\n\t\t *Tv Available \n\t\t*A/C \n\t\t*WIFI\n\t\t*Local
Travel Guide\n\t\t*Balcony with a view\n\t\t*Writing desk");
printf("\n\n*Press 1 to go back");
getch();
check_in();
break;
}
case 2:{
printf("What package do you want?");
scanf(" %d",&i);
p_tot=p_tot+p_price[i-1];
if(i == 1)
{
printf("You have selected Couple package");
}
else if (i == 2)
{
printf("You have selected Family Package ");
}
else if (i == 3)
{
printf("You have selected Deluxe package");
}
else
{
printf("\n\nWrong input, please refer to packages and try
again.\nPress Enter to select another package");
getch();
check_in();
}
printf("\nEnter First Name:\n");
scanf(" %s",&first_name);
printf("Last Name:\n");
scanf(" %s",&last_name);
printf("How many days do you want to stay?");
scanf(" %d",&days);
printf("Enter your Country:");
scanf(" %s",&Country);
printf("Enter your NIC No:");
scanf(" %d",&NIC);
printf("Enter your Contact No:");
scanf(" %d",&contact_No);
printf("Hello %s %s you have booked a Room for
%d",&first_name,&last_name,days);
getch();
system("cls");
int main();
}
case 3: main();
}
}
void restaurant()
{
int fc[6];
char ans;
char food[8][30]={"Bread","Noodles","Salad","Popcorn","Chocolate ice
cream","Vanilla ice cream","Cold Coffee","Milk Shake"};
int price[8]={180,120,65,55,70,70,110,200};
printf("Press Enter To Continue To The Restaurant ");
getchar();
system("cls");
printf("\n\n\n\n\n\t *********");
printf("\n\t MENU CARD");
printf("\n\t *********\n\n\n");
printf("\n Food Code\t\tprice\t\t Food Name\n");
for(i=0;i<8;i++)
{
printf("\n\t\t%d",i+1);
printf("\t\tRs. %d",price[i]);
printf("\t\t%s",food[i]);
}
printf("\n\n\n\t *PRESS 0 TO GO TO THE MAIN MENU\n\t *PRESS 1 TO ORDER FOOD
: ");
scanf(" %d",&answr);
switch(answr)
{
case 0:
{
main();
break;
}
case 1:do
{
printf("\n\nENTER THE FOOD CODE YOU WANT TO HAVE :: ");
scanf("%d", &z);
if (z < 1 || z > 8)
{
printf("Invalid food code\n");
}
tot=tot+price[z-1];
printf("total so far is Rs.%d\n",tot);
printf("DO YOU WANT MORE(Y/N) ::");
scanf(" %c", &ans);
} while(ans=='y'|| ans=='Y');
printf("\n\nYour bill is Rs.%d",tot);
printf("\nYour bill will be added to the total bill at checkout");
printf("\n\nPress Enter to go back to main menu");
getch();
system("cls");
main();
}
}
void check_out()
{
system("cls");
screenheader();
printf("\n\nAre you sure you want checkout (Y/N)");
scanf(" %c",&choose);
if(choose=='n' || choose=='N')
{
main();
}
else if(choose== 'Y' || choose=='y')
{
system("cls");
screenheader();
g_tot=p_tot+tot;
printf("Total");
printf("%d",g_tot);
}
}
int main()
{
screenheader();
printf("\n\n\nPress Enter To Continue");
getchar();
system("cls");
screenheader();
printf("\n\n\n\n\t\t ************* \n");
printf("\t\t * MAIN MENU * \n");
printf("\t\t ************* \n\n\n");
printf("\t\t\t01. Check In \n");
printf("\t\t\t02.Restaurant\n");
printf("\t\t\t03.Checkout \n");
printf("\n\t\t\t04.Exit");
scanf(" %d",&answr);
switch(answr)
{
case 1:{
check_in();
break; }
case 2:{
restaurant();
break; }
case 3: {
check_out();
}
}
return 0;
}
if condition if I enter value more than 8 it'll bring garbage value to tot.
This is as expected. When z > 8, code attempts to access outside price[] range. Result: undefined behavior (UB). The prior if (z < 1 || z > 8) did not steer code away from price[z - 1]. Rest of code including g_tot = p_tot + tot; is now questionable.
int price[8] = {180, 120, 65, 55, 70, 70, 110, 200};
...
if (z < 1 || z > 8) {
printf("Invalid food code\n");
}
tot = tot + price[z - 1]; // UB here
...
g_tot = p_tot + tot;
Do not access price[z - 1] unless z in the range [1...8].
Other problems exist: Best to enable all compilers warnings and seem them (12+) yourself.

C Program are closing automatically

I created a C Program that will take all the order of the user then generate the grandtotal of the orders.
But when I will order another food, the program is closing automatically.
I don't know if this is about my getch or the breaks in my switch method. Sometimes, it will proceed to take another error but it automatically outputs "INVALID FOOD".
Here is my code:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void menu();
void foods();
void main();
char food;
int quantity;
float price;
float total;
float grandtotal;
int choice;
void main()
{
clrscr();
menu();
foods();
getch();
}
void menu(){
food = ' ';
quantity = 0;
price = 0;
total = 0;
choice = 0;
printf("Please select food from the following:\n");
printf(" B = Burger, F = French Fries, P = Pizza, S = Sandwiches \n");
printf("Enter food:");
scanf("%c", &food);
}
void foods(){
switch(food)
{
case 'B':
printf("You selected Burger!\n");
printf("Enter quantity:");
scanf("%d", &quantity);
price = 95.50;
printf("\n Do you want to order more? [1] Yes [2] No:");
scanf("%d", &choice);
total = price*quantity;
if(choice == 1){
menu();
break;
}
else if (choice == 2){
grandtotal = grandtotal + total;
printf("\n Total Price is: %0.2f", grandtotal);
break;
}
case 'F':
printf("You selected French Fries!\n");
printf("Enter quantity:");
scanf("%d", &quantity);
price = 47.75;
printf("\n Do you want to order more? [1] Yes [2] No:");
scanf("%d", &choice);
total = price*quantity;
if(choice == 1){
menu();
break;
}
else if (choice == 2){
grandtotal = grandtotal + total;
printf("\n Total Price is: %0.2f", grandtotal);
break;
}
case 'P':
printf("You selected French Pizza!\n");
printf("Enter quantity:");
scanf("%d", &quantity);
price = 105.00;
printf("\n Do you want to order more? [1] Yes [2] No:");
scanf("%d", &choice);
total = price*quantity;
if(choice == 1){
menu();
break;
}
else if (choice == 2){
grandtotal = grandtotal + total;
printf("\n Total Price is: %0.2f", grandtotal);
break;
}
case 'S':
printf("You selected Sandwiches\n");
printf("Enter quantity:");
scanf("%d", &quantity);
price = 75.50;
printf("\n Do you want to order more? [1] Yes [2] No:");
scanf("%d", &choice);
total = price*quantity;
if(choice == 1){
main();
break;
}
else if (choice == 2){
grandtotal = grandtotal + total;
printf("\n Total Price is: %0.2f", grandtotal);
break;
}
default:
printf("INVALID FOOD!");
break;
}
}
I wish someone could help or guide me. Thanks in advance.
In you code you have duplicated multiple times:
...
if(choice == 1){
menu();
break;
} ...
...
So when you choose choice = 1 then menu() get's displayed, and then the code breaks out of foods(). I think you meant to do the foods section again:
...
if(choice == 1){
menu();
foods();
break;
} ...
...
Yet another problem in your code is the %c scanf modifier. It will not eat up leading whitespaces, so it will read a newline (inputted on the last scanf). Use a leading space " %c" to tell scanf to read up leading whitespaces and ignore the leading newline, in scanf(" %c", &food);
Indent your code.
Don't duplicate statements. The whole scanf(... &choice); if (choice == 1) ... else if (choice == 2) could be placed outside the while switch not beeing duplicated 4 times.
Nesting functions using recursive calls can make your stack run out. Better just use a while loop.
Try not to use global variables. They are misleading and lead to maintainable code.
A slightly modified version of you code with a bit of indententation, added a do ... while loop and removed global variables and code duplication, may look like this:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
char menu(void);
float foods(char food);
void main()
{
clrscr();
float grandtotal = 0;
int choice = 0;
do {
// print menu and choose the food
char food = menu();
// choose food quantity and get it's price
float total = foods(food);
// print the total price
grandtotal = grandtotal + total;
printf("\n Total Price is: %0.2f", grandtotal);
// do you want to continue?
printf("\n Do you want to order more? [1] Yes [2] No:");
if (scanf("%d", &choice) != 1) {
perror("scanf error");
abort();
}
// continue until choice is equal to 1
} while (choice != 1);
}
char menu(void)
{
char food;
printf("Please select food from the following:\n");
printf(" B = Burger, F = French Fries, P = Pizza, S = Sandwiches \n");
printf("Enter food:");
if (scanf(" %c", &food) != 1) {
perror("scanf error");
abort();
}
return food;
}
float foods(char food){
float price = 0;
switch (food) {
case 'B':
printf("You selected Burger!\n");
price = 95.50;
break;
case 'F':
printf("You selected French Fries!\n");
price = 47.75;
break;
case 'P':
printf("You selected French Pizza!\n");
price = 105.00;
break;
case 'S':
printf("You selected Sandwiches\n");
price = 75.50;
break;
default:
fprintf(stderr, "INVALID FOOD!\n");
abort();
}
printf("Enter quantity:");
int quantity;
if (scanf("%d", &quantity) != 1) {
perror("scanf error");
abort();
}
return (float)price * (float)quantity;
}
When you call menu after user input is [1] yes. with menu() function show menu and after menu you should show call food() function.
HERE WHAT YOU WANT
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void menu();
void foods();
void main();
char food;
int quantity;
float price;
float total;
float grandtotal;
int choice;
void main()
{
clrscr();
do {
menu();
foods();
printf("\n Do you want to order more? [1] Yes [2] No:");
scanf("%d", &choice);
getchar(); // <== remove newline
grandtotal = grandtotal + total;
} while (choice == 1);
printf("\n Total Price is: %0.2f", grandtotal);
getch();
}
void menu() {
food = ' ';
quantity = 0;
price = 0;
total = 0;
choice = 0;
printf("Please select food from the following:\n");
printf(" B = Burger, F = French Fries, P = Pizza, S = Sandwiches \n");
printf("Enter food:");
scanf("%c", &food);
}
void foods() {
switch (food)
{
case 'B':
printf("You selected Burger!\n");
printf("Enter quantity:");
scanf("%d", &quantity);
price = 95.50;
//printf("\n Do you want to order more? [1] Yes [2] No:");
//scanf("%d", &choice);
//getchar(); // <== remove newline
total = price*quantity;
break;
//if (choice == 1) {
// menu();
// break;
//}
//else if (choice == 2) {
// grandtotal = grandtotal + total;
// printf("\n Total Price is: %0.2f", grandtotal);
// break;
//}
case 'F':
printf("You selected French Fries!\n");
printf("Enter quantity:");
scanf("%d", &quantity);
price = 47.75;
//printf("\n Do you want to order more? [1] Yes [2] No:");
//scanf("%d", &choice);
total = price*quantity;
break;
//if (choice == 1) {
// menu();
// break;
//}
//else if (choice == 2) {
// grandtotal = grandtotal + total;
// printf("\n Total Price is: %0.2f", grandtotal);
// break;
//}
case 'P':
printf("You selected French Pizza!\n");
printf("Enter quantity:");
scanf("%d", &quantity);
price = 105.00;
//printf("\n Do you want to order more? [1] Yes [2] No:");
//scanf("%d", &choice);
total = price*quantity;
break;
//if (choice == 1) {
// menu();
// break;
//}
//else if (choice == 2) {
// grandtotal = grandtotal + total;
// printf("\n Total Price is: %0.2f", grandtotal);
// break;
//}
case 'S':
printf("You selected Sandwiches\n");
printf("Enter quantity:");
scanf("%d", &quantity);
price = 75.50;
//printf("\n Do you want to order more? [1] Yes [2] No:");
//scanf("%d", &choice);
total = price*quantity;
break;
//if (choice == 1) {
// main();
// break;
//}
//else if (choice == 2) {
// grandtotal = grandtotal + total;
// printf("\n Total Price is: %0.2f", grandtotal);
// break;
//}
default:
printf("INVALID FOOD!");
break;
}
}

Compiler error which I am unable to locate

I'm getting an error which I am not able to resolve. I've gone through my code thoroughly with no success. What am I doing wrong? See code below.
Compiler error:
In function 'main':
ou1.c:49:1: error: expected 'while' before 'printf'
printf("End of program!\n");
^
My code:
#include <stdio.h>
int main(void){
int choice;
float price, sum, SUMusd;
float rate =1;
printf("Your shopping assistant");
do{
printf("1. Set exchange rate in usd (currency rate:%f)\n", rate);
printf("2. Read prices in the foreign currency\n");
printf("3. End\n");
printf("\n");
scanf("%d", &choice);
switch(choice){
case 1:
printf("Give exchange rate: \n");
scanf("%f", &rate);
break;
case 2:
do{
printf("Give price(finsh with < 0)\n");
scanf("%f", &price);
sum =+ price;
}while(price <= 0);
SUMusd = sum*rate;
printf("Sum in foreign currency: %f", sum);
printf("Sum in USD:%f", SUMusd);
break;
default:
printf("Invalid choice\n");
break;
}while(choice != 3);
}
printf("End of program!\n");
return 0;
}
The curly braces of the switch statement need to be closed before the while loop termination.
printf("Invalid choice\n");
break;
}
}while(choice != 3);
printf("End of program!\n");
Corrected full code sample
#include <stdio.h>
int main(void){
int choice;
float price, sum, SUMusd;
float rate =1;
printf("Your shopping assistant");
do{
printf("1. Set exchange rate in usd (currency rate:%f)\n", rate);
printf("2. Read prices in the foreign currency\n");
printf("3. End\n");
printf("\n");
scanf("%d", &choice);
switch(choice){
case 1:
printf("Give exchange rate: \n");
scanf("%f", &rate);
break;
case 2:
do{
printf("Give price(finsh with < 0)\n");
scanf("%f", &price);
sum =+ price;
}while(price <= 0);
SUMusd = sum*rate;
printf("Sum in foreign currency: %f", sum);
printf("Sum in USD:%f", SUMusd);
break;
default:
printf("Invalid choice\n");
break;
}
}while(choice != 3);
printf("End of program!\n");
return 0;
}

Vending Machine - Logic Error

// My issue is a rather specific one, the code compiles "Insert Coins" "Coin
//not accepted " indefinitely and doesn't allow input at all. I've tried this
//program with "While" only loops and "do" loops and it always compiles
//indefinitely without allowing input.I'm trying to figure out where my logic
//error is and possibly a simpler solution if possible. Thanks.
#include <stdio.h>
int main(void){
int money, drink_selection, coins;
money = 0;
do{ //ISSUE HERE??
do{ //OR HERE??
printf("Insert Coins: "); //REPEATS THIS
scanf("%d" ,&coins); //DOESNT ALLOW THIS
if(coins == 0 || coins == 5 || coins == 10 || coins == 25)
{
money +=coins;
}
else {
printf("Coin not accepted \n");//REPEATS INDEFINITELY
}
}while(coins != 0); // looping here?
printf("Please select from the following menu: 1 - Coffee or 2 - Tea ) \n");
printf("Enter your choice: \n");
scanf("%d", &drink_selection);
switch(drink_selection){
case 1:
printf("You have selected coffee as your choice. \n");
money-=25;
if (money >=0){
printf("Please take your coffee. \n");
}
break;
case 2:
money-=15;
if (money >= 0){
printf("Tea dispensing \n");
}
break;
default:
printf("Ivalid input");
break;
}
if (money > 0){
printf("Your change is %d cents ", &coins);
}
else if (money < 0){
printf("Insufficient amount, your change is: %d", &coins);
}
}while(money == 0); //POSSIBLY ISSUE IS HERE?
return 0;
}

If statement with an array. How to see if array integer is 0?

So my program is set to fulfill donation requests and I am trying to make an if statement that says if request is greater than the donation and donation is 0 then print that the request can't be fulfilled. So how would I check if the integer in the array is 0? Case 3 in the program is where my question is. Thanks
#include <stdio.h>
int main() {
int choice, i, number, type;
int temp_donations[5] = {0};
int donations[5] = {0};
int request[5] = {0};
char TYPES[5][20] = {"Protein", "Dairy", "Grains", "Vegetables", "Fruits"};
printf("Welcome to the Food Bank Management Program!\n");
//Give and ask for the user's choice
printf("What would you like to do?\n");
printf("\t1. Enter a Donation\n");
printf("\t2. Enter a Request\n");
printf("\t3. Fulfill Request\n");
printf("\t4. Print status report\n");
printf("\t5. Exit\n");
scanf("%d", &choice);
printf("\n");
//Print if choice is greater than 5 or less than 1
if(choice > 5 || choice < 1)
printf("Sorry, that was not a valid input.\n\n");
while (choice != 5) {
switch (choice) {
case 1:
//ask user the type of food they would like to donate
printf("\nWhat donation type would you like to enter?\n");
number = 0;
for(i=0; i<5; i++){
printf("\t%d. %s\n",number, TYPES[i]);
number += 1;
}
//user input for food type and amount to donate
scanf("%d", &type);
printf("How much would you like to donate? ");
scanf("%d", &donations[type]);
printf("Donation Added!\n\n");
break;
case 2:
//ask user the type of food they would like to request
printf("\nWhat would you like to request?\n");
number = 0;
for(i=0; i<5; i++){
printf("\t%d. %s\n",number, TYPES[i]);
number += 1;
}
//user input for request and amount requested
scanf("%d", &type);
printf("How much would you like to donate? ");
scanf("%d", &request[type]);
printf("Donation Added!\n\n");
break;
case 3:
//go through foods and fulfill the requests if possible
for(i=0; i<5; i++){
if (request[i] > donations[i] && //I'm not sure what to put in here)
printf("%s requests cannot be fulfilled.\n", TYPES[i]);
else if (request[i] > donations[i]){
printf("%s requests will be partially fulfilled.\n", TYPES[i]);
temp_donations[i] = donations[i];
donations[i] -= donations[i];
request[i] -= temp_donations[i];
}
else {
donations[i] -= request[i];
request[i] -= request[i];
}
}
printf("\n");
break;
case 4:
//print table of current donations and requests
for(i=0; i<5; i++){
printf("\t%-10s: Donations: %-2d Requests: %-2d\n", TYPES[i], donations[i], request[i]);
}
printf("\n");
break;
}
//reask for user's choice
printf("What would you like to do?\n");
printf("\t1. Enter a Donation\n");
printf("\t2. Enter a Request\n");
printf("\t3. Fulfill Request\n");
printf("\t4. Print status report\n");
printf("\t5. Exit\n");
scanf("%d", &choice);
printf("\n");
if(choice > 5 || choice < 1)
printf("Sorry, that was not a valid input.\n\n");
}
printf("Thank you for running our system!\n");
return 0;
}
Do you want this?
if (request[i] > donations[i] && donations[i] == 0)

Resources