Comparing an element in an array

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2007
Posts: 5
Reputation: keatsey-9 is an unknown quantity at this point 
Solved Threads: 0
keatsey-9's Avatar
keatsey-9 keatsey-9 is offline Offline
Newbie Poster

Comparing an element in an array

 
0
  #1
Apr 28th, 2007
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.
  1. #include <iostream>
  2. using namespace std;
  3. const int row = 6;
  4. const int col = 7;
  5. void player (char playgrid [row][col]);
  6. int main()
  7. {
  8. char playgrid[row][col];
  9. for (int currentrow = 0; currentrow <= row; currentrow++)
  10. {
  11. for (int currentcol = 0; currentcol <= col; currentcol++)
  12. playgrid [currentrow][currentcol] = '.';
  13. }
  14.  
  15. playgrid [6][2] = 'x';
  16. player (playgrid);
  17. for (int currentrow = 0; currentrow <= row; currentrow++)
  18. {
  19. for (int currentcol = 0; currentcol <= col; currentcol++)
  20. cout << playgrid [currentrow][currentcol];
  21.  
  22. cout << endl;
  23. }
  24.  
  25. return 0;
  26. }
  27. void player (char playgrid [row][col])
  28. {
  29. int colselect;
  30. int rowselect = 6;
  31.  
  32. cout << "Enter column select: ";
  33. cin >> colselect;
  34.  
  35. bool entered = false;
  36.  
  37. while (entered != true)
  38. {
  39. if (playgrid [rowselect][colselect] != 'x')
  40. {
  41. playgrid [rowselect][colselect] = 'x';
  42. entered = true;
  43. }
  44.  
  45. rowselect -= 1
  46. ;
  47.  
  48.  
  49. }
  50.  
  51.  
  52.  
  53. }
Last edited by Ancient Dragon; Apr 28th, 2007 at 6:52 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Comparing an element in an array

 
0
  #2
Apr 28th, 2007
http://www.daniweb.com/techtalkforum...cement8-3.html

Isn't the watermark at the back of the edit box enough of a clue?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Comparing an element in an array

 
0
  #3
Apr 28th, 2007
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.
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1836 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC