while cin loop works every other try.
Jul 17, 2013 at 8:26am UTC
I wrote a small game when bored, it takes user input every other try which is the issue. This happened after I changed my players symbol to a happy face using char(2). How does outputing char(2) thru cout effect cin? First I used a '@' symbol and everything ran as it should. Here is the loop below and full code below that. I tried checking the stream state, I tried clearing the buffer after each read.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
while (cin >> playermove && playermove != 'n' ){
playermove = tolower(playermove);
switch (playermove){
case 'a' :
if (!cin)
cerr << "Stream Error!" << endl;
m_left();
isTrap();
cin.clear();
break ;
case 'd' :
if (!cin)
cerr << "Stream Error!" << endl;
m_right();
isWin();
isTrap();
cin.clear();
break ;
case 's' :
if (!cin)
cerr << "Stream Error!" << endl;
m_down();
isWin();
isTrap();
cin.clear();
break ;
case 'w' :
if (!cin)
cerr << "Stream Error!" << endl;
m_up();
isWin();
isTrap();
cin.clear();
break ;
case 'y' :
clearscreen();
player_pos = 0;
goto START;
break ;
default :
cout << "That is not a option!" << endl;
clearscreen();
DrawBoard(player_pos);
cin.clear();
break ;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
void DrawBoard(int );
void m_left();
void clearscreen();
void m_right();
void m_down();
void m_up();
void isWin();
void isTrap();
string board(100,'#' );
int player_pos = 0;
char playermove;
int main()
{
START:
cout << "Welcome to Robert's SUPER LAME game!" << endl << "Make your way thru the maze of traps to the last position to win, use wasd keys to move!"
<< endl << endl << "Examples:" << endl << "Push 'd' and press enter moves right."
<< endl << "Push 'a' and press enter moves left." << endl << "Push 'w' and press enter to moves up."
<< endl << "Push 's' and press enter to moves down." << endl << "Player position = '" << char (2) << "' " << endl << endl;
DrawBoard(player_pos);
if (playermove == 'Y' || playermove == 'N' )
playermove = tolower(playermove);
while (cin >> playermove && playermove != 'n' ){
playermove = tolower(playermove);
switch (playermove){
case 'a' :
if (!cin)
cerr << "Stream Error!" << endl;
m_left();
isTrap();
cin.clear();
break ;
case 'd' :
if (!cin)
cerr << "Stream Error!" << endl;
m_right();
isWin();
isTrap();
cin.clear();
break ;
case 's' :
if (!cin)
cerr << "Stream Error!" << endl;
m_down();
isWin();
isTrap();
cin.clear();
break ;
case 'w' :
if (!cin)
cerr << "Stream Error!" << endl;
m_up();
isWin();
isTrap();
cin.clear();
break ;
case 'y' :
clearscreen();
player_pos = 0;
goto START;
break ;
default :
cout << "That is not a option!" << endl;
clearscreen();
DrawBoard(player_pos);
cin.clear();
break ;
}
}
cout << endl << endl << "Thanks For Playing!!!" << endl;
return 0;
}
void DrawBoard(const int ppos){
board = string(100,'#' ); // reset board to all '#'
board[ppos] = char (2); //set player pos on board
int seatcnt = 0;
for (int i = 0; i != 100;++i){ //print board
if (seatcnt != 9){
cout << board[i];
++seatcnt;}
else if (board[i] == char (2)){ //if first row has been printed, check if player is there and print char for player
cout << board[i] << endl;
seatcnt = 0;}
else {
cout << '#' << endl; //if player is not there, print last symbol and end the line
seatcnt = 0;}
}
}
void m_left(){
if (board[0] == char (2)){ //check if user is in first position and tryin to go left
cout << "You cant move that direction!" << endl;
clearscreen();
DrawBoard(0);
}
else {
--player_pos;
clearscreen();
clearscreen();
DrawBoard(player_pos);}
}
void m_right(){
++player_pos;
clearscreen();
clearscreen();
DrawBoard(player_pos);
}
void m_up(){
if (player_pos <= 9){
cout << "Cant go up from here!" << endl;
clearscreen();
DrawBoard(player_pos);}
else {
player_pos -= 10;
clearscreen();
clearscreen();
DrawBoard(player_pos);}
}
void m_down(){
if (player_pos >= 90){
cout << "Cant go down from here!" << endl;
clearscreen();
DrawBoard(player_pos);}
else {
player_pos += 10;
clearscreen();
clearscreen();
DrawBoard(player_pos);
}
}
void clearscreen(){
cout << string(1000,' ' ) << endl;}
void isWin(){
if (player_pos == 99)
cout << endl << "You fucking rocked this bitch and won the game!" << endl << "Want to play again!?!? (y/n)" << endl;
}
void isTrap(){
switch (player_pos){
case 2:
case 13:
case 24:
case 35:
case 46:
case 57:
case 78:
case 89:
case 95:
clearscreen();
cout << endl <<"You landed on a trap! Back to the start position!" << endl;
clearscreen();
player_pos = 0;
DrawBoard(player_pos);
break ;
}
}
Last edited on Jul 17, 2013 at 8:32am UTC
Jul 17, 2013 at 8:36am UTC
I copied and pasted the code and everything seems to work like it should. what seems to be the problem?
Jul 17, 2013 at 8:53am UTC
Nothing I just wanted you to play my game... JK, I dont know, I restarted my comp and the issue disappeared... Thanks anyway!
Topic archived. No new replies allowed.