0

please don't mark as duplicate first read full question i want to know how one variable or function(with return:) return two value at same time

i am printing ASCII values of characters and when i press any arrow key then two values are printed like 224 then in next line 80 printed how it possible that one variable give me two values at same time

i am using GCC on code block in windows 7

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

int show;
int main()
{
while(true)
    {
    int show = getch();
    std::cout << show<<std::endl;
    }
getch(); // Just to keep the console open after program execution
}

we know that arrow keys are special keys whom return two ASCII value at same time but how one variable can print both values same time

  • 2
    It's not the case that one variable has two values at the same time. Your code is in a loop, so the variable has two different values **in quick succession**, not at the same. In this example your code goes round the loop twice, with the variable having different values each time round the loop. – john Jul 23 '19 at 11:18
  • thanks for reply . it mean special keys like arrow keys return two values but getch() function accept it one by one . i mean one value with one loop – Manpreet Sidhu Jul 23 '19 at 11:26
  • Yes the getch function returns the two values one at a time. What makes you think that's not the case here? – john Jul 23 '19 at 11:29
  • then please explain – Manpreet Sidhu Jul 23 '19 at 11:58
  • I already did explain. I'm struggling to understand what you don't understand. Of course a variable can't have two values at the same time. I don't understand why you think that is happening here. – john Jul 23 '19 at 12:00
  • It's very simple you have a **loop**, and your code goes round the loop **twice**, with a different value for `show` **each time**. `show` does not have two values at the same time. – john Jul 23 '19 at 12:01
  • Maybe you can try and explain why you don't understand what I've just said, or why you think it is wrong. Because I'm afraid I don't understand. – john Jul 23 '19 at 12:02
  • I believe you would benefit from learning how to use a debugger. They will show you step by step what happens. – MSalters Jul 23 '19 at 12:06
  • 1
    @john I assume he wants to know why a single keypress generates 2 different characters. – IcedLance Jul 23 '19 at 12:08
  • @IcedLance The last sentence of his question shows that he understands that. (Although maybe not completely). – john Jul 23 '19 at 12:09
  • for example i am press up arrow key after run then getch assign value to show and show get printed by printf and cursor will go next line then what happen i cant understand . getch assign new value to show or something else is happen – Manpreet Sidhu Jul 23 '19 at 12:09
  • 1
    getch assigns a new value to show. – john Jul 23 '19 at 12:10
  • I do not think this is a duplicate of set post. @ManpreetSidhu said `i am printing ASCII values of characters and when i press any arrow key then two values are printed like 224 then in next line 80 printed how it possible that one variable give me two values at same time `. You store the value from `getch` inside an integer variable. `std::cout << show << std::endl;` will print 80 if the keypress results in ascii value of 80. If you want the 'character' value simply use `static_cast(show)`. – Zaiborg Jul 23 '19 at 12:28
  • @john if getch assigns a new value to show. then how getch provide the value 2nd time because getch accept one character at a time and if it accept one character then after prees any arrow key it accept 80 or 244 – Manpreet Sidhu Jul 23 '19 at 13:03
  • @ManpreetSidhu getch sits in front of a buffer. If there are no bytes in the buffer then getch waits until you press a key. For most keys one byte is added to the buffer which getch then returns and removes from the buffer. But for the up arrow key two bytes are added to the buffer, getch removes and returns the first byte from the buffer, but the second byte remains. So the next time you call getch it will return that second byte (and remove it from the buffer) without waiting for any key to be pressed. That's how getch works. – john Jul 23 '19 at 13:20
  • @ManpreetSidhu I have to say you are trying to make something very simple into something very complicated. You are probably over thinking things. Your own program is showing you what is happening, and it doesn't need any fantasy like variables having two values at the same time. – john Jul 23 '19 at 13:23
  • thanks sir your right – Manpreet Sidhu Jul 23 '19 at 15:55

0 Answers0