Need help with project assignment C++

Hi fam!

I want to make a manual counter shell app in C++.
I want to increase/decrease an integer, by the use of the up and down arrow keys.

So far, I have gotten code that detects when the arrow keys are pressed and, which (when pressed) outputs a message to the shell. (screenshot 1. example has 4 upstrokes then 1 downstroke)
screenshot1

I want to output:
1 2 3 4 3
(4 upstrokes then 1 downstroke)

How would I go about this?

Here is the code I have so far (press up or down keys):

#include <conio.h>
#include
using namespace std;

#define KEY_UP 72
#define KEY_DOWN 80

int main()
{
int c = 0;
while(1)
{
c = 0;

    switch((c=getch())) {
    case KEY_UP:
        cout << 1 << " ";
        break;
    case KEY_DOWN:
        cout << -1 << " ";
        break;

    }

}

return 0;

}

Thanks
Dave

1 Like

Haha! This was interesting to solve. Here you go:

WorkingCode
#include <conio.h>
#include <iostream>
using namespace std;

#define KEY_UP 72
#define KEY_DOWN 80

int main()
{
    int nr = 0;
    while(1)
    {
        int c = 0;

        switch((c=getch())) {
        case KEY_UP:
            nr++;
            cout << endl << nr << endl;//key up
            break;
        case KEY_DOWN:
            nr--;
            cout << endl << nr << endl;   // key down
            break;
        }

    }

    return 0;
}

The problem in your code was that with every iteration you set c back to 0, so it could only be 1 or -1, you see? I kept 0 as a mere “counter” or indicator and declared another int nr which is then either incremented when keycode is 72 (up) with nr++; or decremented when keycode is 80 (down) with nr--;

Hope this helps. Enjoy :slight_smile:

KeyDetectC++

UpdatedCodeWithESCasExitButton
#include <conio.h>
#include <iostream>
using namespace std;

#define KEY_UP 72
#define KEY_DOWN 80
#define ESCAPE 27

int main()
{
    int nr = 0;
    while(1)
    {
        int c = 0;

        switch((c=getch())) {
        case KEY_UP:
            nr++;
            cout << endl << nr << endl;//key up
            break;
        case KEY_DOWN:
            nr--;
            cout << endl << nr << endl;   // key down
            break;
        case ESCAPE:
        return 0;
        }

    }

    return 0;
}

1 Like

Omg! Thank you Bhujanga!

Yes, I do see. I knew that that was happening but did not know how to get past it.

The real kicker for me is the:

nr++;
cout<< nr

I was missing that step and was trying;

cout <<nr++

Plus, I wasn’t quite sure how to initialize the nr variable properly (int “increase” in my case)

You helped me out tremendously by not only solving the problem but also, confirming that I was on the right track (in my mind). Despite understanding the problem, I was totally stuck and could not have come up with the solution.

Also, thanks for the bonus escape function!
Dave

1 Like

You are very welcome. This happens to all of us I guess. I feel being stuck every single day since coding :sweat_smile: But one thing I learned from learning hard things is, that the higher the potential for frustration, the higher the reward once we overcome the obstacle. Let’s keep learning :sunglasses:

2 Likes

True, because I was so excited when you shared the solution!

Another thing…

I noticed that the geth is surrounded by 2 parenthesis (not counting the ones beside it).

e.g.

((geth()))

However, when I take away one set of parenthesis, it still works.

(geth())

Any logic to this or, is this a random redundancy of where I took the code from?

In your case this is the same. It doesn’t matter in how many parenthesis you wrap the expression in. Switch requires one expression to work, but the expression can have many nested expressions like:
((i) + ( 1 + 2 ))

Where (i) is an expression and (1 + 2) is another expression and together form another expression.
Hope I didn’t overcomplicate the explanation :smile:

2 Likes

No, you didn’t over complicate. You explained it well. The “together they form another expression” does explain this to me.
Thank you Alko!

1 Like