C++ Structures

3. Structures

Structures

#include

using namespace std;

struct part //declaring the structure; “part” is the structure name (or tag) a structure is a way to make user-defined variable type; class for data & functions

{

int modelnum; //those are

int partnum; //structure

float cost; //members

}; //don’t forget the semicolon here

int main()

{

part part1;

part1.modelnum=6244; //accessing structure members using the dot operator

part1.partnum=373; //in fact its name is member access operator

part1.cost=217.55F;

cout<<"Model "<<part1.modelnum<<", part "<<part1.partnum<<" costs $"<<part1.cost<<endl;

return 0;

}

Model 6244, part 373 costs $217.55

Process returned 0 (0x0) execution time : 0.301 s

Press any key to continue.

#include

using namespace std;

struct //note no name is given in declaration

{

int modelnum;

int partnum;

float cost;

}part1; //a name can be given here

int main()

{

part1.modelnum=6244;

part1.partnum=373;

part1.cost=217.55F;

cout<<"Model "<<part1.modelnum<<", part "<<part1.partnum<<" costs $"<<part1.cost<<endl; //same output

return 0;

}

#include

using namespace std;

struct part

{

int modelnum;

int partnum;

float cost;

};

int main()

{

part part1 = {6244, 373, 217.55F}; //initializing a structure variable

part part2;

cout<<"Model "<<part1.modelnum<<", part "<<part1.partnum<<" costs $"<<part1.cost<<endl;

part2=part1;

cout<<"Model "<<part2.modelnum<<", part "<<part2.partnum<<" costs $"<<part2.cost<12)

{

d3.in-=12;

d3.ft++;

}

d3.ft+=d1.ft+d2.ft;

cout<<d1.ft<<"\'-"<<d1.in<<"\" + "<<d2.ft<<"\'-"<<d2.in<<"\" = "<<d3.ft<<"\'-"<<d3.in<<"\"" <<endl;

return 0;

}

Enter feet

18

Enter inches

16

18'-16" + 11'-6.25" = 30'-10.25"

Process returned 0 (0x0) execution time : 8.922 s

Press any key to continue.

#include

using namespace std;

struct dist

{

int ft;

float in;

};

struct room //nested structures

{

dist length;

dist width;

};

int main()

{

room dinning;

dinning.length.ft=13; //equivalent to room dinning = {{13,6.5},{10,0}}

dinning.length.in=6.5;

dinning.width.ft=10;

dinning.width.in=0.0;

float l=dinning.length.ft+dinning.length.in/12; //note the type conversion

float w=dinning.width.ft+dinning.width.in/12;

cout<<"Dinning room area = "<<l*w<<" sq ft"<<endl; //type conversion happens here too

return 0;

}

Dinning room area = 135.417 sq ft

Process returned 0 (0x0) execution time : 0.290 s

Press any key to continue.

#include

using namespace std;

const int clubs=0;

const int diamonds=1;

const int hearts=2;

const int spades=3;

const int jack=11;

const int queen=12;

const int king=13;

const int ace=14;

struct card

{

int number;

int suit;

};

int main()

{

card temp,chosen,prize;

int pos;

card card1={7,clubs};

cout<<"Card 1 is the 7 of clubs\n";

card card2={jack, hearts};

cout<<"Card 2 is the jack of hearts\n";

card card3={ace, spades};

cout<<"Card 3 is the ace of spades\n";

prize=card3;

cout<<"I'm swapping card 1 and card 3\n";

temp=card3; card3=card1; card1=temp;

cout<<"I'm swapping card 2 and card 3\n";

temp=card3; card3=card2; card2=temp;

cout<<"I'm swapping card 1 and card 2\n";

temp=card2; card2=card1; card1=temp;

coutpos;

switch(pos)

{

case 1: chosen=card1; break;

case 2: chosen=card2; break;

case 3: chosen=card3; break;

}

if(chosen.number==prize.number && chosen.suit==prize.suit)

cout<<"\nCorrect! You win!\n";

else

cout<<"\nSorry! You lose!\n";

return 0;

}

Card 1 is the 7 of clubs

Card 2 is the jack of hearts

Card 3 is the ace of spades

I'm swapping card 1 and card 3

I'm swapping card 2 and card 3

I'm swapping card 1 and card 2

Where is the ace of spades? 2

Correct! You win!

Process returned 0 (0x0) execution time : 1.717 s

Press any key to continue.

Card 1 is the 7 of clubs

Card 2 is the jack of hearts

Card 3 is the ace of spades

I'm swapping card 1 and card 3

I'm swapping card 2 and card 3

I'm swapping card 1 and card 2

Where is the ace of spades? 1

Sorry! You lose!

Process returned 0 (0x0) execution time : 1.503 s

Press any key to continue.

Enumerations

#include

using namespace std;

enum wkdys {Sun, Mon, Tues, Wed, Thurs, Fri, Sat}; //declaring an enumeration:: Sun, Mon, … Sat are ‘enumerators’

int main()

{

wkdys day1,day2; //defining a variable of type wkdys; note that it has a certain range of values to take, not like integer

day1=Mon;

day2=Fri;

int diff=day2-day1; //note that enumerators actually takes numeric values starting from 0

cout<<"Days between = "<<diff<<endl;

if(day1<day2)

cout<<"Day 1 comes first"<<endl;

return 0;

}

Days between = 4

Day 1 comes first

Process returned 0 (0x0) execution time : 0.357 s

Press any key to continue.

#include

#include

using namespace std;

enum isword {Y, N};

int main()

{

isword chkw = N;

char chr='a';

int cnt=0;

cout<<"Enter a phrase"<<endl;

do

{

chr=getche();

if (chr==' ' || chr=='\r')

{

if (chkw==Y) //check if chkw is YES to check if this space has a space before it (N) or character(Y)

{

cnt++;

chkw=N; //in case a second successive space is countered, it won’t get here and increment the counter

}

}

else //if it’s a character

if (chkw==N)

chkw=Y; //then reset chkw to Y

}while(chr!='\r');

cout<<"\nword counter: "<<cnt<<endl;

return 0;

}

Enter a phrase

word1 2space word3 many spaces in between let's see

word counter: 9

Process returned 0 (0x0) execution time : 32.170 s

Press any key to continue.

#include

using namespace std;

const int jack=11, queen=12, king=13, ace=14;

enum suit {clubs=1, diamonds, hearts, spades}; //recall how we did it before; we defined integer variables, note that we can customize the first variable value to start from a certain number not 0

struct card

{

int num;

suit suitt;

};

int main()

{

card temp,chosen,prize;

int pos;

card card1={7,clubs};

cout<<"Card 1 is the 7 of clubs\n";

card card2={jack, hearts};

cout<<"Card 2 is the jack of hearts\n";

card card3={ace, spades};

cout<<"Card 3 is the ace of spades\n";

prize=card3;

cout<<"I'm swapping card 1 and card 3\n";

temp=card3; card3=card1; card1=temp;

cout<<"I'm swapping card 2 and card 3\n";

temp=card3; card3=card2; card2=temp;

cout<<"I'm swapping card 1 and card 2\n";

temp=card2; card2=card1; card1=temp;

coutpos;

switch(pos)

{

case 1: chosen=card1; break;

case 2: chosen=card2; break;

case 3: chosen=card3; break;

}

if(chosen.num==prize.num && chosen.suitt==prize.suitt)

cout<<"\nCorrect! You win!\n";

else

cout<<"\nSorry! You lose!\n";

return 0;

}

Card 1 is the 7 of clubs

Card 2 is the jack of hearts

Card 3 is the ace of spades

I'm swapping card 1 and card 3

I'm swapping card 2 and card 3

I'm swapping card 1 and card 2

Where is the ace of spades? 2

Correct! You win!

Process returned 0 (0x0) execution time : 5.987 s

Press any key to continue.