Error Report...

Started by
20 comments, last by Lacutis 18 years, 9 months ago
Well, I've been working on my console tetris for a while and I ran into an unexpected error! I got a Error Report.. Here's my code:
#include <iostream>
#include <windows.h>
#include <string>
#include "Globals.h"
using namespace std;

int Rnd(int min, int max) {
  return (rand() / static_cast<double>(RAND_MAX + 1)) * (max - min + 1) + min;
}
string PlayingField[25];
int Block = 0;
int MaxLeft = 0;
int MaxRight = 0;  
int CurrBLine = 0;
char BlockFrag = '0';
void Refresh()
{
    system("CLS");
    cout<<PlayingField[1]<<PlayingField[2]<<PlayingField[3]<<PlayingField[4];
    cout<<PlayingField[5]<<PlayingField[6]<<PlayingField[7]<<PlayingField[8];
    cout<<PlayingField[9]<<PlayingField[10]<<PlayingField[11]<<PlayingField[12];
    cout<<PlayingField[13]<<PlayingField[14]<<PlayingField[15]<<PlayingField[16];
    cout<<PlayingField[17]<<PlayingField[18]<<PlayingField[19]<<PlayingField[20];
    cout<<PlayingField[21]<<PlayingField[22]<<PlayingField[23]<<PlayingField[24];    
}

void BlockDown()
{
         if ( Block == 1 )
         {
             PlayingField[CurrBLine - 1] = "                          |                |\n";
             PlayingField[CurrBLine] = "                          |       00       |\n";
             PlayingField[CurrBLine + 1] = "                          |       00       |\n";

             CurrBLine = CurrBLine + 1;
              
         }
         if ( Block == 2 )
         {
              
         }
         if ( Block == 3 )
         {
              
         }
         if ( Block == 4 )
         {
              
         }
         if ( Block == 5 )
         {
              
         }
         if ( Block == 6 )
         {
              
         }
         if ( Block == 7 )
         {
              
         }
    Refresh();
}

void MakeBlock()
{
     Block = Rnd(1, 7);
         if ( Block == 1 )
         {
              PlayingField[2] = "                          |       00       |\n";
              PlayingField[3] = "                          |       00       |\n";
              PlayingField[4] = "                          |                |\n";
              PlayingField[5] = "                          |                |\n";
              MaxLeft = 7;
              MaxRight = 7;
              CurrBLine = 3;
         }
         if ( Block == 2 )
         {
              PlayingField[2] = "                          |       0        |\n";
              PlayingField[3] = "                          |       0        |\n";
              PlayingField[4] = "                          |       0        |\n";
              PlayingField[5] = "                          |       0        |\n";
              MaxLeft = 7;
              MaxRight = 8;
              CurrBLine = 5;
         }
         if ( Block == 3 )
         {
              PlayingField[2] = "                          |       0        |\n";
              PlayingField[3] = "                          |       0        |\n";
              PlayingField[4] = "                          |       00       |\n";
              PlayingField[5] = "                          |                |\n";
              MaxLeft = 7;
              MaxRight = 7;
              CurrBLine = 4;
         }
         if ( Block == 4 )
         {
              PlayingField[2] = "                          |        0       |\n";
              PlayingField[3] = "                          |        0       |\n";
              PlayingField[4] = "                          |       00       |\n";
              PlayingField[5] = "                          |                |\n";
              MaxLeft = 7;
              MaxRight = 8;
              CurrBLine = 4;
         }
         if ( Block == 5 )
         {
              PlayingField[2] = "                          |       0        |\n";
              PlayingField[3] = "                          |       00       |\n";
              PlayingField[4] = "                          |        0       |\n";
              PlayingField[5] = "                          |                |\n";
              MaxLeft = 7;
              MaxRight = 7;
              CurrBLine = 4;
         }
         if ( Block == 6 )
         {
              PlayingField[2] = "                          |        0       |\n";
              PlayingField[3] = "                          |       00       |\n";
              PlayingField[4] = "                          |       0        |\n";
              PlayingField[5] = "                          |                |\n";
              MaxLeft = 7;
              MaxRight = 8;
              CurrBLine = 4;
         }
         if ( Block == 7 )
         {
              PlayingField[2] = "                          |       0        |\n";
              PlayingField[3] = "                          |      000       |\n";
              PlayingField[4] = "                          |                |\n";
              PlayingField[5] = "                          |                |\n";
              MaxLeft = 6;
              MaxRight = 7;
              CurrBLine = 3;
         }
     Refresh();
}



int main()
{
    int Looper = 0;
    
    PlayingField[1] = "                          |----------------|\n";
    PlayingField[2] = "                          |                |\n";
    PlayingField[3] = "                          |                |\n";
    PlayingField[4] = "                          |                |\n";
    PlayingField[5] = "                          |                |\n";
    PlayingField[6] = "                          |                |\n";
    PlayingField[7] = "                          |                |\n";
    PlayingField[8] = "                          |                |\n";
    PlayingField[9] = "                          |                |\n";
    PlayingField[10] = "                          |                |\n";
    PlayingField[11] = "                          |                |\n";
    PlayingField[12] = "                          |                |\n";
    PlayingField[13] = "                          |                |\n";
    PlayingField[14] = "                          |                |\n";
    PlayingField[15] = "                          |                |\n";
    PlayingField[16] = "                          |                |\n";
    PlayingField[17] = "                          |                |\n";
    PlayingField[18] = "                          |                |\n";
    PlayingField[19] = "                          |                |\n";
    PlayingField[20] = "                          |                |\n";
    PlayingField[21] = "                          |                |\n";
    PlayingField[22] = "                          |                |\n";
    PlayingField[23] = "                          |                |\n";
    PlayingField[24] = "                          |----------------|\n";

    // Draw the playing field:
    cout<<PlayingField[1]<<PlayingField[2]<<PlayingField[3]<<PlayingField[4];
    cout<<PlayingField[5]<<PlayingField[6]<<PlayingField[7]<<PlayingField[8];
    cout<<PlayingField[9]<<PlayingField[10]<<PlayingField[11]<<PlayingField[12];
    cout<<PlayingField[13]<<PlayingField[14]<<PlayingField[15]<<PlayingField[16];
    cout<<PlayingField[17]<<PlayingField[18]<<PlayingField[19]<<PlayingField[20];
    cout<<PlayingField[21]<<PlayingField[22]<<PlayingField[23]<<PlayingField[24];
    // Start the main game loop:
             MakeBlock();
    while ( Looper != '1' )
    {

         BlockDown();
         Sleep(500);
    }
    cin.get();
system("CLS");
}


Globals.h:
extern string PlayingField[25];
extern int Block;
extern int MaxLeft = 0;
extern int MaxRight = 0;
extern int CurrBLine = 0;
extern char BlockFrag = '0';

Please note that it is NOT completed. Using Windows XP, with Dev-C++. Someone else try this; I want to know if anyone else gets this error. THIS PROGRAM COMPILES PERFECTLY, I get the error report when it runs it. [Edited by - orcfan32 on July 20, 2005 11:14:45 AM]
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
Advertisement
Did you #include <string> someplace in there, perhaps the globals? (Might be nice to see those too)

~zix~
---------------------------------------------------Game Programming Resources, Tutorials, and Multimedia | Free Skyboxes
In BlockDown, is there a reason why you are doing CurrBLine = CurrBLine + '1' instead of just + 1? Because adding a character like that doesn't really add one - it adds the number which the character represents - probably something like 40 - which would cause a buffer overrun and produce the error you are seeing. Change it to CurrBLine = CurrBLine + 1 and see if that helps. Otherwise, I did not notice anything screaming errors in your code.
....[size="1"]Brent Gunning
Going off of skittleo, every variable you define has ' ' around it. The '' defines that there is text in the middle. For instance, when you say:

int Block = '0';
int MaxLeft = '0';
int MaxRight = '0';
int CurrBLine = '0';
char BlockFrag = '0';

your actually saying Block=character 0 (which is 51 in ASCII)

You wawnt to say:
int Block = 0;
int MaxLeft = 0;
int MaxRight = 0;
int CurrBLine = 0;
char BlockFrag = 0;

Go back and get rid of those '' around any numbers.

Hope that does it
~Zix~
---------------------------------------------------Game Programming Resources, Tutorials, and Multimedia | Free Skyboxes
Ehhhhhhhhhh....this looks bad..
In file included from C:/CPP/Console Tetris/Tetris.cpp:4:C:/CPP/Console Tetris/Globals.h:1: error: syntax error before `[' tokenC:/CPP/Console Tetris/Globals.h:3: warning: `MaxLeft' initialized and declared    `extern'C:/CPP/Console Tetris/Globals.h:4: warning: `MaxRight' initialized and declared    `extern'C:/CPP/Console Tetris/Globals.h:5: warning: `CurrBLine' initialized and    declared `extern'C:/CPP/Console Tetris/Globals.h:6: warning: `BlockFrag' initialized and    declared `extern'C:/CPP/Console Tetris/Tetris.cpp: In function `int Rnd(int, int)':C:/CPP/Console Tetris/Tetris.cpp:8: warning: return to `int' from `double'C:/CPP/Console Tetris/Tetris.cpp:8: warning: argument to `int' from `double'C:/CPP/Console Tetris/Tetris.cpp: At global scope:C:/CPP/Console Tetris/Tetris.cpp:12: error: redefinition of `int MaxLeft'C:/CPP/Console Tetris/Globals.h:3: error: `int MaxLeft' previously defined hereC:/CPP/Console Tetris/Tetris.cpp:13: error: redefinition of `int MaxRight'C:/CPP/Console Tetris/Globals.h:4: error: `int MaxRight' previously defined    hereC:/CPP/Console Tetris/Tetris.cpp:14: error: redefinition of `int CurrBLine'C:/CPP/Console Tetris/Globals.h:5: error: `int CurrBLine' previously defined    hereC:/CPP/Console Tetris/Tetris.cpp:15: error: redefinition of `char BlockFrag'C:/CPP/Console Tetris/Globals.h:6: error: `char BlockFrag' previously defined    here


This seriously was compiling correct before. I just exited and got back in to the code and now all these errors..BTW, I updated code in first post.
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
int Block = '0';
int MaxLeft = '0';
int MaxRight = '0';
int CurrBLine = '0';
char BlockFrag = '0';

One of these is correct, the others is correct, but not what you are expecting.
A single character contained within '' is a char, so you setting an int to '0' is actually setting it to the ASCII value of 0, which is integer 48 and not the integer number of 0.

So when you go:

if ( Block == 1 )
{
PlayingField[CurrBLine - 1]
PlayingField[CurrBLine]
PlayingField[CurrBLine + 1]

CurrBLine = CurrBLine + '1';

You are saying this:

if ( Block == 1 )
{
PlayingField[48 - 1]
PlayingField[48]
PlayingField[48 + 1]

CurrBLine = 48 + 49;

That's the problem.


Edit: What the others said :)
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
Most of those errors are caused by redinitions. The Source in header of the main code already defines the variables, making them public to all functions. You don't need to redefine the variables where you use them. So take out MaxLeft, MaxRight, etc inside the main() function (Because they are public anyway).

Hope I could help.
~Zix~
---------------------------------------------------Game Programming Resources, Tutorials, and Multimedia | Free Skyboxes
Quote:Original post by TDragon
What the previous posters said aside, the line PlayingField[CurrBLine - 1] = "... in BlockDown will cause a crash: you're trying to access PlayingField[-1] because CurrBLine starts at 0.


No it doesn't, he initializes it again in MakeBlock();
Updating source again.
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit

This topic is closed to new replies.

Advertisement