Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ghall

macrumors 68040
Original poster
Jun 27, 2006
3,771
1
Rhode Island
So I am continuing my C++ studies. A while ago, I wrote a Madlibs program as an assignment for my C++ tutors, and my new assignment is to modify the code to use structures and/or classes, and I'm stumped. Can somebody give me a few ideas. The code is about 150 lines long, so I've uploaded it as an attachment.

Feel free to compile and play around with it too if you so desire.
 

Attachments

  • main.cpp.zip
    2 KB · Views: 107

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
Well, decide if you want C code with Structures, or C++ code with Structures and/or Classes first.

Can you verbalize what you want to do?

Todd
(I know what to do - but you need to show you know.)
 

ghall

macrumors 68040
Original poster
Jun 27, 2006
3,771
1
Rhode Island
Well, decide if you want C code with Structures, or C++ code with Structures and/or Classes first.

Can you verbalize what you want to do?

Todd
(I know what to do - but you need to show you know.)

I guess I need to use structures. I'm trying to create a structure for blank spaces and an structure for parts of speech. I'm confused at the order I should do them, and how to implement them in the main code.

I'm not even really sure what I'm doing. I'm having a tough time grasping classes and structures.
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
OK, so a structure of group of related data, made up of individual data types and/or other structures (basically). Think of a structure as a programmer defined data type.

What variables do you have now that you want to group together into a structure?
Todd
 

ghall

macrumors 68040
Original poster
Jun 27, 2006
3,771
1
Rhode Island
OK, so a structure of group of related data, made up of individual data types and/or other structures (basically). Think of a structure as a programmer defined data type.

What variables do you have now that you want to group together into a structure?
Todd

Right now, I just want the part of speech to be a variable (noun, verb, etc.).
 

ghall

macrumors 68040
Original poster
Jun 27, 2006
3,771
1
Rhode Island
OK. Grab your C book and let's see your first attempt at defining a structure.

Todd

I don't really have a book, I'm just being taught off a white board. Anyway here's my first program using structures. I got some help with it, but I can't seem to grasp the concept of structures. It's so frustrating, I was doing so well until I reached this point in my lessons. :(

Code:
#include <stdio.h>

//this structure represents walls in a room
struct wall{
	int height;
	int length;
};
//this structure represents a room
struct room{
	wall north,south,east,west,updn;
};

//this function calculates the area of a wall
int area (wall mw){
	return mw.height*mw.length;
}

//this function calculates the area of the floor and ceiling
int updown (room mc){
	return mc.north.length*mc.east.length;
}

//this function calculates the total area of a room
int roomarea (room mr){
	return area(mr.north)+area(mr.south)+area(mr.east)+area(mr.west)+updown(mr)+updown(mr);
}

//this function calculates the area of the walls and the ceiling of a room
int paintarea (room ma){
	return area(ma.north)+area(ma.south)+area(ma.east)+area(ma.west)+updown(ma);
}

//this function calculates the total area of the floor
int carpetarea (room mp){
	return updown(mp);
}


int main()
{
	room r;
	
	//take in the room height and assign it to the nort south west and east walls
	printf ("Height\n");
	scanf ("%d", &r.north.height);
	r.south.height = r.north.height;
	r.west.height = r.north.height;
	r.east.height = r.north.height;
	
	
	//take in the room length and assign it to the north and south walls
	printf ("Length\n");
	scanf ("%d", &r.north.length);
	r.south.length = r.north.length;
	
	//take in the room width and assign it to the east and west walls
	printf ("Width\n");
	scanf ("%d", &r.west.length);
	r.east.length = r.west.length;
	
	//assign the functions roomarea paintarea and carpetarea to integers
	int myarea = roomarea (r);
	int mypaintarea = paintarea (r);
	int mycarpetarea = carpetarea (r);
	
	//output the results
	printf ("%d foot area\n%d paint area\n%d carpet area\n", myarea, mypaintarea, mycarpetarea);
}
 

ChrisBrightwell

macrumors 68020
Apr 5, 2004
2,294
0
Huntsville, AL
Right now, I just want the part of speech to be a variable (noun, verb, etc.).

If you want to use a class or struct, then, it would look something like this:

Code:
struct BlankSpace
{
  private:
    String type; // adj., noun, adv., etc.
    String value; // value given by user.

  public:
    BlankSpace(); // default constructor
    BlankSpace(const BlankSpace&) // copy constructor
    ~BlankSpace(); // default destructor
    BlankSpace& operator= (const BlankSpace& bs);

    void setType(String s);
    void setValue(String s);
    String getType();
    String getValue();
}
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
Chris, I wasn't aware you could define constructors and destructors with structures.

BTW, he's sticking with C, so that C++ example doesn't help him. (him?)

Todd
 

iSee

macrumors 68040
Oct 25, 2004
3,540
272
Chris, I wasn't aware you could define constructors and destructors with structures.

BTW, he's sticking with C, so that C++ example doesn't help him. (him?)

Todd

Under C++, structs and classes are the same thing, the only difference being that members are public by default for structs, and private by default for classes.

(But as a style thing, I'd recommend only using structs in the traditional way.)
 

ChrisBrightwell

macrumors 68020
Apr 5, 2004
2,294
0
Huntsville, AL
Chris, I wasn't aware you could define constructors and destructors with structures.
Structs and Classes in C++ are basically the same. They differ in the default access of their members (structs default to public, classes to private).

BTW, he's sticking with C, so that C++ example doesn't help him. (him?)
I'm not a C guru, by any means, but I don't see why it wouldn't work. Swap String for a char array, maybe, but that's probably the only diff.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.