I NEED HELP PLEASE! C++ etch a sketch program

Apr 19, 2012 at 12:28pm
Hey guys, i just really need help with a programming i have to create for a class and i honestly dont even know where to begin and its due tomorrow. Can someone please help me!! Here's what the assignment is...

This project will create a (cheesy) sketchpad similar to an Etch-a-Sketchtm, on which you’ll be able to produce high quality artwork such as:

+---------------------------------------+
|. . . . . . . . . . . . . . . . . . . .|
|. . . . . . . . . . . . . . . . . . . .|
|. . . . . . # # . . # # x . . . . . . .|
|. . . . . . . # # # # . . . . . . . . .|
|. . . . . . . . # # . . . . . . . . . .|
|. . . . . . # # # # # # . . . . . . . .|
|. . . . . # # . . . . # # . . . . . . .|
|. . . . # # . . . . . . # # . . . . . .|
|. . . . # . . . . . . . . # . . . . . .|
|. . . . # . . . . . . . . # . . . . . .|
|. . . . # . . . . . . . . # . . . . . .|
|. . . . # . . . . . . . . # . . . . . .|
|. . . . # # . . . . . . # # . . . . . .|
|. . . . . # # . . . . # # . . . . . . .|
|. . . . . . # # # # # # . . . . . . . .|
|. . . . . . . . . . . . . . . . . . . .|
|. . . . . . . . . . . . . . . . . . . .|
|. . . . . . . . . . . . . . . . . . . .|
|. . . . . . . . . . . . . . . . . . . .|
|. . . . . . . . . . . . . . . . . . . .|
+---------------------------------------+

Create a class with the following prototype:

class Sketcher
{
private:
char pad[20][20];
int cursorRow, cursorCol;
public:
Sketcher();
void draw(char direction);
void showpad();
void clear();
};

The cursor is the location where the pad will draw next, and is indicated by a row number cursorRow (1 to 20) and a column number cursorCol (1 to 20).

The constructor should set every character in the array to a period (‘.’), and set cursorRow to 20 and cursorCol to 1 (so the cursor will start out in the lower left-hand corner of the pad).

The draw() function should place a ‘#’ in the location currently occupied by the cursor, and move the cursor in the specified direction. Directions will correspond to numbers on the numeric keypad, and the only possible directions will be ‘8’ (north/up), ‘4’ (west/left), ‘6’ (east/right), and ‘2’ (south/down). If a move would put the cursor off the pad (i.e., if a move would put the cursor at a row OR column not between 1 and 20 inclusive), then the move is simply ignored.

The clear() function should set all characters in the array back to a period, but should leave the cursor in its current position (in this way, the user can start the cursor in different places).

Finally, the showpad() function should display the contents of the pad in the format shown above, with boundaries made of ‘-’, ‘|’, and ‘+’ drawn around the pad characters, and a space between consecutive entries in a row (so the spacing of rows and columns looks proportional). The position of the cursor should be indicated with an ‘x’ as shown.

When the class has been coded, write a main program that declares an object of type Sketcher, offers the following instructions:

Welcome to C++ Sketchpad. For each choice, enter either a direction using the numeric keypad (8/4/6/2 for Up/Left/Right/Down, respectively), C to clear the pad, or X to exit the program. Other choices will be ignored.

and then display the pad, and repeatedly prompt the user for their choice (i.e., “Choice: ”). After each selection by the user, move the cursor as specified and then redisplay the pad, clear the pad and redisplay it, or end the program. Ignore any other input, and simply prompt again.
Apr 19, 2012 at 2:56pm
we don't do your homework for you

1
2
Don't post homework questions
Programmers are good at spotting homework questions; most of us have done them ourselves. Those questions are for you to work out, so that you will learn from the experience. It is OK to ask for hints, but not for entire solutions. 


http://www.cplusplus.com/forum/beginner/1/
Apr 19, 2012 at 3:07pm
Alas, this is one of those things where you will have had to been working at it a bit to get it. The code is simple enough, but it is not so simple that you can just "do it" the night before. You need to work at it.

Sorry.

Hints:
You already have a lot of stuff given to you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Sketcher
{
private:
  char pad[20][20];
  // The pad is what you see when you display it: 20 rows by 20 columns
  // of characters. Initially, and after calling clear(), all the characters should
  // be set to '.' (except for the cursor position).

  int cursorRow, cursorCol;
  // This is where your cursor is.

public:
  Sketcher();
  // This is your constructor. Make sure to set the cursor to somewhere in
  // the range [0,19] for both row and column. Also make sure to call clear().

  void draw(char direction);
  // This function modifies the 'pad' array:
  // Put a '#' at the cursor position.
  // Update the cursor row and column according to the 'direction'.
  // Put a 'x' at the new cursor position.

  void showpad();
  // This displays the pad on the screen. You'll need two loops, one inside
  // the other.

  void clear();
  // Set all the characters in the 'pad' to '.', except the character at the
  // cursor, which should be an 'x'.
};

Don't worry about looking pretty. When your program runs, just keep printing to the console. You will see the last pad scroll up on the display and the new one display at the bottom. That is OK. It is possible to make it prettier without the scrolling, but you don't have time to mess with that now.

Good luck!
Apr 19, 2012 at 4:28pm
you could do something like this:
1
2
3
4
5
6
7
8
while (!GetKeyPressed('x')) //using x as the exit key
{
    if (GetKeyPressed(VK_LEFT) || GetKeyPressed(VK_RIGHT) || GetKeyPressed(VK_UP) || GetKeyPressed(VK_DOWN))
    {
        //Update position of the cursor & update the drawing based on which keys are pressed
    }
    Sleep(100); //1 second (might be 0.1 seconds, i forget)
}


to do this you would need to include <Windows.h> though, as Sleep and GetKeyPressed are part of Windows.h
Apr 19, 2012 at 6:35pm
Alright thanks alot, that was helpful so far

#include <iostream>
using namespace std;

class Sketcher
{
private:
char pad[20][20];
int cursorRow, cursorCol;
public:
Sketcher();
void draw(char direction);
void showpad();
void clear();
};

int main()
{

}

Sketcher::Sketcher()
{
for(int i=0; i<20, i++)
{
for(int j=0; j<20, j++)
{
pad[i][j]='.';
}
}
cursorRow=20;
cursorCol=1;
}

void Sketcher::draw(char s)
{
pad[cursorRow][cursorCol]='#';

while(cursorRow>20 || cursorCol>20)
{
if(s=='U')
cursorCol++;
if(s=='L')
cursorRow--;
if(s=='R')
cursorRow++;
if(s=='D')
cursorCol--;
}

pad[cursorRow][cursorCol]='x';
}

void showpad()
{
for(int i=0; i<20; i++)
for(int j=0; j<20; i++)
pad[i][j]='.';
}
does everything seem ok so far?
Apr 19, 2012 at 7:58pm
You have off-by-one errors with your array indices. Remember,valid values are 0 through 19.
Topic archived. No new replies allowed.