|
|
|
| Test 1 | |
| Name: Key hole | Time: 30 min |
| Generate a random number between 1 and 127, then output it, then ask the user to press the key which holds that ASCII value. If they press Esc, the answer should be displayed. Hint: use type casting - put (type) infront of the variable to change its type, eg: int Key=116; cout << (char)Key; the output would be - t . | |
| Answer | ||
| Answer below - drag the cursor over the white space to highlight the answer | ||
| Test 1 | Key hole | Program , Source code |
#include <conio.h>
#include <iostream.h>
#include <time.h>
void main()
{
int number;
char Key;
bool exit=false;
randomize();
number = rand()%127;
cout << "Press key " << number << ": ";
Key=getch();
while((Key!=27)&&(exit==false))
{
if((int)Key==number)
{
cout << "\nCorrect!, press any key.";
exit=true;
}
else
{
cout << "\nThe key " << Key << " of ASCII value " << (int)Key << " is Wrong, try again: ";
}
Key=getch();
}
if(exit==false)
{
cout << "\nThe answer is: " << (char)number;
getch();
}
}
| ||
![]() |
![]() |
![]() |