Site Search:
 
Speak Korean Now!
Teach English Abroad and Get Paid to see the World!
Korean Job Discussion Forums Forum Index Korean Job Discussion Forums
"The Internet's Meeting Place for ESL/EFL Teachers from Around the World!"
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

What's wrong with this C++ code?

 
Post new topic   Reply to topic    Korean Job Discussion Forums Forum Index -> Technology Forum
View previous topic :: View next topic  
Author Message
Peeping Tom



Joined: 15 Feb 2006

PostPosted: Fri Jul 28, 2006 12:17 am    Post subject: What's wrong with this C++ code? Reply with quote

I know Dave's ESL maybe isn't the best place for such a post, but there seem to be a few people who know a bit about computers, so maybe someone can help.

I just started learning C++, and this is the code from the book I'm using. Actually, when I typed it, it wouldn't compile, so I downloaded the source from the book's supplementary webpage. It still didn't compile. Can someone help explain why? If I make it a remark, the error just continues on the following line.

I marked the first compiling error in red. The compiler I'm using is Dev-C++ version 4.9.8.0.

Quote:
/* Listing: WR01.cpp
* Description: Week in Review listing for week 1
*===============================================*/
#include <iostream>
using namespace std;

enum CHOICE {
DrawRect = 1,
GetArea,
GetPerim,
ChangeDimensions,
Quit };

// Rectangle class declaration
class Rectangle
{
public:
// constructors
Rectangle(int width, int height);
~Rectangle();

// accessors
int GetHeight() const { return itsHeight; }
int GetWidth() const { return itsWidth; }
int GetArea() const { return itsHeight * itsWidth; }
int GetPerim() const { return 2*itsHeight + 2*itsWidth; }
void SetSize(int newWidth, int newHeight);

// Misc. methods

private:
int itsWidth;
int itsHeight;
};

// Class method implementations
void Rectangle::SetSize(int newWidth, int newHeight)
{
itsWidth = newWidth;
itsHeight = newHeight;
}

Rectangle::Rectangle(int width, int height)
{
itsWidth = width;
itsHeight = height;
}

Rectangle::~Rectangle() {}

int DoMenu();
void DoDrawRect(Rectangle);
void DoGetArea(Rectangle);
void DoGetPerim(Rectangle);

/*===============================================*/
int main()
{
// initialize a rectangle to 30,5
Rectangle theRect(30,5);

int choice = DrawRect;
int fQuit = false;

while (!fQuit)
{
choice = DoMenu();
if (choice < DrawRect || choice > Quit)
{
cout << "\nInvalid Choice, try again. ";
cout << endl << endl;
continue;
}
switch (choice)
{
case DrawRect:
DoDrawRect(theRect);
break;
case GetArea:
DoGetArea(theRect);
break;
case GetPerim:
DoGetPerim(theRect);
break;
case ChangeDimensions:
int newLength, newWidth;
cout << "\nNew width: ";
cin >> newWidth;
cout << "New height: ";
cin >> newLength;
theRect.SetSize(newWidth, newLength);
DoDrawRect(theRect);
break;
case Quit:
fQuit = true;
cout << "\nExiting... " << endl << endl;
break;
default:
cout << "Error in choice!" << endl;
fQuit = true;
break;
} // end switch
} // end while
return 0;
} // end main

int DoMenu()
{
int choice;
cout << endl << endl; // create two new lines
cout << " *** Menu *** " << endl;
cout << "(1) Draw Rectangle" << endl;
cout << "(2) Area" << endl;
cout << "(3) Perimeter" << endl;
cout << "(4) Resize" << endl;
cout << "(5) Quit" << endl;

cin >> choice;
return choice;
}

void DoDrawRect(Rectangle theRect)
{
int height = theRect.GetHeight();
int width = theRect.GetWidth();

for (int i = 0; i<height; i++)
{
for (int j = 0; j< width; j++)
cout << "*";
cout << endl;
}
}


void DoGetArea(Rectangle theRect)
{
cout << "Area: " << theRect.GetArea() << endl;
}

void DoGetPerim(Rectangle theRect)
{
cout << "Perimeter: " << theRect.GetPerim() << endl;
}
// ========== End of Listing ==========
Back to top
View user's profile Send private message
bobohawke



Joined: 16 Jul 2006
Location: Seoul

PostPosted: Fri Jul 28, 2006 3:41 am    Post subject: C++ Reply with quote

Nothing wrong with it, i pasted it into my c++ compiler and it worked first time, are you sure your IDE is configured properly ?
Back to top
View user's profile Send private message
Demophobe



Joined: 17 May 2004

PostPosted: Fri Jul 28, 2006 3:55 am    Post subject: Reply with quote

It compiled for me as well. I didn't see it in the OP (I have now re-read it)...but try a different compiler.

Last edited by Demophobe on Fri Jul 28, 2006 3:57 am; edited 1 time in total
Back to top
View user's profile Send private message
bobohawke



Joined: 16 Jul 2006
Location: Seoul

PostPosted: Fri Jul 28, 2006 3:56 am    Post subject: C++ Reply with quote

DEV C++ 4.9.9.2
Back to top
View user's profile Send private message
Demophobe



Joined: 17 May 2004

PostPosted: Fri Jul 28, 2006 4:06 am    Post subject: Reply with quote

I haven't needed a compiler in a while, but the Comeau online test-drive compiled it correctly with 'strict C++' settings.

@ PT - Maybe upgrade to DEV C++ 4.9.9.2? bobohawke had no issues with an apparently never version of DEV C++.
Back to top
View user's profile Send private message
bobohawke



Joined: 16 Jul 2006
Location: Seoul

PostPosted: Fri Jul 28, 2006 4:09 am    Post subject: C++ 4.9.9.2 Reply with quote

You can downlaod the version of Dev C++ 4.9.9.2 on this site

http://www.freebyte.com/programming/cpp/#cppcompilers

Very Happy
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Korean Job Discussion Forums Forum Index -> Technology Forum All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


This page is maintained by the one and only Dave Sperling.
Contact Dave's ESL Cafe
Copyright © 2018 Dave Sperling. All Rights Reserved.

Powered by phpBB © 2001, 2002 phpBB Group

TEFL International Supports Dave's ESL Cafe
TEFL Courses, TESOL Course, English Teaching Jobs - TEFL International