Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

What are structures?

When programming, it is often convenient to have a single


name with which to refer to a group of related values.
Structures provide a way of storing many different
values in variables of potentially different types under
the same name. This makes it a more modular program,
which is easier to modify because its design makes things
more compact. Structs are generally useful whenever a lot
of data needs to be grouped together
How to define a structure.
⚫ struct NameOfStruct
{
Datatype variableName;
Datatype VariableName;
.
.
Any number of variables included in struct
};
Void main() //start of main function
declare local variable here
Declare local struct here
struct NameOfStruct localStructName1;
localStructName1.MainStructVariablename=0;
Sample Struct
⚫ struct cd Name of struct
{
char name[20];
char description[40]; Variables in
char category[12]; struct
float cost;
int number;
};
⚫ void main() Local struct
struct cd BBR; declared
BBR.name[20];
BBR.description[40];
BBR.category[12]; Variables in
BBR.cost=0; local struct
BBR.number=0;

Local struct name Variable from main struct


How to define a structure.
⚫ struct NameOfStruct
{
Datatype variableName;
Datatype VariableName;
.
.
Any number of variables included in struct
} localStructName1;
Void main() //start of main function
declare local variable here
Initialize local struct here
localStructName1.MainStructVariablename=0;
Sample Struct
⚫ struct cd Name of struct
{
char name[20];
char description[40]; Variables in
char category[12]; struct
float cost;
int number;
} BBR;
⚫ void main() Local struct
declared
BBR.name[20];
BBR.description[40];
BBR.category[12]; Variables in
BBR.cost=0; local struct
BBR.number=0;

Local struct name Variable from main struct


Compile Struct sample program
Build a new project called structsample.cpp and copy and
paste codes from structsample.doc document to this project.
⚫ Compile and test program using the test data below:
Please enter cd ID: 1000
Enter name of cd: Movie
Enter description of cd: Action
Enter cd category: R
Enter cost of cd: 125.50
Enter cd quantity : 3

Please enter cd ID: 999


Graded Exercise
Graded component below: 15 marks
Modify your codes to include another local struct name
BBG to capture data for cd id 1001. Modify output as well
to print data from this struct called BBG in the same
format as the BBR. The number 999 is used to terminate
data entry and is the sentinel id.
Test data for graded exercise
⚫ Use the following test data when done with the modifications:
Please enter cd ID: 1000
Enter name of cd: Movie
Enter description of cd: Comedy
Enter cd category: PG13
Enter cost of cd: 130.50
Enter cd quantity : 5

Please enter cd ID: 1001


Enter name of cd: AUDIO
Enter description of cd: GOSPEL
Enter cd category: SOLOIST
Enter cost of cd: 60.60
Enter cd quantity : 4

Please enter cd ID: 999


Quiz 7: Structures
⚫ If you haven't already done so, be sure to read through Cprogramming.com's tutorial
on Structures. Otherwise, best of luck with the quiz!
1. Which of the following accesses a variable in structure b?
A. b->var;
B. b.var;
C. b-var;
D. b>var;
2. Which of the following accesses a variable in a pointer to a structure, b?
A. b->var;
B. b.var;
C. b-var;
D. b>var;
3. Which of the following is a properly defined struct?
A. struct {int a;}
B. struct a_struct {int a;}
C. struct a_struct int a;
D. struct a_struct {int a;};

4. Which properly declares a variable of struct foo?


A. struct foo;
B. struct foo var;
C. foo;
D. int foo;
How to submit your work
Use print screen to capture program output and paste in
word document. Include your name and class and email to
dthscomsci@gmail.com.

You might also like