C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-11-2009, 08:49 AM   #1
Registered User
 
Join Date: Feb 2009
Posts: 2
Exclamation Unknown Problem

I can't figure out why a program i'm working on keeps storing values in the wrong variables.

this is just the part of code that doesn't work, and I'm using Dev C++ to compile it. The problem occurs when it asks you to enter the value of 'position[2][3][2][1]' (on the screen in says "enter box 2,1 postion 2,3"), when you enter the value it somehow gets stored in the variable 'b' ('b' is the first variable displayed on each cout line, ie. it asks you to "enter box b,a position d,c" each line essentially).
I have attached the .cpp file if it helps, please just run the program (it's safe, u can see the code and compile it yourself) and when it asks you to "enter box 2,1 position 2,3" put in something like 5 (or a bigger number) and you'll see the problem on the next line straight away. In the code you can see this shouldn't happen.

The code is as follows:
Code:
#include <iostream>

using namespace std;

int main(){
    int x, y, z, w, vars, known, a, b, c, d;
    int position[3][3][3][3];
    int posible[9];
    for ( a=1; a <= 3; a++ ){
        for ( b=1; b <= 3; b++ ){
            for ( c=1; c <= 3; c++ ){
                for ( d=1; d <= 3; d++ ){
                    cout<<"enter box "<< b <<","<< a <<" position "<< d <<","<< c <<"\n";
                    cin>> position[d][c][b][a];
                }
            }
        }
    }
    // display start
    for ( w=1; w <= 3; w++ ){
        for ( y=1; y <= y; x++ ){
            for ( z=1; z <= 3; z++ ){
                for ( x=1; x <= 3; x++ ){
                    cout<< position[x][y][z][w] <<"  ";
                }
            cout<<"  ";
            }
        cout<<"\n";
        }
    cout<<"\n";
    }
    cin.get();
    cin.ignore();
    // display end
}

*EDIT: the problem occurs in the code before where it displays (//display start), but occurs again in the displaying proccess. That is why I included it all, though you could compile only the code up to where it says "//display start" and still see the problem.
Attached Files
File Type: cpp problem code.cpp (898 Bytes, 20 views)

Last edited by DarkFire; 02-11-2009 at 08:54 AM. Reason: added info
DarkFire is offline   Reply With Quote
Old 02-11-2009, 08:56 AM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Code:
for ( x=1; x <= 3; x++ ){
    cout<< position[x][y][z][w] <<"  ";
x will go outside the valid range of 0..2 in this snippet of code. In C and C++, all arrays have a zero-based index, so an array with 3 elements has a valid index of 0, 1 and 2. 3 is outside of that. Since multidimensional arrays grow large pretty quickly, you are probably going way outside your actual memory, and things go wrong because of that.

The same applies to the input section.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 02-11-2009, 08:59 AM   #3
Registered User
 
Join Date: Feb 2009
Posts: 2
oh, of course, silly me. I suppose the quick fix would be to make it an array of [4][4][4][4], though i should alter the code completely as to not waste memory.

Thanks heaps.

Also I did see the problem in the display's for y line. I just hadn't finished changing something when i copied the code.

Last edited by DarkFire; 02-11-2009 at 09:09 AM.
DarkFire is offline   Reply With Quote
Reply

Tags
c++ help, error, problem

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
A question related to strcmp meili100 C++ Programming 6 07-07-2007 02:51 PM
WS_POPUP, continuation of old problem blurrymadness Windows Programming 1 04-20-2007 06:54 PM
Unknown problem Mindphuck C Programming 3 04-08-2006 02:35 PM
Laptop Problem Boomba Tech Board 1 03-07-2006 06:24 PM
Unknown problem pink_langouste Windows Programming 2 12-04-2002 12:36 AM


All times are GMT -6. The time now is 11:37 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22