Is there a way to convert a string that the user inputs into an int * in C++?
int * add;
string input;
getline(cin, input);
add = stoi(input);
You need to convert a string to int and then take its address:
string input;
getline(cin, input);
int add = stoi(input);
int* ptr_to_add = &add;
Related
How can I get constant values from a message and show these values in a result (histogram)?
My message structure is:
message msgCLAS
{
int Singclas = 2206;
int Singlial = 3009;
int Verifclas = 5403;
int Veriflial = 6406;
int Aggreclas = 2;
int Aggrelial = 2;
int Verifagclas = 1833;
int Verifaglial = 1857;
}
I have been desperately stuck on this question on Kattis here. The basic idea is that you are given a general formula for a valid two variable recursive function and then given that formula you are to evaluate the function of this function at various inputs. I have trawled through this question for over an hour and I was wondering if anyone could point me in the right direction (or give a counter-example) of why my answer is wrong. The code I used which got me a Wrong Answer verdict is shown below.
#include<bits/stdc++.h>
using namespace std;
long long dp[105][105];
int a[25];
int b[25];
long long rec(int x_in, int y_in, int coeffs, long long c) {
if(dp[x_in][y_in]!=-1){
return dp[x_in][y_in];
}
else {
long long ans = c;
for(int i=0; i<coeffs; ++i) {
int x_cell = max(0, x_in-a[i]);
int y_cell = max(0, y_in-b[i]);
ans+=rec(x_cell, y_cell, coeffs, c);
}
dp[x_in][y_in] = ans;
return dp[x_in][y_in];
}
}
int main() {
int n;
scanf("%d", &n);
string ex;
getline(cin, ex);
for(int caseno=0; caseno<n; ++caseno) {
memset(dp, -1, sizeof(dp));
if(caseno>0) printf("\n");
string coeffs_list;
getline(cin, coeffs_list);
int pairs = 0;
long long c, d;
char * dup = strdup(coeffs_list.c_str());
char * token = strtok(dup, " ") ;
while(token != NULL){
a[pairs] = atoi(token);
token = strtok(NULL, " ") ;
b[pairs] = atoi(token);
token = strtok(NULL, " ") ;
pairs++;
}
free(dup);
c= (long long) a[pairs-1];
d = (long long) b[pairs-1];
pairs--;
dp[0][0] = d;
for(int i=1; i<105; ++i) {dp[0][i] = d; dp[i][0]=d;}
string inputs;
getline(cin, inputs);
int x_i, y_i;
char * dup2 = strdup(inputs.c_str());
char * token2 = strtok(dup2, " ") ;
while(token2 != NULL){
x_i = atoi(token2);
token2 = strtok(NULL, " ") ;
y_i = atoi(token2);
token2 = strtok(NULL, " ") ;
printf("%lld\n", rec(x_i, y_i, pairs, c));
}
free(dup2);
}
}
As you can see the basic idea of what i did was to construct a dp table and evaluate the function accordingly. Thank you in advance for anyone that could help me over here.
The function can grow very fast, thus overflow a 64bit integer (you can check this by using assert(rec(...) >= 0) (not guaranteed but likely to fail for an overflow)). Use a custom BigInteger implementation or switch to Java/Python.
e.g. for n=20, a_i=1, b_i=0: f(x,y)=20*f(x-1,y)+c = O(20^x*(c+d))
This is also indicated by most solutions under stats using Java: https://open.kattis.com/problems/generalizedrecursivefunctions/statistics https://baylor.kattis.com/problems/generalizedrecursivefunctions/statistics
I got a problem regarding operators.
class Fraction{
public:
Bruch(int z = 0, int n = 1);
void operator*(Fraction &b);
void calculate(char operation, Fraction &b);
private:
}
void Fraction::calculate(char operation, Fraction &b) {
switch(operation) {
case '*': operator*(b); break;
}
Fraction::Fraction(int z, int n){
zaehler = z;
nenner = n;
}
void Fraction::operator*(Fraction &b){
zaehler = zaehler * b.zaehler;
nenner = nenner * b.nenner;
}
int main(){
Fraction a(7, 5);
lont int b = 3.0;
Fraction d = a*b;
}
I want to be able to use the operator* not only with two members of class Fraction but also with one of type long int.
Error: "no known conversion for argument 2 from ‘long int’ to ‘Fraction&’"
Anybody some advice?
I think the argument types are wrong. Also can you check whether the sort function will work properly or not?? I have written this code to make the largest no possible by combining the integers in vector A.
string Solution::largestNumber(const vector<int> &A) {
int i,n;
vector<string> B;
string str;
for(i=0; i<A.size(); i++)
{
n=sprintf(B[i],"%d",A[i]);
}
sort(A.begin(), A.end(),[](string lhs, string rhs){
//[](const string &lhs, const string &rhs) {
// reverse the order of comparison to sort in descending order,
// otherwise we'll get the "big" numbers at the end of the vector
return rhs+lhs < lhs+rhs;
});
for(i=0; i<A.size(); i++)
{
str+= to_string(A[i]);
}
return str;
}
B is a vector of std::string.
B[I] is then a std::string
sprintf expects an array of char, not a std::string.
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
HANDLE hPort = CreateFile("COM2",
GENERIC_WRITE|GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
DCB dcb;
bool writebyte(char*data)
{
DWORD byteswritten;
if (!GetCommState(hPort,&dcb))
{
printf("\nSerial port can't be open\n");
return false;
}
dcb.BaudRate = CBR_9600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
if (!SetCommState(hPort,&dcb))
return false;
bool retVal = WriteFile(hPort,data,1,&byteswritten,NULL);
return retVal;
}
int ReadByte()
{
int Val;
BYTE Byte;
DWORD dwBytesTransferred;
DWORD dwCommModemStatus;
if (!GetCommState(hPort,&dcb))
return 0;
SetCommMask(hPort,EV_RXCHAR | EV_ERR);
WaitCommEvent (hPort,&dwCommModemStatus,0);
if (dwCommModemStatus & EV_RXCHAR)
ReadFile (hPort,&Byte,1,&dwBytesTransferred,0);
Val = Byte;
return Val;
}
int main() {
POINT p;
int x;
int y;
int z;
while(0==0){
GetCursorPos(&p);
x = p.x;
y = p.y;
HDC hDC;
hDC = GetDC(NULL);
cin >> z;
cout << GetPixel(hDC, x, y) << endl;
Sleep(z);
ReleaseDC(NULL, hDC);
char data = GetPixel(hDC, x, y);
if (writebyte(&data))
cout <<" DATA SENT.. " << (int)data<< "\n";
}
}
in the part of sending data through serial communication, instead of sending the data as GetPixel(hDC, x, y), it only sends the value "-1" . I was thinking it is because char is only for small integers and the output I was giving is a very very long number. I tried to change it to long int but i still get the same result. That it only sends "-1". I thought that the solution might be converting char to long int or long int to char before sending the data but I don't know how..can someone help me?
Why do you use hDC after releasing it?
ReleaseDC(NULL, hDC);
char data = GetPixel(hDC, x, y);
GetPixel will return -1 (CLR_INVALID) in case of an error (see MSDN).
And, by the way, a COLORREF is not a char, so you lose Information when storing the return value of GetPixel in char data. You should store the complete COLORREF and send/receive all of it's bytes (so send/receive sizeof(COLORREF) bytes).
Also be aware of byte order. If you are transferring multi byte data between two machines then you must assure that both agree on the order of the bytes. If for example one machine is little endian and the other big endian, then they store COLORREF with different byte order in memory. One is storing the COLORREF 0x00BBGGRR in memory as { 0, 0xbb, 0xgg, 0xrr } whereas the other is storing it as { 0xrr, 0xgg, 0xbb, 0 }. So you need to define a transmit byte order which both sides use independant of their host byte order. If you don't want to invent the wheel new, you can take a look at network byte order and reuse that. Socket API gives you some functions like ntohl and htonl which help you in converting from host byte order to network byte order and vice versa.