Sup Sup Forums im noob at programming at c/c++

sup Sup Forums im noob at programming at c/c++

heres my problem, i have
char x[] = "kek 12345";

is it possible to perform mathematical operation on the whole value of 12345?

pic unrelated

Other urls found in this thread:

xyproblem.info
twitter.com/SFWRedditImages

I'd sscanf that shit

int main ()
{
int i;
char x[] = "kek 12345";
i = atoi (x);
printf ("The value entered is %d. Its double is %d.\n",i,i*2);
return 0;
}

what if it's

x[] = "kek 12345 afwaf 7478645" and i only want to do it on 12345?

maybe a loop that goes through the array at each position and when a number is found add it to a new array, with a stopping condition when the end of the first number is met.

lets say im trying to convert 12345 to one int value, how would i do it?

something like

int i = x[4-8];

4 - 8 = -4

be clear with what you want to do
do you want to get the number 12345 out of string to integer?
do you want to make an operation and return it back to string?
some example would be useful

regex is always an easy tool [+-]?[[:digit:]]+
for string to number maybe avoid atoi(), it does not have an error handling, strtol() is more verbose but can check ranges etc.

use C++

Why the fuck are anons doing your homework?

Anons help anons thats how we stay strong against the normie and jewish empires

c or c++?

How do you know he's not a normie? He writes like one.

alright heres my actual code


char update[] = "UPDATE dur.hur SET count=count+00001 WHERE num='1'";

int switchpressed = 0; //increases each time switch is pressed
for (switchpressed; switchpressed>0; switchpressed-- ) {
//increase the 00001 by one
}


now the +00001 ([46] to [50]) needs to be changeable

he can code skill level doesn't matter normies and jews dont code

Dude is a normie though, that's why he needs help on something this trivial.

prove your not a normie and help him

if its so trivial then why havnt you figured out how and help me yet
checkmate autists

Holy fucking shit. Please tell me this is your homework. I fear for my safety.

Because figuring shit out and solving problems is what programming is all about.

its not

char update[80];
int value = 999;

// (you can change value here)

sprintf(update, "UPDATE dur.hur SET count=count+%d WHERE num='1'", value);

sorry OP i cant code c only javascript and python other wise i would help

Well, fuck. I can only hope your ignorance does not kill or harm anyone.

;_;

T_T

can't you have the char update[] as format string and 00001 as integer?

char updatefmt[] = "UPDATE dur.hur SET count=count+%05d WHERE num='1'";
int press = 0;

printf(updatefmt, press);
printf("\n");
press++; press++;
printf(updatefmt, press);
printf("\n");

prints
UPDATE dur.hur SET count=count+00000 WHERE num='1'
UPDATE dur.hur SET count=count+00002 WHERE num='1'

He's clearly going to want to have a modified string in memory to send it to DB, so printing it to stdout would not be very helpful.

xyproblem.info

mein negro,shit works

THANK YOU user!

what is sprintf

I did give him the sprintf few posts ago.

>SET count=count + 00001
What are you doing?
Doesn't this turn into an increment just by sending the update?
Changing the number will alter the behavior.

He wants to increment by 150 without executing the query 150 times.

Okay.
Didn't seem like he was aware.
He mentioned a switchpress that would alter the value.
Switch presses are done 1-2 times per second if any.
It doesn't seem like the database would be a bottleneck

need to update count in DB with switchpressed

machine can only update the db once every 20 secs or so, but switchpressed will keep going up
it can't update the DB everytime switch is pressed, so i have to store the value of switchpressed until it can update DB again

if you have better way to do please teach

It is fine to do this then.
As you stated the original question, it seemed like you had a string and had to replace the value inside the string.
What you have now is a string where you need to insert a value.
This is much more doable (even in C)

Bit shifting or string to int conversion