Jump to content

[Programming] C++ - looking for a "cin >> ..." alternative


Kartoffelkuchen

Recommended Posts

Hi guys, probably this is the wrong forum, sorry, I didn't know where to post it otherwise. :|

So, I am trying to make a little console game with C++, I have Windows 7 installed on my PC. To make the game work, the player needs to press a key (Let's say the key to press is 'W'). When pressing that key, the speed increases by one. This would work (more or less) with the following code:

cin >> Input;
if (Input == W)
{
Speed = Speed + 1;
}

You just press W on your keyboard and confirm with enter. But I want it so that if I press the 'W' key the speed increases automatically by one, without confirming.

Isn't there something like


On ' 1 '
{
Speed = Speed +1;
}

?

If so, please let me know! I'd really appreciate any help! :)

Link to comment
Share on other sites

You would need to check for a keyboard input, and if there isn't one don't pause the program. I don't believe there's any way to do this with base C++, so you may have to make a function to do so.

Example:


if (keyboard.input())
{
if (keyboard.key == 'W')
move.forward()
}

Now, a way to get any key pressed without calling std::cin would get _getch()

You would implement it as such:


#include <conio.h>
#include <iostream>

int main() {
while (1)
{
int c;
std::cout << "Press Key: ";
c = _getch();

if (c)
{
std::cout << "key pressed: " << c << std::endl;
}
else
std::cout << "An error has occured";

}

}

This, however, will still pause the program. But it is a step forward in order to get what you want.

This gets the current key press, but still pauses the program. I believe it's what you want

Edited by TheCanadianVendingMachine
Link to comment
Share on other sites

Hey, so that with the keyboard input function didn't work, and the _getch function worked, at least more or less, but it doesn't mind which key I press, if I press a it moves on and if I press c or so it also moves on, that doesn't make any difference.

Also, I probably didn't say all what I would like the program to be. So here's the full source code:


#include <iostream>
using namespace std;

int main ()
{
cout << "\t\t\t\t\t" << "Airplane Landing" << endl;
cout << "\n\n\n\t" << " Task: Land the Airplane on the runway." << endl;

_sleep(5000);
system("cls");


// Start of the *real* program
int Level = 1;

// Level 1
if (Level == 1)
{
cout << "\t\t\t\t\t" << "LEVEL 1" << endl;

// Startparameter

double Altitude = 2000; // in Meter (m)
float Speed = 82; // in Meter pro Sekunde (m/s)
float Sinkrate = -5; // Sinkrate in Meter pro Sekunde (m/s)
float Throttle = 0; // In Prozent (1 = Max. Schub | 0 = kein Schub)
int Gear = 1; // Zwei Möglichkeiten: 1 (Extended) | 0 (Retracted)
double DistancetoAirport = 8000; // Entfernung zum Flughafen in Metern (m)
double Drag = 1; // Luftwiederstand
int Speedbrake = 1; // Luftbremse (0 = deaktiviert | 1 = aktiviert

// Anzeigen der Startparameter

cout << "Altitude: " << Altitude << "m" << endl;
cout << "Speed: " << Speed << "m/s" << endl;
cout << "Sinkrate: " << Sinkrate << "m/s" << endl;
cout << "Throttle: " << Throttle << endl;
if (Gear == 0)
{
cout << "Gear: " << "Retracted" << endl;
}
else
{
cout << "Gear: " << "Extended" << endl;
}

if (Speedbrake == 0)
{
cout << "Speedbrake: " << "Deactivated" << endl;
}
else
{
cout << "Speedbrake: " << "Activated" << endl;
}

_sleep(5000);

// Jetzt geht's los...

int Levelcomplete = 0; // 0 = Level nicht geschafft, 1 = Level geschafft

while (DistancetoAirport > 0)
{
system("cls");

// Anzeigen der Flugdaten

cout << "Altitude: " << Altitude << "m" << endl;
cout << "Speed: " << Speed << "m/s" << endl;
cout << "Sinkrate: " << Sinkrate << "m/s" << endl;
cout << "Throttle: " << Throttle << endl;
if (Gear == 0) { cout << "Gear: " << "Retracted" << endl;}
else { cout << "Gear: " << "Extended" << endl;}
if (Speedbrake == 0) { cout << "Speedbrake: " << "Deactivated" << endl;}
else { cout << "Speedbrake: " << "Activated" << endl;}



// Geschwindigkeit
Speed = Speed + (Throttle*2.5);
Speed = Speed - (Gear*0.25);
Speed = Speed - (Sinkrate/25);
Speed = Speed - (Drag/2);
Speed = Speed - (Speedbrake);


// Geschwindigkeit zu niedrig

if (Speed < 60)
{
cout << "\a\n\n\n\t\t" << "WARNUNG: GESCHWINDIGKEIT ZU NIEDRIG" << endl;
if (Speed < 50)
{
cout << "\a\a\\t" << "MELDUNG: SCHUB WURDE AUTOMATISCH AUF VOLLE LEISTUNG GESTELLT" << endl;
Throttle = 1;
Speedbrake = 0;
}
}


// Geschwindigkeit zu hoch

if (Speed > 120)
{
cout << "\a\n\n\n\t\t" << "WARNUNG: GESCHWINDIGKEIT ZU HOCH" << endl;
if (Speed > 140)
{
cout << "\a\a\\t" << "MELDUNG: SCHUB WURDE AUTOMATISCH AUF LEERLAUF GESTELLT" << endl;
Throttle = 0;
Speedbrake = 1;
}
}



// Simulationsgeschwindigkeit
_sleep(500);
}
}
}

Yeah, sorry, there are a few german comments in it, but I hope you understand the code anyway. :) So the program is a little console game. You must land an imagined airplane on the runway. You have a few elements to control the airplane, and you can see the speed, altitutde, gear state, speedbrakes and sinkrate. I'm currently just working on the speed. Everything which is in the 'while"-loop repeats. This is the real game. Inside this loop, there are the following elements:

The flight data, like altitude, speed, gear state (extended/retracted), speedbrakes and throttle

Underneath, I calculate the speed, with gear, speedbrakes and throttle in mind

Then there are two if elements, these lower or rise the throttle if you are too slow or too fast, something like the real "auto-throttle" :)

The last thing is the simulation speed. The _sleep(500) command says how long the program waits before the loop starts again

You can configure the configuration of the airplane at the start of "Level 1", where all the variables are created and defined.

My problem is now: You must be of course able to control the ships throttle. So if I just add a cin >> command, the loop doesn't work at all. And you'd have to confirm with enter key. So I am looking for a solution allowing me to make input with the keyboard, while the loop is running.

Sorry if I didn't clarify this, I hope it is clear now. Is there something like this? It's funny to make a small program like this, but it's pretty much useless if you can't interact with it, hehe:D

Link to comment
Share on other sites

Quick question - what compiler are you using, and what C++ version?

And to your question of where you don't want the loop to stop, that's a harder problem. Because in order to do that, you need to have an If statement that checks if there's input from the keyboard. And if there isn't, don't pause the loop.

There isn't anyway to check this in the base C++ library. You would be better off downloading another library that does this for you, such as SFML, or SDL and use their input function.

Edited by TheCanadianVendingMachine
Link to comment
Share on other sites

you can always use the cstrings library. though i perfer doing all my string manip the c++ way. its great for writing file parsers and the like.

<moderation>Moved to The Space Lounge, where things other than KSP are discussed.</moderation>

actually this falls under the tag [Computer Science] and would be better off in the science labs. might get more programmers eyes on it.

Link to comment
Share on other sites

Err... he's just talking about reading the input stream, isn't he?

I mean, yes, you would have to program a fair bit, but...

*goes to read up on reading the stream*

Okay, I've come to believe this is compiler specific behavior; I recall dealing with "garbage" on the input stream in the past (which means that functions like std::cin.get() could read a character and exit. But g++ seems to just dump keyboard info into the output buffer (I guess expecting people to clear the buffer was too much effort)... and also treat get like "getline", expecting a carriage return for who knows what reason.

In theory, what should have happened is you could check if the stream was std::cin.eof which would indicate there was nothing on the stream, or if there was read a single character (which, given an input is available, wouldn't pause the program)

*Technically speaking, the console / terminal that you're working in has a fully operational keyboard buffer; istream is suppose to read this. You can directly read it using Microsoft Windows Libraries ( https://msdn.microsoft.com/en-us/library/windows/desktop/ms683175(v=vs.85).aspx ) but you really shouldn't need to go that far JUST to read the exact same buffer that istream is interfacing with.

Edited by Fel
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...