C Program for validation of Order - c

Can anybody help me with my code, I have a problem on my 3. Orderwhen validating my order if it exceeds the available stocks or Not
int main(void){
int choice = 0;
int year = 1, i, com;
int storage[99] = {0};
for (;;) {
clrscr();
printf("Year %d\n\n", year);
printf("1. Import\n");
printf("2. Storage\n");
printf("3. Order\n");
printf("\nchoice: ");
scanf("%d", &choice);
if (choice == 1){ // import
clrscr();
printf("Enter sets of computer's imported: ");
scanf("%d", &storage[year]);
year++;
}
if (choice == 2){ // storage
clrscr();
printf("Year sets\n");
for (i = 1; i < year; i++){
printf("%2d %4d\n", i, storage[i]);
}
getch();
}
if(choice == 3){ //order
printf("Enter Sets of Computer ordered: ");
scanf("%d", &com);
for (i = 0; com && i < 99; i++){
if (com <= storage[i]){
storage[i] = storage[i] - com;
com = 0;
}
if (com > storage[i]){
printf("Sorry we have No enough Stocks !");
}
if (storage[i] <= com){
com = com - storage[i];
storage[i] = 0;
}
}
}
}
}
for example:
I import 3 times in choice 1. Import
In Year 1 i import 500 sets of computers
In Year 2 i Import 1000 sets of computers
In Year 3 i Import 800 sets of computers
when viewing my 2. Storage it will show this
Year sets
1 500
2 1000
3 800
total of 2300 sets of computers in my 2. Storage
now in 3. Order i enter sets of computer ordered is 5000.
5000 is exceeds my stocks in my 2. Storage so i am expecting that the order will not continue and it will display Sorry we have No enough Stocks !
but when viewing 2. Storage again it shows wrong output
Year sets
1 0
2 0
3 0
as you can see its now empty

Related

How to jump back to the top of the code after if statement C language

I'm trying to make my program to start again from the beginning after user has selected an option from a menu.
When the user selects 1 and then enters the amount of the donation I want the program to start again and show the menu What would you like to do? and not to just restart just the if statement.
Should I add a for loop inside of the if statement to accomplish this? thanks for the help.
printf("What would you like to do?\n");
printf(" 1 - Donate\n");
printf(" 2 - Invest\n");
printf(" 3 - Print balance\n");
printf(" 4 - Exit\n");
printf("\n");
//scans menu choice
scanf("%d", &menu_option);
if(menu_option==1)
{
printf("How much do you want to donate?\n");
scanf("%lf", &donation);
donations_made++;
current_Balance = initial_Balance + donation;
}
When the user selects 1 and then enters the amount of the donation I want the program to start again and show the menu
just do
for(;;) {
printf(" 1 - Donate\n");
printf(" 2 - Invest\n");
printf(" 3 - Print balance\n");
printf(" 4 - Exit\n");
printf("\n");
//scans menu choice
scanf("%d", &menu_option);
if(menu_option==1)
{
printf("How much do you want to donate?\n");
scanf("%lf", &donation);
donations_made++;
current_Balance = initial_Balance + donation;
// NO BREAK
}
else {
.... management of other cases
break;
}
}
or if you prefer
do {
printf(" 1 - Donate\n");
printf(" 2 - Invest\n");
printf(" 3 - Print balance\n");
printf(" 4 - Exit\n");
printf("\n");
//scans menu choice
scanf("%d", &menu_option);
if(menu_option==1)
{
printf("How much do you want to donate?\n");
scanf("%lf", &donation);
donations_made++;
current_Balance = initial_Balance + donation;
}
// ... management of other cases
} while (menu_option==1);
But are you sure you do not want to redo also in cases 2 and 3 ? In that case replace while (menu_option==1); by while (menu_option != 4); or in the first proposal do the break only when menu_option is 4
I also encourage you to check the return value of scanf("%d", &menu_option); to be sure a valid integer was given in input and menu_option was set
#include <stdio.h>
int main()
{
int menu_option;
double donation;
int donations_made = 0;
int current_Balance = 0;
int initial_Balance = 0;
for (;;)
{
printf("What would you like to do?\n");
printf(" 1 - Donate\n");
printf(" 2 - Invest\n");
printf(" 3 - Print balance\n");
printf(" 4 - Exit\n");
printf("\n");
//scans menu choice
scanf("%d", &menu_option);
if (menu_option==1)
{
printf("How much do you want to donate?\n");
scanf("%lf", &donation);
donations_made++;
current_Balance = initial_Balance + donation;
}
else if (menu_option == 4)
break;
}
}

How To get program to repeat when asked y or n

i am trying to get this program to repeat when prompted Y or N and i cant seem to get it to work right for some reason and this is the last thing i have left and im pretty sure the rest of the code is right i think all i need is it to repeat the whole program if the user enters a "Y" or just exits if the user enters "N"
int main(void)
{
// Constant and Variable Declarations
const int MPH_SPEED_MIN = 1;
const int MPH_SPEED_MAX = 100;
const int HOURS_TRAVLED_MIN = 1;
int mphSpeed = 1;
int hoursEntered = 0;
int distanceTraveled = 0;
int counterNum = 0;
int distanceNum = 0;
char ch = 'y';
// *** Input ***
do {
printf("What is the speed of the vehicle in MPH? ");
scanf("%d", &mphSpeed);
while ((mphSpeed < MPH_SPEED_MIN) || (mphSpeed > MPH_SPEED_MAX)) {
printf("\tThe speed entered must be between %d and %d inclusive
\n",MPH_SPEED_MIN, MPH_SPEED_MAX);
printf("\tPlease re-enter the speed of the vehicle in MPH: ");
scanf("%d", &mphSpeed);
}
printf("How many hours has it traveled? ");
scanf("%d", &hoursEntered);
while (hoursEntered < HOURS_TRAVLED_MIN) {
printf("\tThe hours traveled must be a positive number.\n");
printf("\tPlease re-enter the number of hours traveled: ");
scanf("%d", &hoursEntered);
}
printf("\n");
printf("Hour\tDistance Traveled\n");
distanceTraveled = hoursEntered * mphSpeed;
for (counterNum = 1; counterNum <= hoursEntered; counterNum++) {
distanceNum = distanceTraveled * counterNum;
printf("%d\t%d miles\n", counterNum, distanceNum);
}
printf("\n");
printf("Run the program again (Y/N)? ");
scanf("%c", &ch);
printf("\n");
} while (ch == 'Y' || ch == 'y');
; return 0;
When reading in with scanf(%c..., the statement very likely reads in a new line character left in the buffer from previous inputs. Read in a string instead, because %s ignores any leading white spaces (including such a new line character left in the buffer).
Try ...
char exitYN[2];
if (scanf("%1s",exitYN) != 1) {
exitYN[0]='N';
}
char ch = exitYN[0];
} while (ch == 'Y' || ch == 'y');
The one small yet, the most effective change that can be made here is adding a <space> before the %c while accepting the Y or N, i.e, scanf(" %c, &ch");
And I don't know if the following are errors while typing the code in StackOverflow, or are they originally errors in your code, but definitely are worth making changes:
Header file missing: #include<stdio.h>,
Unwanted and extra semicolon (;) before the return statement at the end,
missing closing bracket (}) at the end, after the return.
Here is the working code:
#include<stdio.h>
int main(void)
{
// Constant and Variable Declarations
const int MPH_SPEED_MIN = 1;
const int MPH_SPEED_MAX = 100;
const int HOURS_TRAVLED_MIN = 1;
int mphSpeed = 1;
int hoursEntered = 0;
int distanceTraveled = 0;
int counterNum = 0;
int distanceNum = 0;
char ch = 'y';
// *** Input ***
do {
printf("What is the speed of the vehicle in MPH? ");
scanf("%d", &mphSpeed);
while ((mphSpeed < MPH_SPEED_MIN) || (mphSpeed > MPH_SPEED_MAX)) {
printf("\tThe speed entered must be between %d and %d inclusive\n",MPH_SPEED_MIN, MPH_SPEED_MAX);
printf("\tPlease re-enter the speed of the vehicle in MPH: ");
scanf("%d", &mphSpeed);
}
printf("How many hours has it traveled? ");
scanf("%d", &hoursEntered);
while (hoursEntered < HOURS_TRAVLED_MIN) {
printf("\tThe hours traveled must be a positive number.\n");
printf("\tPlease re-enter the number of hours traveled: ");
scanf("%d", &hoursEntered);
}
printf("\n");
printf("Hour\tDistance Traveled\n");
distanceTraveled = hoursEntered * mphSpeed;
for (counterNum = 1; counterNum <= hoursEntered; counterNum++) {
distanceNum = distanceTraveled * counterNum;
printf("%d\t%d miles\n", counterNum, distanceNum);
}
printf("\n");
printf("Run the program again (Y/N)? ");
scanf(" %c", &ch);
printf("\n");
} while (ch == 'Y' || ch == 'y');
return 0;
}
I have also attached the output just in case you need to verify.
OUTPUT:
What is the speed of the vehicle in MPH? 12
How many hours has it traveled? 1
Hour Distance Traveled
1 12 miles
Run the program again (Y/N)? y
What is the speed of the vehicle in MPH? 6
How many hours has it traveled? 6
Hour Distance Traveled
1 36 miles
2 72 miles
3 108 miles
4 144 miles
5 180 miles
6 216 miles
Run the program again (Y/N)? n

How to store the users input into an array?

I know from my code it's a bit of a mess as I'm a beginner to programming. I just wanted to see if someone could just quickly help me out by letting me know how to get the users input stored into an array and do what my code already does. So, I want the user to input the amount of tickets and then that gets stored in an array then printed like my code already does. Just want to try and cut down my code as it is very messy and need to store it in an array I've been told but I'm stumped at the moment.
#include <stdio.h>
#include<conio.h>
#include<stdbool.h>
#define MAX 10
void showPrices();
void showRoutes();
void orderTickets();
void main()
{
int userChoice;
while(1)
{
// Looping the menu for the user
printf("\n\n**** MENU ****");
printf("\n*NOTE* MAX TICKETS PER CUSTOMER = 10");
printf("\n1. Show Prices\n2. Show Routes\n3. Order Tickets\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &userChoice);
//gets to chose between the different options
switch(userChoice)
{
case 1: showPrices();
break;
case 2: showRoutes();
break;
case 3: orderTickets();
break;
case 4: exit(0);
default: printf("\nInvalid, try again!!!");
}
}
}
void showPrices()
{
//for the switch statement, this is what I want to be displayed when they pick this option
printf("\n\n---- Prices - ALL DAY ----");
printf("\n1. Child = £3\n2. Teenager = £5\n3. Standard = £10\n4. Student = £7\n5. Senior = £6");
}
void showRoutes()
{
//for the switch statement, this is what I want to be displayed when they pick this option
printf("\n\n---- Routes - ALL DAY ----");
printf("\n---- Leaving from Nottingham Station ----");
printf("\nDestinations:\nDerby\nLeicester\nSheffield\nBirmingham");
}
void orderTickets()
{
int ticketArray[tickets];
int age;
int userInput = 0;
int tickets;
int ticketSum = 0;
int ticketNumber = 1;
int sum;
int userDesti;
//Main function where the user oders the tickets!
printf("\nWhere do you want to go?");
printf("\n1. Derby\n2. Leicester\n3. Sheffield\n4. Birmingham");
scanf("%d", &userDesti);
//asks the user how many tickets they want
printf ("\nHow many tickets do you need?: ");
scanf ("%d", &tickets);
//loop to keep asking the user to input the information they need to enter for each ticket until it reaches 0
while (tickets > 0)
{
/*only does this if they enter 10 or less and more than 0
if (tickets <= 10 && tickets > 0)
{
printf("\nHow old are you?: ");
scanf("%d", &age);
if(age < 10)
{
printf("\nChild Ticket = £3\n");
ticketSum = ticketSum + 3;
}
else if (age < 16)
{
printf ("\nTeenager Ticket = £5\n");
ticketSum = ticketSum + 5;
}
else if (age >= 16 && age < 60)
{
printf("\nStandard Ticket = £10\n");
ticketSum = ticketSum + 10;
}
else
{
printf("\nSenior Ticket = £6\n");
ticketSum = ticketSum + 6;
}
}
//if they enter an invalid number
else
{
printf("Error, invalid number of tickets!!\n");
break;
}*/
// this allows the student discount to be deducted from the final price
printf("\nAre you a student? Enter 1 for yes or 2 for no: ");
scanf("%i", &userInput);
if(userInput == 1)
{
printf ("\nYou are eligible for the student discount = £3 deducted\n");
//sum to deduct the discount
ticketSum = ticketSum - 3;
}
//if they aren't a student then they will get this message
else if (userInput == 2)
{
printf ("\nYou are not eligible for the student discount, sorry!\n");
}
// gives the user their number for that ticket
printf("\n------------------");
printf("\nTicket Number: %d ", ticketNumber);
printf("\n------------------\n");
ticketNumber++;
// lets the user know how much each ticket adds onto the previous
printf("\n-----------------------");
printf("\nThe summary so far: %d ", ticketSum);
printf("\n-----------------------\n");
// gets out of the main loop when the tickets are less than or equal to 0
if (tickets <= 0 )
{
break;
}
//takes away the value of tickets each time so if they want 2 tickets then it takes away 1 so then it equals 1 left umtil it reaches 0
tickets--;
}
//the receipt of all the tickets added together.
printf("\n-----------------------------------");
printf("\n-----------------------------------");
printf("\nThe receipt for your order: £%d", ticketSum);
printf("\n-----------------------------------");
printf("\n-----------------------------------");
}

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)

Check for validation if Input exceeds the available stocks

Can anybody help me fix the validation of my program
actual output of my program:
choice 1. Import I input 2 times, my 1st input is 100 2nd input is 200
so when viewing the choice 2. Storage output will be this
Year sets
1 100
2 200
total of 300 sets of computer in storage
now in choice 3. Sell Order I input 400 so it will display Sorry we have No enough Stocks !
my problem is when viewing my 3. Storage again all stocks are now empty
Year sets
1 0
2 0
I am expecting that when my Input exceeds the available stocks it will not continue reduced the stocks in my storage
int main(void) {
int choice = 0;
int year = 1, i, com;
int storage[99] = { 0 };
for (;;) {
clrscr();
printf("Year %d\n\n", year);
printf("1. Import\n");
printf("2. Storage\n");
printf("3. Sell Order\n");
printf("\nchoice: ");
scanf("%d", &choice);
if (choice == 1) { // import
clrscr();
printf("Enter sets of computer's imported: ");
scanf("%d", &storage[year]);
year++;
}
if (choice == 2) { // storage
clrscr();
printf("Year sets\n");
for (i = 1; i < year; i++) {
printf("%2d %4d\n", i, storage[i]);
}
getch();
}
if (choice == 3) { //order
printf("Enter Sets of Computer ordered: ");
scanf("%d", &com);
for (i = 0; com && i < 99; i++) {
if (com <= storage[i]) {
storage[i] = storage[i] - com;
com = 0;
} else {
com = com - storage[i];
storage[i] = 0;
}
}
if (com > storage[i]) { // validation
printf("Sorry we have No enough Stocks !");
getch();
}
}
}
}
You are wiping the stocks before you have calculated how much stock you have. You want to first add up how much stock you have, check to see if you have enough, THEN wipe the stocks IF you have enough
if (choice == 3) { //order
printf("Enter Sets of Computer ordered: ");
scanf("%d", &com);
for(i=0;i<99;i++) //calculates the amount of stock you have
{
Total_Stock+=storage[i];
}
for (i = 0; com && i < 99; i++) {
if(Total_Stock<com) //If not enough stock it breaks the loop
//before subtracting the stock
{
printf("Not enough stock.\n");
break;
}
if (com <= storage[i]) {
storage[i] = storage[i] - com;
com = 0;
} else {
com = com - storage[i];
storage[i] = 0;
}
}

Resources