I am working on a math tutoring program
that asks the user two random numbers from 10 to 100,
and asks them to key in the correct answer within 5 seconds .the program must ask ten questions showing them and then display the score

Dani AI

Generated

Brief summary and a practical fix: the thread already points out presentation and scoring gaps ( flagged that a problem must be shown, and sketched counters). The missing piece is enforcing the 5‑second limit reliably. Below is a compact POSIX console approach that (1) uses a modern RNG to produce numbers in the 10–100 range, (2) prints each question, (3) gives 5 seconds for the answer using select(), and (4) tracks the score for 10 questions.

#include <iostream>
#include <string>
#include <random>
#include <sys/select.h>
#include <unistd.h>

bool read_line_timeout(std::string &out, int seconds) {
    fd_set set;
    FD_ZERO(&set);
    FD_SET(STDIN_FILENO, &set);
    struct timeval tv{seconds, 0};
    int rv = select(STDIN_FILENO + 1, &set, NULL, NULL, &tv);
    if (rv > 0) { std::getline(std::cin, out); return true; }
    return false;
}

int main() {
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<int> dist(10, 100);

    int correct = 0;
    for (int i = 0; i < 10; ++i) {
        int a = dist(gen), b = dist(gen), total = a + b;
        std::cout << "Q[" << (i+1) << "] " << a << " + " << b << " = " << std::flush;

        std::string s;
        if (!read_line_timeout(s, 5)) {
            std::cout << "\nToo slow. Answer was " << total << '\n';
            continue;
        }
        try {
            int ans = std::stoi(s);
            if (ans == total) { ++correct; std::cout << "Correct.\n"; }
            else std::cout << "Wrong. Answer was " << total << '\n';
        } catch (...) {
            std::cout << "Invalid input. Answer was " << total << '\n';
        }
    }
    std::cout << "Final score: " << correct << " / 10\n";
    return 0;
}

Notes and cautions:

  • select()/STDIN_FILENO is POSIX only (Linux/macOS). For Windows use a character-polling loop with _kbhit/_getch or the Win32 console API (PeekConsoleInput / WaitForSingleObject on STD_INPUT_HANDLE).
  • Prefer <random> (shown) for correct uniform range (10..100). The distribution expression avoids off‑by‑one mistakes.
  • Avoid std::async/std::future for portable input timeouts: standard streams block and cannot be cleanly cancelled on many platforms, which can leave threads blocked.
  • Validate input (std::stoi with try/catch) to avoid crashes on non-numeric answers.

This fills the timing gap while keeping presentation and scoring explicit (building on and suggestions).

Recommended Answers

All 6 Replies

Okay. What do you have so far?

#include <cstdlib>
#include <ctime>
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

long int random() {
   srand((unsigned)time(0));
   int random_integer;
   random_integer = (rand()%100)+1;
   return random_integer;
}

int main()
{
for(int index=0; index<10; index++) { 
   x = random();
   y = random();
   total = x+y; 
   cout<<"Enter answer: ";
   cin>>answer;

if (answer == total)
   cout<<"That is correct"<<endl;
else {
cout<<"The true answer is: "<<total<<endl;
    }
}
return 0;
}

You don't actually present the problem to the student - are they supposed to telepathically know what values are returned to x and y?

I would only call srand( ) once, in main( ). The way you are using it, I would expect the same to "random" values to be returned for both x and y.

Lines 11-13 could be combined into just one

return (rand()%100)+1;

which now becomes the entire body of the function.

I don't see where you are keeping count of correct and incorrect attempts.

thats where am stuck

Well you could put two variables, one that will count the correct answers and another one that will count the wrong answers:

#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

long int random() {
   return (rand()%100+1);
}

int main(){
    srand(time(NULL));
    int x, y, total, answer, correct=0, wrong=0, index=0;
    for (int index=0;index<10;index++) {
           x = random();
           y = random();
           total = x+y;
           cout<<"Q["<<index<<"]\nX: "<<x<<", Y: "<<y<<";\nX+Y = ";
           cin>>answer;
        if (answer == total)
            cout<<"That is correct.\nCorrect answers: "<<++correct<<endl;
        else
            cout<<"The true answer is: "<<total<<"\nWrong answers: "<<++wrong<<endl;
    }
    return 0;
}

hi,pl what language u working on?

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.