Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm making a game in C++ i.e. hangman in which for entering each letter as an input I want to give user a time limit of 20 seconds. In those 20 seconds if input letter is not given then program ends with display "Time is up".
Remember 'while the timer is running' I want to give input letter.
Is that possible?
Please help through code implementation.
If your platform has conio.h available and your compiler supports C++11 (including <chrono>) you can do it like this:
#include <iostream>
#include <chrono>
#include <conio.h>
int main(int argc, char* argv[]){
std::chrono::time_point<std::chrono::system_clock> start;
start = std::chrono::system_clock::now(); /* start timer */
while(true){
__int64 secondsElapsed = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now()-start).count();
if(secondsElapsed >= 20){ /* 20 seconds elapsed -> leave main lopp */
break;
}
if(_kbhit()){ /* keypressed */
char c = _getch(); /* get character */
std::cout << c; /* output character to stdout */
}
}
return 0;
}
After 20 seconds are passed the programm terminates - until that, you can enter characters to the console window (tested on Windows 7 with Visual Studio 2012).
See also the documentation for _kbhit(), _getch() and <chrono>.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm trying to create a game (tic tac toe) and I have to use a library. I used ncurses to make the appearance appealing. However, each time I implement ncurses into my code it runs but DOES NOT run my main code that starts the game.. it either exits out or does not allow me to see the user input.. What can I do? This is the code:
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
#include "board.h"
#include "players.h"
#include <ncurses.h>
using namespace std;
/**
* Controls main function of the game
*/
int main(int argc, char** argv) {
initscr();
cbreak();
refresh();
WINDOW* windowTest = newwin(1, 23, 3, 0);
wprintw(windowTest, "Hello World");
//refresh();
wrefresh(windowTest);
getch();
delwin(windowTest);
endwin();
return 0;
string name;
cout << "What's your name: ";
cin >> name;
cout << "Nice to meet you, "<< name << ". Welcome to Tic Tac Toe!" << endl;
//Player is a object and does not matter here
Player game;
game.play();
return 0;
};
The problem is these three statements in the middle of the main function, before you create your game object:
delwin(windowTest);
endwin();
return 0;
The delwin and endwin calls should be after the game.play() call. And the return statement at this location should be removed.
It seems that you took some "test" program, and just added a few statements at the end of the main function.
You need to do some better planning about your programs and how they work. Preferably starting with a list of requirements, creating an analysis from those requirements, and from the analysis create a design that you then implement.
And while test programs can be very useful, when you start on the actual project I recommend that you start from scratch instead. Then mistakes like this should not happen.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I need to organize infinite loop with symbol analysis in it. In C I used fgets(buf, N, stdin), suppose buf is buf[10]. User could type string of any length and I could analyze it by breaking down the input and examining parts of length 10. How can I implement this in C++ without using C libraries. Sorry for my English if you can't understand what I mean
In C++ you should std::cin to read from standard input.
// #include <iostream>
do
{
char buf[10]{}; // create array of 10 bytes filled with zeros.
std::cin.read(buf, 10); // read 10 bytes
// at this point you should check if std::cin.read succeeded.
// otherwise you will be reading zeros.
std::streamsize numRead = std::cin.gcount(); // obtain number of read bytes.
std::cout << numRead << " " << buf << std::endl; // some printing.
}while(std::cin);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Hellow guys i want to add nine integers to array at once time without pressing enter key in run time. please guys tell me how to add nine integers to array simultaneously in C++.
Thanks!
If you want to process each integer value right after its input in console is complete (e.g. in that a blank indicates that the next integer value shall begin), you are in a bad position.
The reason is that terminal input (beyond of what your C++ program can influence) often is buffered, and even cin might not receive any character until Enter or EOF is pressed in the terminal.
There may exist workarounds like conio.h or ncurses, but the are not standard and probably not worth the effort in your situation unless you really need to implement integer scanning for a production environment tightly connected to console input.
Try it out and compare input taken directly from console to input from a stream that is already "filled" with enough input:
int main() {
stringstream ss("12 34 56 78 90 10 11 12 13");
//istream &in = ss; // would output each integer immediately.
istream &in = cin; // will probably wait for enter before processing begins.
int value = 0;
for (int i=0; i<9; i++) {
if (! (in >> value))
break;
cout << value << "; ";
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a program in c++ where I have to detect spacebar, how can I do? I see that I need the function getch(), but in my program I havenĀ“t conio.h. Exist other solution?
With getchar I need press intro, exist other form that I press only spacebar?
For example, can I introduce a intro without press intro???
Simple Example
#include <iostream>
using namespace std;
int main()
{
char ans;
do
{
//code
}
while(getchar() != 32 || getchar() != ' ');
cout << "Space pressed" << endl;
return 0;
}
Compiled Code
Windows.h:
if(GetAsyncKeyState(VK_SPACE) & 0x80000000)
MessageBox(NULL, "Spacebar pressed!", "TEST", MB_OK);
See no conio.h
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
what have of wrong here ? Work in my notebook, but not in my PC . . .
The two are 64-bits, Windows 7 ultimate.
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
HKEY CH;
char File_Name[] = "C:\\Users\\RMS\\Desktop\\M.txt";
if(RegCreateKey(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run",&CH) != 0){
printf("Erro - RegCreateKey\n");
system("PAUSE");
return -1;
}
if(RegOpenKey(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run",&CH) != 0) // Abre a CH "Minha CH"
{
printf("Erro - RegOpenKey\n");
system("PAUSE");
return -1;
}
if(RegSetValueEx(CH,L"My_Value",0,REG_SZ,(LPBYTE) L"C:\\Users\\RMS\\Desktop\\M.txt",40) != 0)
printf("Erro - RegSetValue\n");
RegCloseKey(CH);
printf("\nsucesso !\n");
system("PAUSE");
return 0;
}
I found. . . Was only do this:
if(RegSetValueEx(CH,L"My_Value",0,REG_SZ,(LPBYTE) L"C:\\Users\\RMS\\Desktop\\M.txt",60) != 0)
VERY THANK GUYS !!
Your problem is that the HKLM registry key is only writable by elevated programs, and your program is not running elevated. The reason it works on one machine and not the other is that one has user access control turned down/off while the other doesn't.
If you ran the program from an elevated command prompt it will work.
Additionally, you're using L"" for the strings, but using a RegSetValueEx call with 40, which is 40 bytes, and will actually cut off the M.txt on the text you're setting (if it works at all). Where you initialize the .txt file you should use:
TCHAR File_Name[] = L"C:\\Users\\RMS\\Desktop\\M.txt";
Then for the RegSetValueEx you do:
RegSetValueEx(CH,L"My_Value",0,REG_SZ,(LPBYTE) File_Name, sizeof File_Name + sizeof(TCHAR))
This makes it the number of bytes that corresponds to the filename, plus the final NULL TCHAR.
what error do you get?
try
RegOpenKeyEx
instead of RegOpenKey, since that's for 16 bit windows.