why is this c++ program not working?

Status
Not open for further replies.

shiftyape

Honorable
Feb 23, 2013
48
0
10,580
fist of al, dont critisize me for being unoriginal, stupid, or lame. the error im getting is:
<error reading characters of string>
<unable to read memory>





#include <iostream>
#include <cmath>
#include <Windows.h>
#include <ctime>
#include <cstdlib>
#include <conio.h>
#include <cstring>
using namespace std;



int main() {

char* space[39] = {" ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ",
" ", " ", " ", " ",
" ", " ", " ", " ",
" ", " "};

int a, b, c, d;

char key = ' ';
cout << "use wasd to move around" << endl;

Sleep(3000);



cout << endl; cout << endl; cout << endl; cout << endl; cout << endl; cout << endl; cout << endl; cout << endl; cout << endl; cout << endl;

a = 38;
b = 39;
c = 35;
d = 34;

while (1) {
system("cls");
cout << space[a] << "/\\" << endl; //38
cout << space << "/ \\" << endl; //39
cout << space << "| |" << endl; //39
cout << space << "| |" << endl; //39
cout << space[a] << "/ \\" << endl; //38
cout << space[c] << "--- ---" << endl; //35
cout << space[d] << "/ \\" << endl; //34
cout << space[d] << "----- ----" << endl; //34
cout << space << "| |" << endl; //39
cout << space << "/ \\" << endl; //39
cout << space << "-------" << endl; //39


char left = 'a';
char right = 'd';
char down = 's';
char up = 'w';

key = _getch();

if (key == left)
{
a = a - 1;
b = b - 1;
c = c - 1;
d = d - 1;
}
else if (key == right)
{
a = a + 1;
b = b + 1;
c = c + 1;
d = d + 1;
}
}

system("PAUSE");
return 0;
}


// i know i suck at writing code just try and help anyway

 
Solution
First you shouldn't be asking for homework help here.
Second the compiler it would help if you explained what it's suppose to do. I've found in most cases that errors result when people don't understand what it is that they need the code to do.

Your code has a number of different issues, your naming isn't consistent, your indentation isn't consistent, and your structure is a disaster. You shouldn't use system to clear the screen or pause, there are portable solutions to this. You need to clean up the code first and the error should become clear. To be fair I see several problems that would be resolved through cleaner data structures and simpler code.

ddpruitt

Honorable
Jun 4, 2012
226
0
10,860
First you shouldn't be asking for homework help here.
Second the compiler it would help if you explained what it's suppose to do. I've found in most cases that errors result when people don't understand what it is that they need the code to do.

Your code has a number of different issues, your naming isn't consistent, your indentation isn't consistent, and your structure is a disaster. You shouldn't use system to clear the screen or pause, there are portable solutions to this. You need to clean up the code first and the error should become clear. To be fair I see several problems that would be resolved through cleaner data structures and simpler code.
 
Solution

ddpruitt

Honorable
Jun 4, 2012
226
0
10,860


This is why I advised restructuring the code, there's a much bigger problem with the "array". In fact I would be surprised if the code compiles without several pages worth of warnings.
 

shiftyape

Honorable
Feb 23, 2013
48
0
10,580


this isnt a homework assignment, its just something i made in my spare time for fun. thanks for answering though. i will try and fix that stuff. sorry, i just started a programming class a couple weeks ago with no experience before that, so thats why my structure sucks. thanks for the help

didnt know about that portable stuff, but now that ive researched it, ive found cin.get(); to be better. thanks.
 

shiftyape

Honorable
Feb 23, 2013
48
0
10,580


its not, dont worry
 

shiftyape

Honorable
Feb 23, 2013
48
0
10,580


super helpful, thanks. do i just extend it to 40 then?
 
Status
Not open for further replies.