Omnimaga

General Discussion => Technology and Development => Computer Projects and Ideas => Topic started by: meishe91 on September 16, 2011, 06:41:22 pm

Title: Fun Little C++ Pointless Program
Post by: meishe91 on September 16, 2011, 06:41:22 pm
I'm a teacher's aide and today the class was taking a quiz so I was pretty bored. So I decided to make a little walking man program using the console window...considering I can't do much else that's what I was limited to haha. Anywho, here is the code.

Code: [Select]
#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;
}

There really is no purpose to this code at all. Just a little fun. Probably not even the best written code either. If you guys want to make suggestions I am all for them.

Note:
I know of no way of detecting key presses in C++ so that is why you need to input a W, A, S, or D to move him. Also, if you put him in the top-left corner and paste in "ddddddddddssssssssssaaaaaaaaaawwwwwwwwwwsssssddddd" it will do a little box and then go to the middle. Ya...

Anywho, enjoy this random program.
Title: Re: Fun Little C++ Pointless Program
Post by: Ashbad on September 16, 2011, 06:44:28 pm
\o/ looks fun :)

Perhaps for keypresses, if you want to stay with standard non-DOS functions, perhaps just query cin repeatedly? :P  Or use something like SDL.  That would probably overkill, though.
Title: Re: Fun Little C++ Pointless Program
Post by: DrDnar on September 16, 2011, 06:45:13 pm
Try doing something like
Code: [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.
Title: Re: Fun Little C++ Pointless Program
Post by: meishe91 on September 16, 2011, 06:48:09 pm
\o/ looks fun :)

Perhaps for keypresses, if you want to stay with standard non-DOS functions, perhaps just query cin repeatedly? :P  Or use something like SDL.  That would probably overkill, though.

Ya...I don't really know what you mean :P I'm still pretty much a C++ noob.

Try doing something like
Code: [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.

Ya...double for this post :P Sorry, could you explain what that does?
Title: Re: Fun Little C++ Pointless Program
Post by: Netham45 on September 16, 2011, 06:52:32 pm
Code: [Select]
while (char foo != 'q')
     foo = cin.get();
     cout << (int)foo;
}

Give that a try. cin.get should return one char off of the stream. It'll also block until it has something to return.
Title: Re: Fun Little C++ Pointless Program
Post by: DrDnar on September 16, 2011, 06:58:46 pm
Both of us are getting at the same thing: pressing an arrow key might return a special, non-printable "char". It might not show anything interesting, but it still returns a value. Then, we typecast it to int to print the numerical value of that special char. Netham's cin.get() might work a little better.
Title: Re: Fun Little C++ Pointless Program
Post by: calc84maniac on September 16, 2011, 07:05:07 pm
I thought console input waits for the user to press enter, then it starts returning characters one after another (only blocking again after the newline)
Title: Re: Fun Little C++ Pointless Program
Post by: meishe91 on September 16, 2011, 07:07:32 pm
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.
Title: Re: Fun Little C++ Pointless Program
Post by: DrDnar on September 16, 2011, 07:10:07 pm
Code: [Select]
char foo = 'a';
while (foo != 'q')
{
     foo = cin.get();
     cout << (int)foo;
}
Title: Re: Fun Little C++ Pointless Program
Post by: Ashbad on September 16, 2011, 07:10:46 pm
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.

Probably because Netham's example is missing a bracket right after the 'while' declaration line.
Title: Re: Fun Little C++ Pointless Program
Post by: calc84maniac on September 16, 2011, 07:11:41 pm
Technically, it's better practice to use do-while in this case:
Code: [Select]
char foo;
do
{
     foo = cin.get();
     cout << (int)foo;
} while (foo != 'q');
Title: Re: Fun Little C++ Pointless Program
Post by: meishe91 on September 16, 2011, 07:12:19 pm
Oh wow...duh. I feel like an idiot now. I'll let ya know.

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.

Probably because Netham's example is missing a bracket right after the 'while' declaration line.

Well yes, but no that is not why 'cause I fixed that.

Edit:
Arrow buttons don't return anything.
Also, I don't know why I didn't understand what you guys were trying. I, for whatever reason, thought foo was some command or function or something :P

@calc84maniac
Ya, in console you do have to press the Enter button. At least using cin you do. I don't know if there is another way or not.
Title: Re: Fun Little C++ Pointless Program
Post by: Netham45 on September 16, 2011, 07:19:50 pm
Check out ConIO, it'll let you get scancodes.

http://www.cplusplus.com/forum/beginner/12361/ has some more info on it.
Title: Re: Fun Little C++ Pointless Program
Post by: meishe91 on September 16, 2011, 07:54:12 pm
WOO! That was the key right there! Thanks, Netham!

New modified code to work how I originally intended it to:

Code: [Select]
#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;
}

Note:
Normal WASD keys to move. Q to quit.
Title: Re: Fun Little C++ Pointless Program
Post by: Goplat on September 16, 2011, 09:22:40 pm
Why did you change the character constants 'w', 's', etc into their much-harder-to-recognize numeric values 119, 115, etc?
Title: Re: Fun Little C++ Pointless Program
Post by: meishe91 on September 16, 2011, 11:57:20 pm
The original code was using W, A, S, and D but you had to press Enter each time. Then Netham showed me that thread and that's how I learned to do it, there wasn't anything about recognizing them by letter. If you have something to offer please do.
Title: Re: Fun Little C++ Pointless Program
Post by: Netham45 on September 17, 2011, 02:40:45 am
Just check if it's equal to '<char>'.
'w' = 119d, 'a' = 97d 'd'=100d, 's'=115d.
Title: Re: Fun Little C++ Pointless Program
Post by: z80man on September 17, 2011, 02:55:52 am
With a few modifications your first version will compile under Linux but the second version will not because it relies on conio.h which was originally designed for MS DOS. btw try to avoid to avoid system() calls as they are very platform specific. For some of functionality you need though higher level libraries are needed which will require you to leave the console. Part of the problem is just that consoles don't work very well for graphical apps.
Title: Re: Fun Little C++ Pointless Program
Post by: Munchor on September 17, 2011, 08:41:50 am
With a few modifications your first version will compile under Linux but the second version will not because it relies on conio.h which was originally designed for MS DOS. btw try to avoid to avoid system() calls as they are very platform specific. For some of functionality you need though higher level libraries are needed which will require you to leave the console. Part of the problem is just that consoles don't work very well for graphical apps.

system(); calls the command line, in whichever system we are. Different systems have different terminal languages (bash, batch, etc).

I never use them unless if my program is supposed to be platform-specific. And what you said about conio.h is also true ;)
Title: Re: Fun Little C++ Pointless Program
Post by: meishe91 on September 17, 2011, 10:41:09 am
I know that system() is relatively Windows specific but for the purposes of my class that is what we are supposed to use for the most part to get the program from just starting, running, and finishing.

Edit:
Code: [Select]
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;
void move(int 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;
int direction=27;
do
{
move(direction,x,y);
display(x,y);
direction=getch();
system("cls");
}while(direction!='q');
system("pause");
return 0;
}