0 Members and 1 Guest are viewing this topic.
#include <iostream>#include <iomanip>using namespace std;void move(char direction,int &x,int &y){ if(direction=='w') { y-=(y!=1); } else if(direction=='s') { y+=(y!=11); } else if(direction=='a') { x-=(x!=1); } else if(direction=='d') { x+=(x!=11); } else {;} return;}void display(int x,int y){ cout<<"*************\n"; for(int i=0;i<11;i++) { if(i+1==y) { cout<<"*"<<setw(x)<<"X"<<setw(13-x)<<"*\n"; } else { cout<<"* *\n"; } } cout<<"*************\n";}int main(){ int x=6; int y=6; char direction='z'; while(direction!='q') { move(direction,x,y); display(x,y); cin>>direction; system("cls"); } system("pause"); return 0;}
while (char foo != 'q'){ cin >> foo; cout << (int)foo;}
\o/ looks fun Perhaps for keypresses, if you want to stay with standard non-DOS functions, perhaps just query cin repeatedly? Or use something like SDL. That would probably overkill, though.
Try doing something likeCode: [Select]while (char foo != 'q'){ cin >> foo; cout << (int)foo;}With any luck, pressing the arrow keys will return key codes. But, you know, that would probably be too easy.
while (char foo != 'q') foo = cin.get(); cout << (int)foo;}
char foo = 'a';while (foo != 'q'){ foo = cin.get(); cout << (int)foo;}
I can't get it to work. It keeps saying there is an undeclared identifier and that it is expecting an assignment operator instead of does-not-equal. So I'm not sure if I'm just missing something or if something is going on.
char foo;do{ foo = cin.get(); cout << (int)foo;} while (foo != 'q');
Quote from: meishe91 on September 16, 2011, 07:07:32 pmI can't get it to work. It keeps saying there is an undeclared identifier and that it is expecting an assignment operator instead of does-not-equal. So I'm not sure if I'm just missing something or if something is going on.Probably because Netham's example is missing a bracket right after the 'while' declaration line.
#include <iostream>#include <iomanip>#include <conio.h>using namespace std;void move(int direction,int &x,int &y){ if(direction==119) { y-=(y!=1); } else if(direction==115) { y+=(y!=11); } else if(direction==97) { x-=(x!=1); } else if(direction==100) { x+=(x!=11); } else {;} return;}void display(int x,int y){ cout<<"*************\n"; for(int i=0;i<11;i++) { if(i+1==y) { cout<<"*"<<setw(x)<<"X"<<setw(13-x)<<"*\n"; } else { cout<<"* *\n"; } } cout<<"*************\n";}int main(){ int x=6; int y=6; int direction=27; do { move(direction,x,y); display(x,y); direction=getch(); system("cls"); }while(direction!=113); system("pause"); return 0;}