Author Topic: Fun Little C++ Pointless Program  (Read 7245 times)

0 Members and 1 Guest are viewing this topic.

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Fun Little C++ Pointless Program
« 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.
« Last Edit: September 16, 2011, 06:43:25 pm by meishe91 »
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Ashbad

  • Guest
Re: Fun Little C++ Pointless Program
« Reply #1 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.

Offline DrDnar

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 546
  • Rating: +97/-1
    • View Profile
Re: Fun Little C++ Pointless Program
« Reply #2 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.
« Last Edit: September 16, 2011, 06:45:54 pm by DrDnar »
"No tools will make a man a skilled workman, or master of defense, nor be of any use to him who has not learned how to handle them, and has never bestowed any attention upon them. . . . Yes, [] the tools which would teach men their own use would be beyond price."—Plato's The Republic, circa 380 BC

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Fun Little C++ Pointless Program
« Reply #3 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?
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline Netham45

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2103
  • Rating: +213/-4
  • *explodes*
    • View Profile
Re: Fun Little C++ Pointless Program
« Reply #4 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.
« Last Edit: September 16, 2011, 06:53:38 pm by Netham45 »
Omnimaga Admin

Offline DrDnar

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 546
  • Rating: +97/-1
    • View Profile
Re: Fun Little C++ Pointless Program
« Reply #5 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.
"No tools will make a man a skilled workman, or master of defense, nor be of any use to him who has not learned how to handle them, and has never bestowed any attention upon them. . . . Yes, [] the tools which would teach men their own use would be beyond price."—Plato's The Republic, circa 380 BC

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Fun Little C++ Pointless Program
« Reply #6 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)
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Fun Little C++ Pointless Program
« Reply #7 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.
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline DrDnar

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 546
  • Rating: +97/-1
    • View Profile
Re: Fun Little C++ Pointless Program
« Reply #8 on: September 16, 2011, 07:10:07 pm »
Code: [Select]
char foo = 'a';
while (foo != 'q')
{
     foo = cin.get();
     cout << (int)foo;
}
"No tools will make a man a skilled workman, or master of defense, nor be of any use to him who has not learned how to handle them, and has never bestowed any attention upon them. . . . Yes, [] the tools which would teach men their own use would be beyond price."—Plato's The Republic, circa 380 BC

Ashbad

  • Guest
Re: Fun Little C++ Pointless Program
« Reply #9 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.

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Fun Little C++ Pointless Program
« Reply #10 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');
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Fun Little C++ Pointless Program
« Reply #11 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.
« Last Edit: September 16, 2011, 07:18:04 pm by meishe91 »
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline Netham45

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2103
  • Rating: +213/-4
  • *explodes*
    • View Profile
Re: Fun Little C++ Pointless Program
« Reply #12 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.
Omnimaga Admin

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: Fun Little C++ Pointless Program
« Reply #13 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.
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline Goplat

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 289
  • Rating: +82/-0
    • View Profile
Re: Fun Little C++ Pointless Program
« Reply #14 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?
Numquam te deseram; numquam te deficiam; numquam circa curram et te desolabo
Numquam te plorare faciam; numquam valedicam; numquam mendacium dicam et te vulnerabo