So I'm doing a guess the secret number game for my computer science class(10 tries to guess a number between 1 and 1000). I was wondering if someone could give me some advice on adding a timer. Is that possible to do with C++. If so how?
Timer?
Started by restin84, Mar 19 2009 03:22 PM
12 replies to this topic
#1
Posted 19 March 2009 - 03:22 PM
|
|
|
#2
Posted 19 March 2009 - 05:09 PM
Why would you need a timer? Im sure you could use a switch statement to determine what try there on.
#3
Posted 19 March 2009 - 06:16 PM
What are you trying to time?
#4
Posted 19 March 2009 - 06:24 PM
I've done one like that. You don't really want a timer. Get a counter.
And then cout the number of tries needed to guess it.
And then cout the number of tries needed to guess it.
#5
Posted 19 March 2009 - 10:30 PM
No I have all the counters working fine. I guess I should have gave a better description. If the answer is guessed correctly I would like to tell the player how long it took them to guess correctly so that they could try to beat their time.
#6
Posted 19 March 2009 - 11:25 PM
Like this?
#include <iostream>
#include <string>
#include <ctime>
int main()
{
std::time_t start,end;
double total_time;
std::string fake_question;
std::cout << "Random Question?:";
std::time(&start);
std::cin >> fake_question;
std::time(&end);
total_time = std::difftime(end,start);
std::cout << "It took you:" << total_time << " secs to answer.\n";
return 0;
}
#7
Posted 20 March 2009 - 07:47 AM
Yeah exactly like that
although I'm not very familiar with stuff like...
std::cout << ...
why the std::
I see it all over the place but it never comes up in class. Whats the deal with that? haha I really do sound like a newbie eh?
although I'm not very familiar with stuff like...
std::cout << ...
why the std::
I see it all over the place but it never comes up in class. Whats the deal with that? haha I really do sound like a newbie eh?
#8
Posted 20 March 2009 - 08:01 AM
cout is part of namespace std. Many books/teachers have students use a statement like "using namespace std;" which eliminates the need to explicitly prefix cout with its namespace.
#9
Posted 20 March 2009 - 08:41 AM
Okay. Yeah that would explain it. Thanks. Do you think it just comes down to preference? To tell you the truth, I'd much rather just put "using namespace std;" at the top. Seems like a lot less work
#10
Posted 20 March 2009 - 09:52 AM
It's less work, but can introduce variable name clashes that can be VERY hard to debug. If I'm going to do that, I'll do something like "using std::cout;" so I only bring that object into scope.
#11
Posted 20 March 2009 - 10:15 AM
Well we've only touched on namespaces and using directives so I'm hoping that we will go into it more in the future. Or do you think they just teach that as a standard?
#12
Posted 20 March 2009 - 10:22 AM
I have heard from certain teachers in my structured programming course last semester that other teachers don't even talk about int as a variable. In other words, they just teach students to use double for everything to avoid confusing students about what type a variable should be. Sounds kinda silly but I guess that just makes forums like this even more important to a student.


Sign In
Create Account


Back to top









