I have a C++ string which contains some characters. How can I change the char colour if I meet certain chars? Below is the sample code:
#include <iostream>
#include "windows.h"
using namespace std;
int main()
{
HANDLE h;
h = GetStdHandle(STD_OUTPUT_HANDLE);
string str = "my name is meow.";
for(int i=0; i<str.length(); i++)
{
if(str[i] == 'm')
{
//change the char 'm' to red color..
}
cout<<str[i];
}
return 0;
}
if(str[i] == 'm')
{
SetConsoleTextAttribute(h, FOREGROUND_RED);
cout<<str[i];
}
else
{
SetConsoleTextAttribute(h, 15);
cout<<str[i];
}
maybe this is what you wanna do?
Related
I want to pause my program on the 0x32, 0x33 and 0x34 key and make it work again on the 0x31 key, how can I? I used this code to pause on the 0x32 key, it's working, but I can't get it back to work on the desired key
To summarize what #user4581301 suggested:
#include <conio.h>
...
if (GetAsyncKeyState(0x32) || GetAsyncKeyState(0x33) || GetAsyncKeyState(0x34)) {
while (_getch() != 0x31)
;
}
If your application is a console application, I implemented the function you want through loop. If it's a desktop application, you can refer to my code logic.
#include <iostream>
#include<cstdlib>
#include<time.h>
#include<Windows.h>
#include<conio.h>
using namespace std;
int main()
{
while (true) {
for (int i = 0; i < 5; ++i) {
if (GetAsyncKeyState(gun_keys[i]) && (gun != guns[i])) {
gun = guns[i];
system("cls");
gun_delay = GetTime(gun->rpm);
gun_index = 0;
break;
}
}
if (GetAsyncKeyState(VK_DELETE)) //Bind key, what close this program
{
ExitProcess(-1); //Exit Process
}
if (GetAsyncKeyState(MOUSEEVENTF_MOVE) < 0) {
if (!is_mouse_down) {
is_mouse_down = true;
if (gun != nullptr)
gun_index = 0;
}
if (gun != nullptr && gun_index != gun->len) {
mouse_event(MOUSEEVENTF_MOVE, long(gun->pattner[gun_index][0] * K), long(gun->pattner[gun_index][1] * K), 0, 0);
++gun_index;
Sleep(gun_delay);
continue;
}
}
else
is_mouse_down = false;
if (_kbhit())//Checks if there is currently keyboard input and returns a non-zero value if there is, or 0 otherwise
{
int ch = _getch();
if (ch == 0x32 || ch == 0x33 || ch == 0x34)
{
ch = _getch();//It waits for input and pauses the program
}
if (ch != 0x31)
{
while (true)
{
if (_kbhit())
{
int ch = _getch();
if (ch == 0x31) break;
}
}
}
fflush(stdin);//Clear the input buffer
}
Sleep(150);
}
return 0;
}
I am making a simple game using C++
It's just a tile game with an ASCII map.
The game itself works fine, but the console screen(map) is flickering when I move my player and I don't know how to fix this. Any help appreaciated, thanks!
Code:
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <ctime>
#include <vector>
#include <string>
#include <cstdlib>
#include <fstream>
using namespace std;
vector<string> map;
int playerX = 10;
int playerY = 10;
int oldPlayerX;
int oldPlayerY;
bool done = false;
void loadMap();
void printMap();
void setPosition(int y, int x);
void eventHandling();
int main()
{
loadMap();
map[playerY][playerX] = '#';
printMap();
while(!done){
eventHandling();
printMap();
}
exit(1);
return 0;
}
void eventHandling(){
char command;
command = _getch();
system("cls");
oldPlayerX = playerX;
oldPlayerY = playerY;
if(command == 'w'){
playerY--;
}else if(command == 'a'){
playerX--;
}else if(command == 'd'){
playerX++;
}else if(command == 's'){
playerY++;
}
if(map[playerY][playerX] == '#'){
playerX = oldPlayerX;
playerY = oldPlayerY;
}
setPosition(playerY,playerX);
}
void setPosition(int y, int x){
map[oldPlayerY][oldPlayerX] = '.';
map[y][x] = '#';
}
void printMap(){
for(int i = 0 ; i < map.size() ; i++){
cout << map[i] << endl;
}
}
void loadMap(){
ifstream file;
file.open("level.txt");
string line;
while(getline(file, line)){
map.push_back(line);
}
}
std::cout is not intended to be used that way.
You should refer to the system specific API for the target OS and environment. For example, for Windows you should use Console API functions for your purpose. These functions are defined in Wincon.h include file.
It also helps if you use a double buffering system such that only what needs to be overwritten every frame is changed. IO operations are extremely expensive so ought to be minimized.
Cameron Gives a Very Thorough Description of How to Do this Here
But in essence, you'd use two arrays, one containing the current state of the map, one containing the previous state and only write to the specific locations that have changed.
One method to clear the screen that works on many systems is to print the form feed character, \f. The Linux console supports this, and so did MS-DOS if you loaded ansi.sys. Unix has ncurses and terminfo to abstract these functions.
Here is what I get when I try to encrypt "This is a test sentence!": "Ymnxp���p�����t�������"
I have tested my encryption part before and it worked fine.
Can anyone please tell me what did I do wrong here?
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include "unistd.h"
using namespace std;
void displayUsage(){
// Displays how to use this program
// TODO
cout << "Instruction: \n use -r to give rotation number \n use -f to give file name" <<endl;
}
int main(int argc, char **argv){
string text;
char* input_file_name;
int rotation;
bool have_rotation = false;
bool have_input_file_name = false;
// process command-line arguement
int opt = 0;
extern char *optarg;
static const char* opt_string = "r:f:";
opt = getopt( argc, argv, opt_string);
while(opt != -1) // While there are parameters to parse, do so
{
switch(opt)
{
case 'r':
have_rotation = true;
rotation = atoi(optarg);
break;
case 'f':
have_input_file_name = true;
input_file_name = optarg;
break;
default:
displayUsage();
return 1;
}
opt = getopt( argc, argv, opt_string); // Pull the next parameter, or 0 if none.
}
if(!have_rotation)
{
displayUsage();
return 0;
}
if(have_rotation)
{
if(have_input_file_name)
{
ifstream file(input_file_name);
string text2, temp;
while(!file.eof())
{
getline(file, temp);
text2 += temp;
text2 += "\n";
}
text = text2[text2.size()-2];
}
else
{
cout <<"Enter text:"<<endl;
cin >> text;
}
}
char cipher[text.size()];
for(int i=0; i<text.size(); i++)
{
cipher[i] = text[i];
if(islower(cipher[i]))
{
cipher[i] = (cipher[i] - 'a' + rotation)%26 + 'a';
}
else if(isupper(cipher[i]))
{
cipher[i] = (cipher[i] - 'A' + rotation)%26 + 'A';
}
}
cout <<cipher<<endl;
return 0;
}
I guess the error is because you did not terminate your cipher array with a '\0'.
The printing function will process characters from an array (and possibly beyond) until it finds a '\0' character.
Your array should be one bigger to account for this terminating character.
Or get rid of the char array and use std::string.
I can get my code run if I type the sentence manually. It doesn't printout anything if I input a text file.
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include "unistd.h"
using namespace std;
void displayUsage(){
// Displays how to use this program
// TODO
cout << "Instruction: \n use -r to give rotation number \n use -f to give file name" <<endl;
}
char caesar(char c, int r)
{
if(isalpha(c))
{
if(islower(c))
{
c = (((c-97)+r)%26)+97; // 97 is a value of 'a'
}
else if(isupper(c))
{
c = (((c-65)+r)%26)+65; // 65 is a value of 'A'
}
}
return c;
}
int main(int argc, char **argv){
string text;
char* input_file_name;
int rotation;
bool have_rotation = false;
bool have_input_file_name = false;
// process command-line arguement
int opt = 0;
extern char *optarg;
static const char* opt_string = "r:f:";
opt = getopt( argc, argv, opt_string);
while(opt != -1) // While there are parameters to parse, do so
{
switch(opt)
{
case 'r':
have_rotation = true;
rotation = atoi(optarg);
break;
case 'f':
have_input_file_name = true;
input_file_name = optarg;
break;
default:
displayUsage();
return 1;
}
opt = getopt( argc, argv, opt_string); // Pull the next parameter, or 0 if none.
}
if(!have_rotation)
{
displayUsage();
return 0;
}
if(have_rotation)
{
if(have_input_file_name)
{
ifstream file(input_file_name);
string text2, temp;
while(!file.eof())
{
getline(file, temp);
text2 += temp + "\n";
}
text = text2[text2.size()-2];
}
else
{
cout <<"Enter text:"<<endl;
getline(cin, text);
}
}
string output = "";
for(int i = 0; i < text.size(); i++)
{
output += caesar(text[i],rotation);
}
cout<<output<<endl;
return 0;
}
I wrote a getSample(devicePath) function that reads raw input events from keyboard, however after running this function, ncurses doesn't display anything.
includes
#include <linux/input.h>
#include <unistd.h>
#include <fcntl.h>
#include <ncurses.h>
#include <vector>
#include <string>
getSample
const int KEY_PRESSED = 1;
const int KEY_RELEASED = 0;
std::vector<input_event> getSample(std::string devicePath) {
std::vector<input_event> events;
int fileDescriptor = open(devicePath.c_str(), O_RDONLY);
while (true) {
struct input_event event;
read(fileDescriptor, &event, sizeof(struct input_event));
if (event.type == EV_KEY)
if (event.value == KEY_PRESSED || event.value == KEY_RELEASED) {
if (event.code == 28 && event.value == KEY_PRESSED)
break;
events.push_back(event);
}
}
close(fileDescriptor);
return events;
}
main
int main() {
std::string devicePath = "/dev/input/by-id/"
"usb-Microsft_Microsoft_Wireless_Desktop_Receiver_3."
"1-event-kbd";
std::vector<input_event> sample = getSample(devicePath);
initscr();
printw("Hello World!");
refresh();
getch();
endwin();
return 0;
}
Is there a way to fix it?
Tip
Function getSample works correctly, because when I replace ncurses code with simple printf(), everything works fine
int main() {
std::string devicePath = "/dev/input/by-id/"
"usb-Microsft_Microsoft_Wireless_Desktop_Receiver_3."
"1-event-kbd";
std::vector<input_event> sample = getSample(devicePath);
for (unsigned int i = 0; i < sample.size(); i++)
printf("%d | %3d | %ld.%06ld\n", sample[i].value, sample[i].code,
(long)sample[i].time.tv_sec, (long)sample[i].time.tv_usec);
return 0;
}
I am having trouble with getting input after a modifier in the text of a .txt file.
What I want to do is if the .txt file has the word "type:" then anything after that will be put into a char.
My code so far:
#include "stdafx.h"
#include <windows.h>
#include "VKH.h"
#include "Strmif.h"
#include <iostream>
#include <stdio.h>
#include <string>
#include <fstream>
void GetDocumentandRead() {
string line;
ifstream myfile (line1);
if (myfile.is_open())
{
while ( !myfile.eof() )
{
getline (myfile,line);
char aline[100];
strcpy(aline, line.c_str());
printf(aline, "\n");
if (line.compare("mouseup") == 0){
MouseUp(10);
}
if (line.compare("mousedown") == 0){
MouseDown(10);
}
if (line.compare("mouseright") == 0){
MouseRight(10);
}
if (line.compare("mouseleft") == 0){
MouseLeft(10);
}
if (line.compare("mouseclick") == 0){
MouseClick();
}
if (line.compare("enter") == 0){
Enter();
}
if (line.compare("ctrltab") == 0){
CtrlTab();
}
if (line.compare("tab") == 0){
Tab();
}
if (line.compare("altf4") == 0){
AltF4(0);
}
if (line.compare("alttab") == 0){
AltTab();
}
if (line.compare("mousecenter") == 0){
MouseCenter();
}
if (line.compare(6,5,"type:") == 0){
//Don't know what to put here...
}
}
myfile.close();
}
else printf("\nUnable to open file\n\n");
}
So after the "type:" in a text file it would type that using a function I have called TypeStr();
void TypeStr(char *lpszString)
{
char cChar;
while((cChar=*lpszString++)) // loops through chars
{
short vk=VkKeyScan(cChar); // keycode of char
if((vk>>8)&1){keybd_event(VK_LSHIFT,0,0,0);} // hold shift if necessary
keybd_event((unsigned char)vk,0,0,0); // key in
keybd_event((unsigned char)vk,0,KEYEVENTF_KEYUP,0); // key out
if((vk>>8)&1){keybd_event(VK_LSHIFT,0,KEYEVENTF_KEYUP,0);} // release shift if necessary
}
}
Any help would be appreciated. Thanks!
First you should rewrite your TypeStr function so that it takes a const char *. Like this
void TypeStr(const char *lpszString)
{
...
}
no other changes needed.
Then you should call that function from your code like this
if (line.compare(6,5,"type:") == 0){
TypeStr(line.c_str() + 11);
}
The reason that you have to change your TypeStr function to const char* (apart from it being general good style) is that the c_str() method of std::string returns a const char* not a char*.