473,326 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Structures

I need to build a structured program that has to deal with pizza size, pounds of cheese, and price of the pizza in C++. I haven't got far because instead of the program actually having the defined variables the user has to put their own variables in.

struct pizza { //declare a structure
int size; //size of the pizza in inches
int cheeseAmount; //amount of cheese in pounds
float price; //price of the pizza
};



int main()
{
pizza pizza1; //define a structure variable
//give values to structre members
pizza1.size =
pizza1.cheeseAmount =
pizza1.price =


//display structure members
cout << " Enter the size of the pizza in inches: " << pizza1.size;

cout << " Enter the pounds of cheese to be added: " << pizza1.cheeseAmount;

cout << " Enter the price of the pizza: " << pizza1.price;

cout << " \n A " << pizza1.size << " inch pizza with " << pizza1.cheeseAmount << " pounds of cheese will cost\n " << pizza1.price ;


Is what I have so far I just need to know what I need to do to get it to work the the correct structure.
Oct 5 '09 #1
3 1171
weaknessforcats
9,208 Expert Mod 8TB
Your use of the struct looks OK.

But assigning ot the struct members directly in main(), while it works, does violate the priciple of a structured program.

That is a structured program consists of a hierarchy of functions where the code of the lowest functions is obvious.

For your program to be structured, you would have to:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     Call a function to get the size of the pizza
  4.     Call a function to change the size of the pizza
  5.     Ditto for the other pizza members
  6.     etc...
  7.     Call a function to display the updated pizza
  8.  
  9. }
Typically, these functions would have a pointer to the pizza as one argument so that the correct pizza could be updated. There would be other arguments based on what is being done
Oct 5 '09 #2
I know but I am confused on how the function is set up. What do I put in all those fields?
Oct 5 '09 #3
weaknessforcats
9,208 Expert Mod 8TB
You don't need to worry about the fields in the struct.

You pass a pointer to a pizza variable:

Expand|Select|Wrap|Line Numbers
  1. void ChangeSize(pizza* ptr, int newsize)
  2. {
  3.      ptr->size = newsize
  4. }
The function should know what it's doing. This example fiddles with the size member and leaves all the other members alone. You write other functions that work with the other members and you guarantee that all the functions are called.

This way each function only has to worry about its job and not worry about the entire struct variable.
Oct 5 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: kazack | last post by:
Hi all it's me again with another question as I got further in my book. The chapter I am in covers structres, abstract data and classes. I only read through to the end of the coverage on...
33
by: Peter Seaman | last post by:
I understand that structures are value types and arrays and classes are reference types. But what about arrays as members of structures i.e. as in C struct x { int n; int a; }
6
by: Ken Allen | last post by:
OK, I admit that I have been programming since before C++ was invented, and I have developed more than my share of assembly language systems, and even contributed to operating system and compiler...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
14
by: pmclinn | last post by:
I've noticed that many programmers use classes to store data about such things like: Class Customers .....Phone ....ID ....Address End Class....
2
by: thomasfarrow | last post by:
At work, our development team has a development standards document that insists Structures should never be used. I'm looking to change this standard but need a suitable argument in order to make...
11
by: efrat | last post by:
Hello, I'm planning to use Python in order to teach a DSA (data structures and algorithms) course in an academic institute. If you could help out with the following questions, I'd sure...
44
by: svata | last post by:
Hello, I wonder how to resize such array of structures using realloc()? #include <stdio.h> #include <stdlib.h> #define FIRST 7 typedef struct { char *name;
4
by: cleanrabbit | last post by:
Hello! I hate having to do this, because im almost certain there is someone in the world that has come across this problem and i just havent found their solution yet, so i do appologise if this...
8
by: Bob Altman | last post by:
Hi all, I have a structure that includes a constructor. I want to add a bunch of these structures to an STL map (whose index is an int). If I define the map like this: map<int,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.