Jump to content

Timer?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1
restin84

restin84

    Learning Programmer

  • Members
  • PipPipPip
  • 53 posts
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?

#2
nicckk

nicckk

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 629 posts
Why would you need a timer? Im sure you could use a switch statement to determine what try there on.

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
What are you trying to time?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
redkid

redkid

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
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.

#5
restin84

restin84

    Learning Programmer

  • Members
  • PipPipPip
  • 53 posts
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
Kodos

Kodos

    Newbie

  • Members
  • Pip
  • 3 posts
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
restin84

restin84

    Learning Programmer

  • Members
  • PipPipPip
  • 53 posts
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?

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#9
restin84

restin84

    Learning Programmer

  • Members
  • PipPipPip
  • 53 posts
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
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#11
restin84

restin84

    Learning Programmer

  • Members
  • PipPipPip
  • 53 posts
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
restin84

restin84

    Learning Programmer

  • Members
  • PipPipPip
  • 53 posts
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.