0

Hello

I have this code bellow i need some help with. im trying to make a number game but i have a problem that i cant solve, the loop i made dosent update the grid like it's supposed to do. when you first start it you will se 2 grids with numbers in them like the ones bellow. one is the grid im going to play in and the other is a goal grid, howwever the problem is that then i press any of the buttons "W A S D" it only lopps the textline " cout << "You move by using W A S D on you keyboard: ";" how can i make it also look the "start" grid ?

*note: inside goal.txt and start.txt is currently this*

1 2 3
4 5 6
7 8 X

// heero.cpp : Defines the entry point for the console application.
//

/* string map1,map2
cout "map" "(start.txt/start2.txt): ";*/

#include "stdafx.h"
#include <iostream>  
#include <fstream>
#include <string>
using namespace std; 

void jump(char start[][3],int x, int y);

int main() 
{
int yes,abryt,x,y; //deklarerade variabl
char start[3][3], goal[3][3]; //börjar på mitt grid


ifstream infil("start.txt");
//läser in filen som blivit vald
infil >> start[0][0] >> start[0][1] >> start[0][2] 
      >> start[1][0] >> start[1][1] >> start[1][2] 
	  >> start[2][0] >> start[2][1] >> start[2][2];
//tar reda på vart X är på gridet
	  	for(int i=0;i<3;i++){
		for(int j=0;j<3;j++){
			if (start[i][j] == 'X'){
			y=i;
			x=j;
			cout << "your on Square: " << "Y: " << y << " X: " << x << endl << endl;
          
			}
		}
	}
//skriver ut mitt start grid
cout << start[0][0] << " " << start[0][1] << " " << start[0][2] 
     << endl << start[1][0] << " " << start[1][1] << " " << start[1][2] 
	 << endl << start[2][0] << " " << start[2][1] << " " << start[2][2] 
	 << endl << endl;

ifstream infil2("goal.txt");
//läser in mitt målgrid från vald fil
infil2 >> goal[0][0] >> goal[0][1] >> goal[0][2] 
       >> goal[1][0] >> goal[1][1] >> goal[1][2] 
	   >> goal[2][0] >> goal[2][1] >> goal[2][2];
//skriver ut mitt mål grid
cout << goal[0][0] << " " << goal[0][1] << " " << goal[0][2] 
     << endl << goal[1][0] << " " << goal[1][1] << " " << goal[1][2] << endl 
     << goal[2][0] << " " << goal[2][1] << " " << goal[2][2] << endl << endl;

	 int loop = 0;
	 while (loop == 0){
		jump(start, x, y); 
  
	}
	 /* system ("cls"); */
	 

return 0;
}

void jump(char start[][3], int x, int y){
string jump2;
cout << "You move by using W A S D on you keyboard: ";
cin >> jump2;


//funktion för att avbryta spelet
if(jump2 == "avbryt"){
	string quit;
    cout << "Want to quit the game eh? Yes/no" ;
    cin >> quit;
if(quit == "yes"){
	cout << "bye message" << endl;
}
else cout << "stay message";

}
//if satser för att röra på sej
if(jump2 == "w"){
	y--;
	if (y = 0 ){
		cout << "lol, you cant move here" << endl;
	}
	
}


if(jump2 == "a"){
	x--;
	if (x = 0 ){
		cout << "lol, you cant move here" << endl;
	}
}
if(jump2 == "s"){
	y++;
	if (y = 2 ){
		cout << "lol, you cant move here" << endl;
	}
}
if(jump2 == "d"){
	x++;
	if (x = 2 ){
		cout << "lol, you cant move here" << endl;
	}
}
	}
2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by skatamatic
0

Hmmm it's not very clear what the problem actually is...but I think the reason why its looping that text is because you forgot to clear the cin stream. Use this line:

cin.clear();
cin.ignore(cin.rdbuf()->in_avail());

Put that before every 'cin' line you use to clear excess characters from being reaad from the last cin statement.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.