hi,
as you can see when the value 'a' is to be entered we have to press enter with it too, is there anyway that it can happen by just entering the digit and not pressing enter after it.

:eek:

# include <iostream.h>
# include <conio.h>
# include <dos.h>
# include <stdlib.h>
# include <time.h>


void main()
    {
    clrscr();
    int reflex;
    int a,ran;
    cout<<"Press enter to alculate your reflex action";
    cout<<"When the test starts press enter";
    cout<<"A number will be displayed [0-9] you need to type the number\n";
    cout<<"to see your result";

    getch();

    clrscr();

    cout<<"3";
    delay(1000);

    clrscr();

    cout<<"2";
    delay(1000);

    clrscr();
    cout<<"1";
    delay(1000);
    clrscr();

    randomize();
    ran=random(9)+1;

    cout<<"Enter the number   "<<ran<<endl;

    clock_t start, end;
    start = clock();

    cin>>a;

    end = clock();

    clrscr();
    //cout<<(end - start) / (CLK_TCK);
    reflex=(end - start)*1000/ (CLK_TCK);



    cout<<"\n\n\n";

    if (ran==a)
    {
    cout<<"Your reflex action = "<<reflex;
    }
    else
    {
    cout<<"Wrong value entered";
    }
    getch();
    }

Dani AI

Generated

Short answer: read one keypress instead of using formatted extraction. As pointed out, that removes the need to press Enter; cin >> waits for a newline, so switching to a raw single-character read and converting from the ASCII digit to an integer is the usual fix. Since this thread targets Turbo C, that approach is the simplest there, but the rest of this note covers portability, timing accuracy, and input validation so the program is robust years later.

For modern systems (POSIX) here is a compact pattern that measures wall-clock milliseconds and returns the first key pressed without waiting for Enter:

#include <iostream>
#include <chrono>
#include <termios.h>
#include <unistd.h>

int get_one_char_raw() {
  termios oldt, newt;
  tcgetattr(STDIN_FILENO, &oldt);
  newt = oldt; newt.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  int ch = getchar();
  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  return ch;
}

auto t0 = std::chrono::steady_clock::now();
int ch = get_one_char_raw();
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
            std::chrono::steady_clock::now() - t0).count();

Notes and tips:

  • Validate the key: ensure ch is between '0' and '9' before converting, and handle other keys gracefully (retry or abort).
  • Use std::chrono::steady_clock (wall-clock) for elapsed time; clock() can report CPU time on some platforms and mislead timing for user input.
  • For Windows, use the platform equivalent (for example _getch() / _kbhit() from the platform conio), and on old Turbo C the DOS getch() approach is what was referring to — fine for hobby code but not portable.
  • Replace legacy RNG and DOS-only calls with std::mt19937 and <random> for predictable, well-seeded random digits if you update the program.
  • Echoing: decide whether the pressed digit should appear on screen; disabling echo (raw mode) suppresses it, so print the digit yourself when valid.

Recommended Answers

All 5 Replies

this program is for tc only

>is there anyway that it can happen by just entering the digit and not pressing enter after it.
Change this:

cin>>a;

To this:

a = getch() - '0';

Thx alot Narue

what does "Kisama wa akan da!" mean?

>what does "Kisama wa akan da!" mean?
You probably don't want to know. ;)

>what does "Kisama wa akan da!" mean?
You probably don't want to know. ;)

Is it anything like one of my favorite quotes from Married... With Children?

... And this is the river the Indians used to call KissYourWhiteAssGoodbye.

:p

[edit]The episode had something to do with whitewater rafting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.