[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

sup /g/ im noob at programming at c/c++ heres my problem, i

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 38
Thread images: 4

File: 0b3b0eab.jpg (286KB, 900x1200px) Image search: [Google]
0b3b0eab.jpg
286KB, 900x1200px
sup /g/ 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
>>
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;
}
>>
>>62179546
what if it's

x[] = "kek 12345 afwaf 7478645" and i only want to do it on 12345?
>>
>>62179596
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.
>>
>>62179649
lets say im trying to convert 12345 to one int value, how would i do it?

something like

int i = x[4-8];
>>
>>62179795
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.
>>
>>62179795
use C++
>>
File: 1468494971482.jpg (368KB, 840x700px)
1468494971482.jpg
368KB, 840x700px
Why the fuck are anons doing your homework?
>>
File: anons.jpg (38KB, 500x333px)
anons.jpg
38KB, 500x333px
>>62179832
Anons help anons thats how we stay strong against the normie and jewish empires
>>
>>62179493
c or c++?
>>
>>62179855
How do you know he's not a normie? He writes like one.
>>
>>62179825
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
>>
>>62179873
he can code skill level doesn't matter normies and jews dont code
>>
>>62179855
Dude is a normie though, that's why he needs help on something this trivial.
>>
>>62179896
prove your not a normie and help him
>>
>>62179896
if its so trivial then why havnt you figured out how and help me yet
checkmate autists
>>
>>62179889
Holy fucking shit. Please tell me this is your homework. I fear for my safety.
>>
>>62179910
Because figuring shit out and solving problems is what programming is all about.
>>
>>62179917
its not
>>
>>62179889
char update[80];
int value = 999;

// (you can change value here)

sprintf(update, "UPDATE dur.hur SET count=count+%d WHERE num='1'", value);
>>
File: sad.gif (418KB, 498x280px)
sad.gif
418KB, 498x280px
sorry OP i cant code c only javascript and python other wise i would help
>>
>>62179933
Well, fuck. I can only hope your ignorance does not kill or harm anyone.
>>
>>62179955
;_;
>>
>>62179955
T_T
>>
>>62179889
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'
>>
>>62179985
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.
>>
http://xyproblem.info
>>
>>62179937
mein negro,shit works

THANK YOU ANON!
>>
>>62180001
what is sprintf
>>
>>62180018
I did give him the sprintf few posts ago.
>>
>>62179889
>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.
>>
>>62180075
He wants to increment by 150 without executing the query 150 times.
>>
>>62180106
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
>>
>>62180075
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
>>
>>62180178
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)
>>
>>62179795
Bit shifting or string to int conversion
Thread posts: 38
Thread images: 4


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.