I need to set up 2 buttons to take input from the user, that increase and decrease a variable, it doesn't matter which 2 buttons are used, but preferably the up and down arrows. And I also need to have a display of the variable that constantly updates as it is changed by the user.

Basically what I'm doing is creating a simulated datalogging program used on a car's ECU. So the up and down arrows would be controlling my throttle position percentage. And the display would be constantly showing the percentage of throttle the user is controlling. I already have all the forumulas I need for all the other sensors I plan on showing (RPM, KPH, MAP sensor value, Air/fuel ratio etc etc.) I just need some sort of base sample of code of how to take the user's input in this style and have a constantly updating display.

Thanks guys.

Is this a console program? That kind of thing is better suited to windowed programs, but you can do it easily if you want:

#include <iostream>
#include <iomanip>
#include <climits> 
#include <windows.h> 

int main()
{
  int count = 0;

  for ( ; ; ) {
    if ( GetAsyncKeyState ( VK_ESCAPE ) != 0 )
      break;
    else if ( GetAsyncKeyState ( VK_UP ) & SHRT_MAX ) {
      if ( count < 100 )
        ++count;
    }
    else if ( GetAsyncKeyState ( VK_DOWN ) & SHRT_MAX ) {
      if ( count > 0 )
        --count;
    }

    std::cout<< std::setw ( 3 ) << count <<"%\r";
    Sleep ( 100 );
  }
}
#include <iostream>
#include <iomanip>
#include <climits> 
#include <windows.h> 
using namespace std;
int main()
{
  int TPP = 0;
  for ( ; ; ) {
    if ( GetAsyncKeyState ( VK_ESCAPE ) != 0 )
      break;
    else if ( GetAsyncKeyState ( VK_UP ) & SHRT_MAX ) {
      if ( TPP < 100 )
        ++TPP;
    }
    else if ( GetAsyncKeyState ( VK_DOWN ) & SHRT_MAX ) {
      if ( TPP > 0 )
        --TPP;
    }
    std::cout<< std::setw ( 2 ) << TPP <<"% Throttle\r";
    Sleep ( 100 );
int RPM
RPM = TPP * 74.5 + 750;
HANDLE hConsole = GetStdHandle ( STD_OUTPUT_HANDLE );
if ( INVALID_HANDLE_VALUE != hConsole )
{
COORD pos = {0, 1};
SetConsoleCursorPosition ( hConsole, pos );
cout << RPM << " RPMs";
}

Okay that throttle stuff worked perfectly, thanks for that :) Now though I'm trying to put in my RPM gauge and I can't seem to get it working right. I have a section of code there that puts it on the second line too. I thought I would be able to figure it out myself but I'm having lots of trouble.

This article has been dead for over six months. Start a new discussion instead.