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 2 years ago.
Improve this question
I need your help for C ++, I need to use a GetKeyState with a string
Example (does not work) :
string test = "0x47" // G Key for examble(https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes)
GetKeyState(test)
{
cout << "Nice << endl;
}
I would just like to know how to use the GetKeyState function with a string instead of the key (GetKeyState (string) instead of: GetKeyState (0x47))
GetKeyState() takes an int as input, not a string. So you MUST parse the string to convert it into an int, there is no other option.
There are many ways to do that parsing in C++, eg:
std::stoi():
#include <string>
std::string test = "0x47";
int value = std::stoi(test, nullptr, 0/*or 16*/);
GetKeyState(value)
std::sscanf():
#include <string>
#include <cstdio>
std::string test = "0x47";
int value = 0;
std::sscanf(test.c_str(), "%x", &value);
GetKeyState(value)
std::istringstream:
#include <string>
#include <sstream>
#include <iomanip>
std::string test = "0x47";
int value = 0;
std::istringstream(test) >> std::hex >> value;
GetKeyState(value)
Just to name a few.
Related
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 5 years ago.
Improve this question
i want to convert a sentence like 'good boy' to ascii code . i know the code that is a loop and print the ascii code of each character of sentence but i don't want this. i want that the ascii code of sentence (all characters alltogether) in long for example 1259788712..
You can use string to handle it.
#include <iostream>
#include <sstream> // use stringstream
using namespace std;
// turn int into string
string IntTOstring(int);
int main(void)
{
string sIn,sOut;
// input
sIn = "good boy";
sOut="";
for (int i=0 ; i<sIn.length() ; i++ ) {
// get one char from sIn each time
int temp=sIn.c_str()[i];
// turn int into string & save in sOut
sOut += IntTOstring(temp);
}
cout << sOut << endl;
return 0;
}
// use stringstream to convert int to string
string IntTOstring(int i){
stringstream ss;
ss << i;
return ss.str();
}
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 7 years ago.
Improve this question
i am trying to write a small automated program to calculate some values for me and output some text to a simple .txt file. do the redirection symbols < > & << >> work the same in C++ as they do in the command line for batch scripts? When i try to search how to redirect to a .txt file in C++. All of the examples, and tutorials i have found are presented in a manner that assumes IO on the console like the following.
cout::<<"show this text on the console";
cin::>> whatever you would call here to accept user input.
what i want to know is will it work to do it this way?
#include <string>
using namespace std;
int main()
{
int X = 0;
string zero = "touchPress 0 483 652\n";
if {
(X=0)
zero>>C:\test.txt;
x+5;
} return 0;
}
Your code does not work. I am not absolutely sure what is the desired behaviour, but this code writes the string zero to a file:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
int X = 0;
string zero = "touchPress 0 483 652\n";
ofstream myFile("C:\\Data\\test.txt");
//a condition which is always true
if (X==0)
{
myFile<<zero;
X + 5; //this is valid but useless
}
return 0;
}
#include <fstream>
int main(){
string zero = "touchPress 0 483 652\n";
std::ofstream fout("test.txt"); // creates new test.txt in folder where .exe is
fout << zero; //same as cout << zero;//but in the file
return 0;
}
fout as cout, i just reworked your barely alive program. is this what you wanted?
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm having the problem with launching encrypting program. For single words strings it works well (for example, abcd), but when i type two or three or more words (a sentence, for example, abcd ab ac) it doesn't ask for a key, but rewrites the sentence I typed. What am I doing wrong? Thanks in advance. The code:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <conio.h>
#include <string>
#include <stdlib.h>
#include <stdio.h>
int main()
{
int key, l;
char choose;
string message;
cout<<"Type the message"<<endl;
cin>>message;
cout<<"Give me a key from 0 to 26"<<endl;
cin>>key;
for (int i=0,l=message.size(); i<=l; i++)
{
if (isalpha(message[i]))
{
if (isupper(message[i]))
{
cout<<(char)('A'+(message[i]-'A'+key)%26);
}
if (islower(message[i]))
{
cout<<(char)('a'+(message[i]-'a'+key)%26);
}
}
else
{
cout<<message[i];
}
}
return 0;
}
Use
std::getline(std::cin, message);
cin does only read until the next whitespace, the rest of the input is being kept in the stream buffer so that you get the next word of your sentence entered in the first step as input for your second cin.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I am trying to read a file with full path and get each line and put them into an array. my code is like this:
#include <fstream>
#include <iostream>
using namespace std;
void main(){
int Log[200];
int i;
For(int i=0; i<30; i++)
{
getline(/var/asl/data/audit/20130502/20130502-0611/20130502-61157-UYHEZX8AAAEAAAbKRvKAAAAC, line);
Log[i] = line;
cout << Log[i] < "\n";
}
}
but the below errors come to me and I do not how to solve them. Can anyone help me?
log1.cpp:7: error: :main must return int
log1.cpp: In function int main():
log1.cpp:12: error: expected primary-expression before int
log1.cpp:12: error: expected before token
Another question I have is that if I want to search a special character that is in the line that stored in arrays,(I mean search in an array) what can I do?
Thanks a lot dear users for your reply. I tried the code and it does not have any errors. But when i run it nothing happen. My file is not in text format. it is as like as Apache server logs format. Should it be in text format? The other question is if i put these line in arrays can i search a special value in it?
Thanks for your reply in advance.
Salam ,Try this:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main(){
string line;
ifstream myfile ("example.txt"); //file address
string Log[200];
int i=0;
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
Log[i] = line;
i++;
cout << line << endl;
}
myfile.close();
}
return 0;
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
#include<fstream>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
ifstream infile;
ofstream outfile;
infile.open("oldfile.txt");
outfile.open("newfile.txt");
while(infile){
string str,nstr;
infile>>str;
char charr[10];
charr[0]='<';charr[1]='\0';
nstr=str.substr(0,str.find_first_of(charr));
outfile<<nstr<<' ';
}
}
this program uses substr(0, string.find_first-of(charcter array which is starting point to be substring))each word's unnecessary sub strings but it doesn't preserve the line number when writing to another file. can you fix it . it writes the file word by word sequencially. the code didn't preserve line by line,
string input doesn't care about line boundaries, it treats \n,\t,\v and probably others the same as space.
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string line,word;
char foo[] = "<";
while ( getline(cin,line) ) {
string newline;
for ( istringstream words(line)
; words >> word ; ) {
newline+=word.substr(0,word.find_first_of(foo))+' ';
}
cout << newline << '\n';
}
}
Change
outfile<<nstr<<' ';
to
outfile<<nstr<<endl;
This will write line by line instead of sperating with a single whitespace character.