Omnimaga: The Coders Of Tomorrow
Welcome, Guest. Please login or register.
 
Omnimaga: The Coders Of Tomorrow
25 May, 2013, 06:56:11 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   home   news downloads projects tutorials misc forums rules new posts irc about Login Register  
+-OmnomIRC

You must Register, be logged in and have at least 40 posts to use this shout-box! If it still doesn't show up afterward, it might be that OmnomIRC is disabled for your group or under maintenance.

Note: You can also use an IRC client like mIRC, X-Chat or Mibbit to connect to an EFnet server and #omnimaga.

Pages: [1] 2   Go Down
  Print  
Author Topic: Fun Little C++ Pointless Program -  (Read 836 times) Bookmark and Share
0 Members and 1 Guest are viewing this topic.
meishe91
Super Ninja
Members
LV11 Super Veteran (Next: 3000)
***********
Offline Offline

Gender: Male
Last Login: 02 May, 2013, 23:54:14
Date Registered: 05 March, 2010, 05:39:48
Posts: 2965


Topic starter
Total Post Ratings: +102

View Profile WWW
« on: 17 September, 2011, 00:41:22 »
0

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.


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
#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.
« Last Edit: 17 September, 2011, 00:43:25 by meishe91 » Logged





For the 51st time, that is not my card! (Magic Joke)
Ashbad
Guest
« Reply #1 on: 17 September, 2011, 00:44:28 »
0

\o/ looks fun Smiley

Perhaps for keypresses, if you want to stay with standard non-DOS functions, perhaps just query cin repeatedly? Tongue  Or use something like SDL.  That would probably overkill, though.
Logged
DrDnar
LV6 Super Member (Next: 500)
******
Offline Offline

Last Login: Yesterday at 00:39:46
Date Registered: 29 October, 2010, 00:08:46
Posts: 460

Total Post Ratings: +76

View Profile
« Reply #2 on: 17 September, 2011, 00:45:13 »
0

Try doing something like

1
2
3
4
5
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.
« Last Edit: 17 September, 2011, 00:45:54 by DrDnar » Logged

"The tools which would teach men their own use would be beyond price."—The Republic
meishe91
Super Ninja
Members
LV11 Super Veteran (Next: 3000)
***********
Offline Offline

Gender: Male
Last Login: 02 May, 2013, 23:54:14
Date Registered: 05 March, 2010, 05:39:48
Posts: 2965


Topic starter
Total Post Ratings: +102

View Profile WWW
« Reply #3 on: 17 September, 2011, 00:48:09 »
0

\o/ looks fun Smiley

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

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

Try doing something like

1
2
3
4
5
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 Tongue Sorry, could you explain what that does?
Logged





For the 51st time, that is not my card! (Magic Joke)
Netham45
WOOOOOO
President
LV11 Super Veteran (Next: 3000)
*
Offline Offline

Gender: Male
Last Login: 23 May, 2013, 05:57:14
Date Registered: 26 August, 2008, 07:35:31
Location: Denver, Colorado
Posts: 2296


Total Post Ratings: +208

View Profile WWW
« Reply #4 on: 17 September, 2011, 00:52:32 »
0


1
2
3
4
5
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.
« Last Edit: 17 September, 2011, 00:53:38 by Netham45 » Logged

Creator of OmnomIRC and SpyBot45
Join LOPN(Lobsters Opposing Pink Names) now, help us fight back!
Message me for more information, and to join now!
Members: Graphmastur;Stefan Bauwens
HOLY SHIT, I HAVE A BLOG



                                     
Put this in your signature if you've played the original WFRNG
DrDnar
LV6 Super Member (Next: 500)
******
Offline Offline

Last Login: Yesterday at 00:39:46
Date Registered: 29 October, 2010, 00:08:46
Posts: 460

Total Post Ratings: +76

View Profile
« Reply #5 on: 17 September, 2011, 00:58:46 »
0

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.
Logged

"The tools which would teach men their own use would be beyond price."—The Republic
calc84maniac
Epic z80 roflpwner
Coder Of Tomorrow
LV11 Super Veteran (Next: 3000)
*
Offline Offline

Gender: Male
Last Login: Today at 03:47:14
Date Registered: 28 August, 2008, 05:09:05
Location: Right behind you.
Posts: 2735


Total Post Ratings: +373

View Profile
« Reply #6 on: 17 September, 2011, 01:05:07 »
0

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)
Logged

"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman
meishe91
Super Ninja
Members
LV11 Super Veteran (Next: 3000)
***********
Offline Offline

Gender: Male
Last Login: 02 May, 2013, 23:54:14
Date Registered: 05 March, 2010, 05:39:48
Posts: 2965


Topic starter
Total Post Ratings: +102

View Profile WWW
« Reply #7 on: 17 September, 2011, 01:07:32 »
0

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.
Logged





For the 51st time, that is not my card! (Magic Joke)
DrDnar
LV6 Super Member (Next: 500)
******
Offline Offline

Last Login: Yesterday at 00:39:46
Date Registered: 29 October, 2010, 00:08:46
Posts: 460

Total Post Ratings: +76

View Profile
« Reply #8 on: 17 September, 2011, 01:10:07 »
0


1
2
3
4
5
6
char foo = 'a';
while (foo != 'q')
{
     foo = cin.get();
     cout << (int)foo;
}
Logged

"The tools which would teach men their own use would be beyond price."—The Republic
Ashbad
Guest
« Reply #9 on: 17 September, 2011, 01:10:46 »
0

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.
Logged
calc84maniac
Epic z80 roflpwner
Coder Of Tomorrow
LV11 Super Veteran (Next: 3000)
*
Offline Offline

Gender: Male
Last Login: Today at 03:47:14
Date Registered: 28 August, 2008, 05:09:05
Location: Right behind you.
Posts: 2735


Total Post Ratings: +373

View Profile
« Reply #10 on: 17 September, 2011, 01:11:41 »
0

Technically, it's better practice to use do-while in this case:

1
2
3
4
5
6
char foo;
do
{
     foo = cin.get();
     cout << (int)foo;
} while (foo != 'q');
Logged

"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman
meishe91
Super Ninja
Members
LV11 Super Veteran (Next: 3000)
***********
Offline Offline

Gender: Male
Last Login: 02 May, 2013, 23:54:14
Date Registered: 05 March, 2010, 05:39:48
Posts: 2965


Topic starter
Total Post Ratings: +102

View Profile WWW
« Reply #11 on: 17 September, 2011, 01:12:19 »
0

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 Tongue

@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.
« Last Edit: 17 September, 2011, 01:18:04 by meishe91 » Logged





For the 51st time, that is not my card! (Magic Joke)
Netham45
WOOOOOO
President
LV11 Super Veteran (Next: 3000)
*
Offline Offline

Gender: Male
Last Login: 23 May, 2013, 05:57:14
Date Registered: 26 August, 2008, 07:35:31
Location: Denver, Colorado
Posts: 2296


Total Post Ratings: +208

View Profile WWW
« Reply #12 on: 17 September, 2011, 01:19:50 »
0

Check out ConIO, it'll let you get scancodes.

http://www.cplusplus.com/forum/beginner/12361/ has some more info on it.
Logged

Creator of OmnomIRC and SpyBot45
Join LOPN(Lobsters Opposing Pink Names) now, help us fight back!
Message me for more information, and to join now!
Members: Graphmastur;Stefan Bauwens
HOLY SHIT, I HAVE A BLOG



                                     
Put this in your signature if you've played the original WFRNG
meishe91
Super Ninja
Members
LV11 Super Veteran (Next: 3000)
***********
Offline Offline

Gender: Male
Last Login: 02 May, 2013, 23:54:14
Date Registered: 05 March, 2010, 05:39:48
Posts: 2965


Topic starter
Total Post Ratings: +102

View Profile WWW
« Reply #13 on: 17 September, 2011, 01:54:12 »
0

WOO! That was the key right there! Thanks, Netham!

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


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
#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.
Logged





For the 51st time, that is not my card! (Magic Joke)
Goplat
LV5 Advanced (Next: 300)
*****
Offline Offline

Gender: Male
Last Login: Yesterday at 07:37:57
Date Registered: 08 December, 2009, 13:17:47
Posts: 289

Total Post Ratings: +77

View Profile
« Reply #14 on: 17 September, 2011, 03:22:40 »
0

Why did you change the character constants 'w', 's', etc into their much-harder-to-recognize numeric values 119, 115, etc?
Logged

Numquam te deseram; numquam te deficiam; numquam circa curram et te desolabo
Numquam te plorare faciam; numquam valedicam; numquam mendacium dicam et te vulnerabo
Pages: [1] 2   Go Up
  Print  
 
Jump to:  

Powered by EzPortal
Powered by MySQL Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Powered by PHP
Page created in 0.299 seconds with 31 queries.
Skin by DJ Omnimaga edited from SMF default theme with the help of tr1p1ea.
All programs, games and songs avaliable on this website are property of their respective owners.
Best viewed in Opera, Firefox, Chrome and Safari with a resolution of 1024x768 or above.