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;
}
}
Related
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-----------------------------------");
}
I am new to C programming language. I want the following code to repeat the main menu after the "Transaction successful" and also store a new balance after an amount is entered.How do i go about it. Here is the code
#include<stdio.h>
int main()
{
int mainmenu, amt, balance = 0;
printf("1.Pay \n");
printf("2.Balance \n");
printf("3.Transaction history \n");
scanf("%d",&mainmenu);
if(mainmenu == 1){
printf("Select Amount \n");
printf("$0.50 \n");
printf("$1.00 \n");
printf("$1.50 \n");
printf("$2.00 \n");
scanf("%d",&amt);
if(amt == 1){
balance = balance + 0.5;
printf("Transaction Successful \n");
}
if(amt == 2){
balance = balance + 1;
printf("Transaction Successful \n");
}
if(amt == 3){
balance = balance + 1.5;
printf("Transaction Successful \n");
}
if(amt == 4){
balance = balance + 2;
printf("Transaction Successful \n");
}
}
if(mainmenu == 2){
printf("\n");
printf("Balance = %d",balance,"\n");
}
if(mainmenu == 3){
printf("No Transaction History At The Moment");
}
}
Simplest way is probably just to use a while loop in this case.
while(1){
//mainmenu here
printf("4. Exit Program");
scanf("%d", &mainmenu);
//if statements/functions here
}
This would require you to add in a fourth option in order to exit out of the while loop, so it doesn't run endlessly (you could also use other methods to know when to exit). A good aspect of this is if they accidentally put in something invalid such as 5 or 7, it doesn't cause any bugs but rather just runs the loop again.
Keep in mind that you'd have to put all your if statements within the while loop, or put them in a different function and call that function after you get the user's input.
P.S. Unrelated but switch statements would probably be a bit easier than if else statements here. They're not necessary but could make your life a little easier.
There are two different methods you can use. A while loop, or a for loop.
The syntax for both are like so:
while(condition) {
statement(s); // execute this section of code until the condition is met
}
and
for (initilization; condition; increment){
statement(s); // execute this section of code until the condition is met
}
if you want an infinite loop, you can use TRUE as the condition in the while loop like:
while(TRUE) {
statement(s); // execute this section of code indefinitely
}
or you can leave the parameters empty in the for loop like so:
for( ; ; ){
statement(s); // execute this section of code indefinitely
}
in your case, it would look like:
#include<stdio.h>
int main()
{
int mainmenu, amt, balance = 0;
while(TRUE){
printf("1.Pay \n");
printf("2.Balance \n");
printf("3.Transaction history \n");
scanf("%d",&mainmenu);
if(mainmenu == 1){
printf("Select Amount \n");
printf("$0.50 \n");
printf("$1.00 \n");
printf("$1.50 \n");
printf("$2.00 \n");
scanf("%d",&amt);
if(amt == 1){
balance = balance + 0.5;
printf("Transaction Successful \n");
}
if(amt == 2){
balance = balance + 1;
printf("Transaction Successful \n");
}
if(amt == 3){
balance = balance + 1.5;
printf("Transaction Successful \n");
}
if(amt == 4){
balance = balance + 2;
printf("Transaction Successful \n");
}
}
if(mainmenu == 2){
printf("\n");
printf("Balance = %d",balance,"\n");
}
if(mainmenu == 3){
printf("No Transaction History At The Moment");
}
}
}
or:
#include<stdio.h>
int main()
{
int mainmenu, amt, balance = 0;
for( ; ; ){
printf("1.Pay \n");
printf("2.Balance \n");
printf("3.Transaction history \n");
scanf("%d",&mainmenu);
if(mainmenu == 1){
printf("Select Amount \n");
printf("$0.50 \n");
printf("$1.00 \n");
printf("$1.50 \n");
printf("$2.00 \n");
scanf("%d",&amt);
if(amt == 1){
balance = balance + 0.5;
printf("Transaction Successful \n");
}
if(amt == 2){
balance = balance + 1;
printf("Transaction Successful \n");
}
if(amt == 3){
balance = balance + 1.5;
printf("Transaction Successful \n");
}
if(amt == 4){
balance = balance + 2;
printf("Transaction Successful \n");
}
}
if(mainmenu == 2){
printf("\n");
printf("Balance = %d",balance,"\n");
}
if(mainmenu == 3){
printf("No Transaction History At The Moment");
}
}
}
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)
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
int a = 1;
printf("Enter the number of items from 1 and 10: \n");
while (a <= 10)
{
scanf("%d", &a);
if (a >= 1 && a <= 10)
{
printf("Thank You!\n");
break;
}
else
{
printf("Wrong input! Try Again.\n");
continue;
}
}
To be more detailed about what I'm asking lets say that the user enters 3 (for 3 items) how would I use the for loop to retrieve that information so I can further finish the code.
You should keep in mind following points:
Get the no.of choice before starting loop
Check the condition in loop with no. of choices.
Only one loop is enough for your task.
I think you need this:
int a = 1;
bool bFlag = true;
int price[10];
printf("Enter the number of items from 1 and 10: \n");
while(bFlag){
scanf("%d", &a);
if (a >= 1 && a <= 10)
{
printf("Thank You!\n");
break;
}
else
{
printf("Wrong input! Try Again.\n");
continue;
}
}
for (int i = 0; i< a; i++)
{
printf("Enter price for item %d = ", i);
scanf("%d",&price[i]);
}