Click here to Skip to main content
15,885,984 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
//
#include<stdio.h>

#include<stdlib.h>

#include <string.h>

#include <conio.h>

//Declaration Of Constants
int notents;
int nochairs;
int nostables;
int noltables;
//Struct
struct order {
    int numTents;
    int numChairs;
    int numSTables;
    int numLTables;
    int Orderdays;
    int Ordermonths;
    double Totaldue;
    };
//Integer variables used in program
int numorder, choice, x = 0;

//Declaration of Functions
void menu();
void logo();
void order();
void exit();

struct order All[15];

int main(){
	logo();
    menu();
    return 0;
}


void logo(){
	printf("Wendell`s T.T.C\n");
    printf("***********************\n");
}

void menu(){
    printf("What would you like to order?\n");
    printf("1. place an Order\n");
    printf("2. Exit\n\n");
    scanf("%d", & choice);
    switch (choice) {
    case 1:
        order();
        break;

    case 2:
        printf("Exiting");
        //ExitFunction();
        break;

    default:
        printf("Invalid choice, Please try again\n ");
        menu();
        break;

    }
}

void order() {
	FILE * pt = fopen("NoItems.txt", "a+");
    printf("How many orders would you like\n");
    scanf("%d", &numorder);
    int i;
    for (int i=1; i<=numorder; i++) {
    	printf("%d", i);
        printf("Number of Tents\n");
        scanf("%d",&All[i].numTents);
        printf("Number of chairs\n");
        scanf("%d", &All[i].numChairs);
        printf("Number of smalltables\n");
        scanf("%d", &All[i].numSTables);
        printf("Number of largetables\n");
        scanf("%d", &All[i].numLTables);
        printf("Tents $100\n");
        printf("Chairs $15\n");
        printf("SmallTables $25\n");
        printf("LargeTables $50\n\n");
        
        printf("Total Due \n");
        All[x].Totaldue = (All[i].numTents * 100) + (All[i].numChairs * 15) + (All[i].numSTables * 25) + (All[i].numLTables * 50);
        All[x].numTents = 100 * notents;
        All[x].numChairs = 15 * nochairs;
        All[x].numSTables = 25 * nostables;
        All[x].numLTables = 50 * noltables;
        printf("%f TotalDue");
        fprintf(pt, "\n%s %d %d", All[i].numTents, All[i].numChairs, All[i].numSTables, All[i].numLTables, All[i].Orderdays, All[i].Ordermonths, All[i].Totaldue);
    
	//Closing  for loop
	}
    fclose(pt);
    menu();
}


void exit(){
	
};


What I have tried:

I tried to create a formula for it which is line 98 but I don't know
Posted
Updated 29-Apr-21 20:35pm
Comments
jeron1 29-Apr-21 20:59pm    
You use 'x' as an index(All[x]), I can't see where you've assigned it a value other than zero. Can you step through the code with a debugger?

Jeron1 gave you the answer to your problem. This line :
C++
All[x].Totaldue = (All[i].numTents * 100) + (All[i].numChairs * 15) + (All[i].numSTables * 25) + (All[i].numLTables * 50);
also looks suspect. In fact, all references to All[x] are suspect. If you want to have the totals of each item in an order then you can make an instance of an order structure and call it Totals or something like that and sum the quantities of all the order items into it.

That brings me to the bigger problem : x is a global variable. It is bad enough to have a global variable but to give it a single-letter name makes it even worse. I don't see a need for any of those global variables. The variable choice can be local to the menu function and returned from it if needed elsewhere.

-edit-

You have order All[ 15 ] so add the variable order Total. You can sum the totals like this :
C++
for( i = 0; i < numorder; ++i )
{
   Total.numTents += All[ n ].numTents;
   Total.numChairs += All[ n ].numChairs;
   // sum the rest of the values the same way
}
You should also make a function to calculate the price of one order, similar to the line of code I noted above. It could like this :
C++
void SetTotalDue( order * porder )
{
    porder->Totaldue = ( porder->numTents * 100 );
    porder->Totaldue += ( porder->numChairs * 15 );
    porder->Totaldue += ( porder->numSTables * 25 );
    porder->Totaldue += ( porder->numLTables * 50 );
}
With these, you can call the function for every order and calculate its TotalDue. You can also loop through each order and determine the total number of each item ordered as shown above. You can also call the SetTotalDue function to get the value of the totals structure like this :
C++
SetTotalDue( & Total );
 
Share this answer
 
v2
Comments
Rick York 30-Apr-21 4:16am    
This has become messed up so please see the revised solution.
Best is to start learning using the debugger. Visit this debugger tutorial or watch some youtube videos.

some tips:

1. Using global variables is a common cause for nasty bugs.
2. Giving the variables and functions long and good names is an art which avoides a lot of headaches.

Both tips I have learned and proved in 20+ years of coding experience.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900