9 Replies - 2669 Views - Last Post: 05 December 2010 - 12:09 PM Rate Topic: -----

#1 Guest_SwizzCheese*


Reputation:

[Help] Breaking loop when user input detected.

Posted 04 December 2010 - 10:58 AM

Hi, I am generally new to C++.

I am writing a program that times how long I play certain games. It works with a switch statement then you choose what game you want to play and then it excecutes the following code (If I select COD:Black Ops for example):

int BO() {                           //The Black Ops function.
int blackOps = 0;  
for(;;)/> {                            
blackOps++;                          //Adds 1 to blackOps.
Sleep(1000);                         //Waits 1 second.
cout << blackOps;
cout << " \n";                      
        }
         }



This code adds 1 to the integer "blackOps" every 1000 Mili seconds (Or 1 second). Thus timing how long I play in seconds.

Now, I want it to break the loop when it detects any user input (Like I press enter or any other key).

Ofcourse I can't use something like "cin >>" then it would just wait for user input and not continue with counting.

I was thinking of using a do while. loop. DO the counting timer loop WHILE no user input detected.

The problem is, how to I tell it to look for user input without stopping the counting?

Thanks in advance.

Is This A Good Question/Topic? 0

Replies To: [Help] Breaking loop when user input detected.

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3030
  • View blog
  • Posts: 10,558
  • Joined: 08-August 08

Re: [Help] Breaking loop when user input detected.

Posted 04 December 2010 - 11:14 AM

That's going to depend on the OS and framework you're using.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_SwizzCheese*


Reputation:

Re: [Help] Breaking loop when user input detected.

Posted 04 December 2010 - 11:16 AM

I am using Dev-c++ with Windows 7 Ultimate 64-bit.
Was This Post Helpful? 0

#4 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1706
  • View blog
  • Posts: 3,268
  • Joined: 30-May 10

Re: [Help] Breaking loop when user input detected.

Posted 04 December 2010 - 11:29 AM

http://www.adrianxw..../Consoles5.html
Scroll down to where GetNumberOfConsoleInputEvents are described.
Was This Post Helpful? 1
  • +
  • -

#5 Guest_SwizzCheese*


Reputation:

Re: [Help] Breaking loop when user input detected.

Posted 04 December 2010 - 02:52 PM

I'm having a little trouble understanding it. Care to explain?
Was This Post Helpful? 0

#6 Guest_SwizzCheese*


Reputation:

Re: [Help] Breaking loop when user input detected.

Posted 05 December 2010 - 02:26 AM

Bump.
Was This Post Helpful? 0

#7 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1706
  • View blog
  • Posts: 3,268
  • Joined: 30-May 10

Re: [Help] Breaking loop when user input detected.

Posted 05 December 2010 - 02:41 AM

> Care to explain?
No.

Read it, try it for yourself.
If you get stuck, then post the code YOU tried and the error messages/bugs you're stuck on.
Was This Post Helpful? 1
  • +
  • -

#8 Guest_SwizzCheese*


Reputation:

Re: [Help] Breaking loop when user input detected.

Posted 05 December 2010 - 04:02 AM

int BO() {  
int blackOps = 0;  
    for(;;)/> {    
    blackOps++;
    Sleep(1000);
    cout << blackOps;
    cout << " \n";
        if(kbhit())
                {
                   
                   blackOps = blackOps / 60;
                   cout << "You have been playing for: ";
                   cout << blackOps;
                   cout << " minutes \n";
                   system("PAUSE");
                   break;
                   
                }        
            }
             }



Works like a charm.
Was This Post Helpful? 0

#9 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1706
  • View blog
  • Posts: 3,268
  • Joined: 30-May 10

Re: [Help] Breaking loop when user input detected.

Posted 05 December 2010 - 07:46 AM

Well it depends on your point of view I suppose.

But using #include <conio.h> and kbhit() to solve your problems today, when they were rendered obsolete about 20 years ago seems a little sub-optimal to me.

That API died with DOS (a 16-bit OS), and you're using a 64-bit OS (do you see the contradiction here?)
Was This Post Helpful? 0
  • +
  • -

#10 Guest_SwizzCheese*


Reputation:

Re: [Help] Breaking loop when user input detected.

Posted 05 December 2010 - 12:09 PM

Found another way of solving the problem. Here's the whole program:

#include <time.h>
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;

int time() {




string name;
cout << "What game do you wish to time? ";
cin >> name;
    
    const time_t start = time(NULL);

    cout << name;
    cout << " timing....\n";
    cout << "Enter any key and press enter to stop \n";
    std::string input;
    std::cin >> input;

    const time_t stop = time(NULL);
    
    if (difftime(stop,start) > 3600) {
      std::cout << "You played " << difftime(stop, start) << " hours." << std::endl;
    system("PAUSE");
                                     } else {
                                          
    std::cout << "You played " << difftime(stop, start) / 60 << " minutes." << std::endl;
    system("PAUSE");
    }

char sdate[9];

char stime[9];

_strdate( sdate );

_strtime( stime );

 
 
 ofstream time;
 time.open ("C:\\time.txt", fstream::app);
 time << name;
 time << " played for: ";
 time << difftime(stop, start) / 60;
 time << " minutes. \n";
 time << "On: ";
 time << sdate;
 time << " At: ";
 time << stime;
 time << endl;
 time.close();
            
             }
             
int played() {
system("START C:\\time.txt");
                  }

int resetTimes() {
system("DEL C:\\time.txt");
                 }

int whatGame() {

int choice = 0;
cout << "1. Start game timer. \n";
cout << "2. View game times. \n";
cout << "3. Reset game times. \n";
cout << "4. Exit program. \n";
cout << "Only type the number. Ex. 4 \n";
cout << " \n";
cin >> choice;

    switch (choice) {
    
    case 1:time();
    break;

    
    case 2:played();
    break;
    
    case 3:resetTimes();
    break;
    
    case 4:
    exit(1);
    break;
    
    default:
    cout << "Please enter only 1, 2, 3 or 4.";


             }
             }

int main() {
    for(;;)/> { 
    whatGame();
            }
}


Was This Post Helpful? 0

Page 1 of 1