can you please show me how to make a countdown timer?
and a game program, like guess the missing word. because when i tried it, it doesn't give the right output. please help me. because i really don't know much about c++ i might fail to this subject.:S:'(

Dani AI

Generated

Brief summary and what to fix first: earlier replies correctly called out the root problems — your variables need to hold text and your escape sequences were wrong (see , and ). also suggested clearer names and initializing the correct answer. The remaining task is how to add a countdown that actually limits the time to answer: a safe, portable option is to read input on a background task and wait for it with a timeout. That avoids copying the clock()-based example already shown and works on both Windows and Linux with C++11 or later.

Example (portable, uses std::async + std::future; returns an empty string on timeout):

#include <iostream>
#include <string>
#include <future>
#include <chrono>
#include <algorithm>
#include <cctype>

std::string timedReadLine(unsigned seconds) {
    auto fut = std::async(std::launch::async, [](){
        std::string s;
        std::getline(std::cin, s);
        return s;
    });
    if (fut.wait_for(std::chrono::seconds(seconds)) == std::future_status::ready)
        return fut.get();
    return {}; // timeout
}

std::string toLower(std::string s) {
    std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c){ return std::tolower(c); });
    return s;
}

int main() {
    const std::string correctAnswer = "room"; // your answer
    std::cout << "it's four am and i'm waking up to your _______\n";
    std::cout << "You have 8 seconds. Answer: " << std::flush;
    std::string answer = timedReadLine(8);

    if (answer.empty()) {
        std::cout << "\nTime's up!\n";
    } else if (toLower(answer) == toLower(correctAnswer)) {
        std::cout << "Correct!\n";
    } else {
        std::cout << "Wrong answer!\n";
    }
}

Notes and caveats: std::getline in a background task may remain blocked if the user never types anything; for a one-shot question that ends the program this is usually fine, but for a continuous game you should either use platform-specific non-blocking console APIs (select/kbhit) or design the loop to avoid leaving blocked reader threads. Always use std::getline (so multi-word answers are accepted), trim leading/trailing whitespace, and do a case-insensitive compare (example toLower above). Flush output where you want the prompt/timer visible (std::flush) and compile with a C++11-capable compiler.

Recommended Answers

All 14 Replies

Show us what you tried, so we can make corrections and give you some suggestions on how to improve your code.

If you post the code you have tried to make then we can see what the errors are.

#include <iostream>
#include <string>
using namespace std;

int main()
{
	int word, a, perfume;

	
	cout<<"Singing bee game./nGuess the missing word for phrase."<<endl;
	cout<<"Are you ready?/nset/nStart!"<<endl;

	cout<<"it's four am and i'm waking up to your _______"<<endl;
	cout<<"what is the missing word to complete the phrase?"<<endl;
	cin>>word;

	a=perfume;

	if (word == a)
	{
		cout<<"Correct answer!/nYou can now proceed to the next song."<<endl;
	}
	else
	{
		cout<<"Wrong answer!/nSorry, try again next time :P"<<endl;
	}

	return 0;

}

this is what i did
and i don't know how can i put the timer.

sorry for the late reply.huhu

int is a number, not word. Instead use std::string.

/n <-- this is one main problem! it should be \n which is new line.

change "int" into char or sting

int word, a, perfume;

These variable are used for numbers :

int word, a, perfume;

What you need to use is something that can handle words, like std::string;

string word, a, perfume;

Also I notice that "a" variable, is the correct word that you are trying to compare in this statement :

if (word == a)

That means that you should initialize "a" variable to something like so:

string a = "room"; //correct answer
string word, perfume;

Also it would be better if your variable name is not "a". Give it something more meaningful like correctAnswer;

Here is a complete program , which is the same as yours but with my suggestion incorporated.

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string userInput;
	string correctAnswer = "room";

	
	cout<<"Singing bee game.\nGuess the missing word for phrase."<<endl;
	cout<<"Are you ready?\nset\nStart!"<<endl;

	cout<<"it's four am and i'm waking up to your _______"<<endl;
	cout<<"what is the missing word to complete the phrase?"<<endl;
	cin>> userInput;


	if(userInput == correctAnswer)
	{
		cout<<"Correct answer!\nYou can now proceed to the next song."<<endl;
	}
	else //incorrect answer
	{
		cout<<"Wrong answer!/nSorry, try again next time :P"<<endl;
	}

	return 0;

}

Also I am not sure if its supposed to be case insensitive or not, but it is in the above program.

tnx a lot.
uhm..
how can i put a timer in this program?
what is the syntax for a countdown timer?

To use timer, you need to use the clock function. Maybe this example will help you :

#include <iostream>
#include <ctime> //for clock()

using namespace std;

bool myPrint()
{
	static int cnt = 10;
	cout << cnt << "!\n";
	cnt--;

	if(cnt <= 0)
		return false; //stop at 0

	return true;
}
int main()
{	
	//convert our starting time to seconds
	unsigned int clk_strt = clock()/CLOCKS_PER_SEC; 
 
	unsigned int UPDATE_TIME = 1; //update every 1 seconds

	while(true)
	{		
		unsigned int clk_end = clock()/CLOCKS_PER_SEC;
		//if the difference in the time we started
		//and the current time is greater than our 
		//UPDATE_TIME, which is 1 second then ...
		if(clk_end - clk_strt >= UPDATE_TIME)
		{
			//reset time
			clk_strt = clk_end;
			//count down
			if( myPrint()== false){
				cout<<"!!!BOOM!!!\n";
				break;			
			}
		}		
	}


	
	return 0;

}

tnx a lot. u really help me guys.
i really appreciated it.

how can i put this in the application?

tnx a lot.
bt how can i insert it in my application?

ctrl-c ctrl-v ?

not that.
how can i insert the time syntax to my application syntax?

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.