| | |
Comparing an element in an array
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
i am trying to compare an element in an array but it gets stuck in a loop. from what i see it doesnt even compare the element. I want to place a x in an array but if there already is an x there i want to place it above it.
c Syntax (Toggle Plain Text)
#include <iostream> using namespace std; const int row = 6; const int col = 7; void player (char playgrid [row][col]); int main() { char playgrid[row][col]; for (int currentrow = 0; currentrow <= row; currentrow++) { for (int currentcol = 0; currentcol <= col; currentcol++) playgrid [currentrow][currentcol] = '.'; } playgrid [6][2] = 'x'; player (playgrid); for (int currentrow = 0; currentrow <= row; currentrow++) { for (int currentcol = 0; currentcol <= col; currentcol++) cout << playgrid [currentrow][currentcol]; cout << endl; } return 0; } void player (char playgrid [row][col]) { int colselect; int rowselect = 6; cout << "Enter column select: "; cin >> colselect; bool entered = false; while (entered != true) { if (playgrid [rowselect][colselect] != 'x') { playgrid [rowselect][colselect] = 'x'; entered = true; } rowselect -= 1 ; } }
Last edited by Ancient Dragon; Apr 28th, 2007 at 6:52 am. Reason: add code tags
http://www.daniweb.com/techtalkforum...cement8-3.html
Isn't the watermark at the back of the edit box enough of a clue?
Isn't the watermark at the back of the edit box enough of a clue?
lines 9, 11, 17 and 19 -- they are looping too many times, causing data overflow. Replace "<=" with just "<".
line 16: accessing a row in the array that does not exist. Since array elements begin with 0, there is no 6th row.
line 16: accessing a row in the array that does not exist. Since array elements begin with 0, there is no 6th row.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Permutations of an N element array (C++)
- Comparing elements in an array (C++)
- Stripping the last element off an array (Perl)
- inserting an element into an array in c language (C)
- inserting an element into an array in c language (C)
- sorting an array of string (C)
Other Threads in the C++ Forum
- Previous Thread: multiplication table
- Next Thread: Proxy in C++?
Views: 1836 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






