0

Ok, I already know a few people are gonna be angry I'm asking this question because there is already so much on it, but i just can seem to make any sense of it; how do you get instant Input (on a single character) in C/C++? Im slowly making my way towards a old school 'the legend of zelda' like game, so far the movement seems good, but instant input would really make it better.

Im also sure i'm doing something wrong with the char's and the numbers next to them, if anyone could help me, or provide a tutorial or link I would be very grateful.

Also when researching the 'goto' function, i released many people said it was messy and confusing. Yes, it is a bit time consuming, but not messy or confusing to me at least. Could anybody explain all the hate towards it?

Anyway, sorry for mumbling on, here's the code i have so far, feel free to rip into me if i've made tonnes of rookie mistakes.

// movement test


#include <iostream>
#include <stdio.h>


int main(void){

char move[101];



one:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n O=== \n ==== \n ==== \n ==== \n";
std::cin >> move;

if (*move == 'w' ) {
    goto one;
}
if (*move == 'a') {
    goto one;
}
if (*move == 's') {
    goto five;
}
if (*move == 'd') {
    goto two;
}



two:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n =O== \n ==== \n ==== \n ==== \n";
std::cin >> move;

if (*move == 'w' ) {
    goto two;
}
if (*move == 'a') {
    goto one;
}
if (*move == 's') {
    goto six;
}
if (*move == 'd') {
    goto three;
}



three:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ==O= \n ==== \n ==== \n ==== \n";
std::cin >> move;

if (*move == 'w' ) {
    goto three;
}
if (*move == 'a') {
    goto two;
}
if (*move == 's') {
    goto seven;
}
if (*move == 'd') {
    goto four;
}



four:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ===O \n ==== \n ==== \n ==== \n";
std::cin >> move;

if (*move == 'w' ) {
    goto four;
}
if (*move == 'a') {
    goto three;
}
if (*move == 's') {
    goto eight;
}
if (*move == 'd') {
    goto four;
}



five:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ==== \n O=== \n ==== \n ==== \n";
std::cin >> move;

if (*move == 'w' ) {
    goto one;
}
if (*move == 'a') {
    goto five;
}
if (*move == 's') {
    goto nine;
}
if (*move == 'd') {
    goto six;
}



six:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ==== \n =O== \n ==== \n ==== \n";
std::cin >> move;

if (*move == 'w' ) {
    goto two;
}
if (*move == 'a') {
    goto five;
}
if (*move == 's') {
    goto ten;
}
if (*move == 'd') {
    goto seven;
}



seven:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ==== \n ==O= \n ==== \n ==== \n";
std::cin >> move;

if (*move == 'w' ) {
    goto three;
}
if (*move == 'a') {
    goto six;
}
if (*move == 's') {
    goto eleven;
}
if (*move == 'd') {
    goto eight;
}



eight:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ==== \n ===O \n ==== \n ==== \n";
std::cin >> move;

if (*move == 'w' ) {
    goto four;
}
if (*move == 'a') {
    goto seven;
}
if (*move == 's') {
    goto twelve; 
}
if (*move == 'd') {
    goto eight;
}



nine:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ==== \n ==== \n O=== \n ==== \n";
std::cin >> move;

if (*move == 'w' ) {
    goto five;
}
if (*move == 'a') {
    goto nine;
}
if (*move == 's') {
    goto thirteen;
}
if (*move == 'd') {
    goto ten;
}



ten:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ==== \n ==== \n =O== \n ==== \n";
std::cin >> move;

if (*move == 'w' ) {
    goto six;
}
if (*move == 'a') {
    goto nine;
}
if (*move == 's') {
    goto fourteen;
}
if (*move == 'd') {
    goto eleven;
}



eleven:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ==== \n ==== \n ==O= \n ==== \n";
std::cin >> move;

if (*move == 'w' ) {
    goto seven;
}
if (*move == 'a') {
    goto ten;
}
if (*move == 's') {
    goto fifteen;
}
if (*move == 'd') {
    goto twelve;
}



twelve:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ==== \n ==== \n ===O \n ==== \n";
std::cin >> move;

if (*move == 'w' ) {
    goto eight;
}
if (*move == 'a') {
    goto eleven;
}
if (*move == 's') {
    goto sixteen;
}
if (*move == 'd') {
    goto twelve;
}



thirteen:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ==== \n ==== \n ==== \n O=== \n";
std::cin >> move;

if (*move == 'w' ) {
    goto nine;
}
if (*move == 'a') {
    goto thirteen;
}
if (*move == 's') {
    goto thirteen;
}
if (*move == 'd') {
    goto fourteen;
}



fourteen:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ==== \n ==== \n ==== \n =O== \n";
std::cin >> move;

if (*move == 'w' ) {
    goto ten;
}
if (*move == 'a') {
    goto thirteen;
}
if (*move == 's') {
    goto fourteen;
}
if (*move == 'd') {
    goto fifteen;
}



fifteen:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ==== \n ==== \n ==== \n ==O= \n";
std::cin >> move;

if (*move == 'w' ) {
    goto eleven;
}
if (*move == 'a') {
    goto fourteen;
}
if (*move == 's') {
    goto fifteen;
}
if (*move == 'd') {

}goto sixteen;



sixteen:
move[100] = 0;
std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ==== \n ==== \n ==== \n ===O \n";
std::cin >> move;

if (*move == 'w' ) {
    goto twelve;
}
if (*move == 'a') {
    goto fifteen;
}
if (*move == 's') {
    goto sixteen;
}
if (*move == 'd') {
    goto sixteen;
}




  return 0;
}
  • 1
    How to get a single character without requiring return to be pressed is platform-dependent. What platform are you writing for, or alternatively, would it be acceptable to require return after each character? – icktoofay Jan 19 '14 at 04:41
  • 1
    Also, as for `goto`s, does it not seem a bit messy to be repeating all of that code for each position? – icktoofay Jan 19 '14 at 04:42
  • 1
    The thing with goto is that it makes reading your code very tedious. To understand your code one has to read it many times to follow the flow. It's also generally symptomatic of a lack of understanding of tools that would better do the job. The cases where goto is a better design choice than other tools are rare if not none. In your case, it's not even clear what you're trying to accomplish using goto. I suggest that you never use it until you are very comfortable with the language. Then you will be "mature" enough to use it properly (if at all) – ApplePie Jan 19 '14 at 04:53
  • Have we not been introduced to Cartesian (x, y) coordinates yet? – SamB Jan 19 '14 at 04:55
  • 1
    try the curses library. – Cheers and hth. - Alf Jan 19 '14 at 05:10
  • It's probably not hate toward `goto` itself but look at this code and notice you have the same thing sixteen times with labels "one" through "sixteen"? Now in a larger program, multiply this kind of occurence by about a few hundred, and then you'll have a pretty good definition of hate. To simplify this pattern look into keywords "state machine" and "functions" – Brandin Jan 19 '14 at 05:23
  • Firstly, thank you for all immediate responses, this is a really great site. Secondly, since all of you have said 'goto' is a bad thingy to use, what are the alternates? And since this is just gonna be a small scale project is it really worth learning when I'm happy using 'goto'? And one last thing, what exactly are the numbers after the char doing? My understanding it that if it has '100', that means it can carry 100 letters or characters. And the only reason the char is made with '101' and the rest are '100' is because it gave me a warning otherwise, could anybody expand on this? – user3211304 Jan 19 '14 at 14:01

1 Answers1

1

First of all, Are you gonna create a Condition for each movement the player can do ?

Then, if you want to have only 1 character on the standard input, you can use the function Getchar which is the same in c or c++.

i'm gonna introduce you on this website : Getchar or Getchar

If you want to avoid to press "Enter", you can go in RAW mode

system ("/bin/stty raw");

==> This gonna send all the char directly in stdin

[C] Example :

#include <stdio.h>

int main ()
{
  int c;
  puts ("Enter text. Include a dot ('.') in a sentence to exit:");
  system ("/bin/stty raw");
  do {
    c=getchar();
    putchar (c);
  } while (c != '.');

  system ("/bin/stty cooked");
  return 0;
}
Laykker
  • 322
  • 1
  • 3
  • 12
  • Ouch, upvoted accidentally :( You should've introduced [this site](http://en.cppreference.com/w/cpp/io/c/getchar) instead. – Basilevs Jan 19 '14 at 06:27
  • I believe he wants to get the input as soon as the character is pressed, not after `enter` is pressed. So, this doesn't help. – Nathan S. Jan 19 '14 at 06:27
  • @NathanS, getchar won't wait for EOL. – Basilevs Jan 19 '14 at 06:29
  • @Basilevs It always has on my machines. I just tested it to make sure. But, on some machines you can avoid it: [See this SO question.](http://stackoverflow.com/questions/1798511/how-to-avoid-press-enter-with-any-getchar) – Nathan S. Jan 19 '14 at 06:32
  • 1
    External process? Why not most upvoted answer? – Basilevs Jan 19 '14 at 06:45
  • While researching ways to do this, i saw one comment about using a timer, i thought about it and how i could easily understand how it could work. But because there were no other people suggesting it i kinda just left it. It seems pretty simple; to set up a timer to take user input every second or something like that, what do you guys think? – user3211304 Jan 19 '14 at 14:08