Computing.Net > Forums > Programming > Simple C++ prog help plz

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Simple C++ prog help plz

Reply to Message Icon

Name: Dark_Kartug
Date: April 12, 2005 at 02:15:14 Pacific
OS: WinXP
CPU/Ram: 800/512
Comment:

Hi @ everyone again.

I havent gotte naround to alot of programming lately but just today i found some time to start on something new. Im sure you all know the infamous Turtle Graphics Program. I have a little problem...but heres my code first.

//----------INCLUDES----------
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <iomanip>
using std::setw;

//----------PROTOTYPES----------
int penUP( int );
int penDOWN( int );
enum turnRIGHT( enum );
enum turnLEFT( enum );
void moveFORWARD(int & [][20],int, enum );
void print(int & [][20]);
void commandList();
int end(int);


//----------PROGRAM STARTS HERE----------
int main()
{
//main declarations
int command = 0, penStatus = 0;
enum Direction ( LEFT, RIGHT };

//declare floor array and initialize to 0
int floor[20][20];

for(int i = 0; i < 20; i++)
{
for(int j = 0; j < 20; j++)
{
floor[i][j] = 0;
}
}

Direction moveDirection; //Direction enum object declared

cout << "Welcome! Im zour drawing TURTLE for tonight."
<< "\nTell me how to move" << endl;

//loop until sentinal value returned
while(command != -1)
{
cout << "Please Insert Command (10 for command list): ";
cin >> command;
cout << endl;

//process command input
switch(command)
{
case 1: //input command 1
penDOWN(penStatus); //lower the pen
break; //break out of switch

case 2: //input command 2
penUP(penStatus); //raise the pen
break; //break out of switch

case 3: //input command 3
turnRIGHT(moveDirection); //turn turtle right
break; //break out of switch

case 4: //input command 4
turnLEFT(moveDirection); //turn turtle left
break; //break out of switch

case 5: //input command 5
moveFORWARD(floor[20][20],penStatus,moveDirection); //Move turtle forward x times
break; //break out of switch

case 6: //input command 6
print(); //print floor array
break; //break out of switch

case 9: //input command 9
end(); //returns sentinal value
break; //break out of switch

case 10: //input command 10
commandList(); //display list of commands
break; //break out of switch

default:
cout << "Invalid command input."
<< " Enter new command." << endl;
break;
}
}
return 0; //indicate successful termination
}

//----------FUNCTIONS----------
int penUP( int newPenStatus )
{
cout << "Im not drawing now" << endl;
return newPenStatus = 2;
}

int penDOWN( int newPenStatus )
{
cout << "Im ready to draw" << endl;
return newPenStatus = 1;
}

enum turnRIGHT( enum newDirection )
{
cout << "From now on I shall move to the right!" << endl;
return newDirection = RIGHT;
}

enum turnLEFT( enum newDirection )
{
cout << "From now on I shall move to the left!" << endl;
return newDirection = LEFT;
}

void moveFORWARD( int array [][20], int currentPenStatus, enum currentDirection)
{
int steps;
cout << "How many steps should I take? ";
cin >> steps;

if(currentPenStatus == 1)
{
if(currentDirection == RIGHT)
{
for(int i = 0; 0 < steps; i++)
{
//to be created
}
}
}

}

void print()
{
}

void commandList()
{
}

int end()
{
cout << "Creativity makes you sleepy does it? Good night." << endl;
return command = -1;
}


Now the problem i am having is in the moveFORWARD function (which is also the actual drawing routine). When i set the direction of enum to left or right, how can i make sure i loop through the array properly? For example if you would 'travel' vertically through the area, its all well and good (just increment/decrement the counter value in the for loop respectivly to row/column). But if i were 'travel' horizontaly then it doesnt work anymore.

Or maybe i just have a mental block. Can someone help me out?

Cheers
Dark_Kartug

War sucks, but the sound is good and WE are the DJ's



Sponsored Link
Ads by Google

Response Number 1
Name: wizard-fred
Date: April 12, 2005 at 07:34:13 Pacific
Reply:

Left and Right should act the same as Up and Down.

A question. Why are you trying to reinvent LOGO and not including all the good stuff?


0

Response Number 2
Name: borelli35
Date: April 12, 2005 at 17:25:59 Pacific
Reply:

==============================================================================
Actually, left and right should NOT act the same as up and down.

The trick is remembering what you already know. Programmers have the benefit of knowing (or at least knowing where) the information they need is stored or specifically what it is all together. i.e. how wide is your matrix? 200 cells? then up means -200 and down means +200 (first checking for >=200 condition of course). Your matrix is 500?! Well it's all the same to me...-500/+500. What's that you say? The matrix width is unknown?!!! well -/+ mWidth (still checking for >=mWidth) etc....

borelli35


0

Response Number 3
Name: Dark_Kartug
Date: April 12, 2005 at 18:07:23 Pacific
Reply:

@borrelli
I think im getting the idea haha...basically like a vector plane with inverted y axis. ok i will give it a shot

@wizard
Well im doing some teach-yourself-C++. i thought trying to 'reinvent the wheel' without ever having seen the original is gonne give me a good strong understanding of the concepts of arrays and control structures. So first im gonne get a basic version working...and then i can add more features later on. Theoretical knowledge is worhtless if you dont know how to apply it right?

War sucks, but the sound is good and WE are the DJ's


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Simple C++ prog help plz

C Prog. Help www.computing.net/answers/programming/c-prog-help/5663.html

simple C++ code help www.computing.net/answers/programming/simple-c-code-help/8207.html

simple C++ compile problem in Linux www.computing.net/answers/programming/simple-c-compile-problem-in-linux-/1282.html