olli
so you can see repeated code inside switch
but in this case you would need to assign every time? also, since this looks like a potential hotspot I would prefer not to use function pointers.
Sergey
but in this case you would need to assign every time? also, since this looks like a potential hotspot I would prefer not to use function pointers.
Or do you mean that i will use pointer for each iteration? another words, the current implementation is a good way?
olli
thank you
another option could be this https://paste.ubuntu.com/p/SBRvwrcGQ2/ where you basically have the loop inside the function, so you don't have to switch on every iteration.
Sergey
I see, thanks
Anonymous
#include <stdio.h> #include <math.h> int main () { int x ; float y,z ; printf("enter a number : " ); scanf("%f", &y); x = floor(y); z = y-x ; while (fmod(z,1)!= 0) { z *=10;} printf("x is : %d", x) ; printf("z is : %f", z) ; return 0; } input : 12.5 x is 12 z is 5.00000 how can i have z = 5 instead of 5.0000?
Anonymous
cast data types
Anonymous
z want to be int
Anonymous
Anonymous
yes
#include <stdio.h> #include <math.h> int main () { int x; float y,z; printf("enter a number : " ); scanf("%f", &y); x = floor(y); z = y-x ; while (fmod(z,1)!= 0) { z *=10;} printf("x is : %d", x) ; printf("z is : %d", int(z)) ; return 0; }
Roshan
As x is int type
Roshan
I think it is %i in C IDK
Anonymous
I think it is %i in C IDK
both d and i can be used for it . and I just edited int(z) or (int) z part only.
Anonymous
amir , at least say ,what do you want to do ? I didn't concern your full code .I concerned only about it's output .
Anonymous
amir , at least say ,what do you want to do ? I didn't concern your full code .I concerned only about it's output .
i want to seprate a float number into two integers example : 25.321 x is : 25 y is : 321
Roshan
i want to seprate a float number into two integers example : 25.321 x is : 25 y is : 321
Use string to store that number. If( !(isdigit(str[i])) ){ //This is where you encounter the dot }
Roshan
Then you can use the stringstream to get that part of number
Roshan
Anonymous
i get the input like string?
Mar!o
I've discovered something really weird in C++! I wanted to load a binary file into a Blob - typedef for std::vector<std::byte> in my engine
Mar!o
First I did it the modern C++ way: auto blob_from_disk(const std::filesystem::path& file) -> Blob { std::basic_ifstream<std::byte> stream(file, std::ios::in | std::ios::binary); if (!stream) { return {}; } return std::vector<std::byte>(std::istreambuf_iterator<std::byte>(stream), std::istreambuf_iterator<std::byte>()); }
Anonymous
Yes
how can i do that?
Mar!o
Loading a 24MB texture file took almost 900ms! I already was suspicious that's a way to long!
Mar!o
auto blob_from_disk(const std::filesystem::path& _file) -> Blob { const auto path = _file.string(); FILE* file; #if COM_MSVC fopen_s(&file, path.c_str(), "rb"); #else file = fopen(_file.data(), "rb"); #endif [[unlikely]] if(!file) { return {}; } fseek(file, 0, SEEK_END); const long size = ftell(file); [[unlikely]] if(!size) { return {}; } rewind(file); Blob blob = {}; blob.resize(size); const auto read = fread(blob.data(), sizeof(std::byte), size, file); blob.resize(read); return blob; } Changed the code to use the C file library
Mar!o
And it went from 900ms to 80ms! The C std version was more than 10x faster!
Mar!o
This is incredible dangerous! I thought that the modern C++ version is smart enought to reserve the vector and to read the file in one chunk. Seems it is not.
Roshan
how can i do that?
Get the input in str (string). Use for loop and apply the logic I gave to encounter the dot. Then you get the other part of the dot.
Низами
Hello. The question is related to (fstream). I am trying to write (string = "Hello") to (fstream). And I do it. But at the end of my text (HHHHHH .....) appears. I Use (Windows form). I do not understand what is the matter and what I am doing wrong. Google didn't help me much (((
Junaid
Hey every one can some one please share some source to learn about basics of stacks in c language
Igor🇺🇦
Igor🇺🇦
yes
Yes what? 🤷‍♂️
Junaid
Yes what? 🤷‍♂️
I want to know its working when push will happens and when pull
Igor🇺🇦
I want to know its working when push will happens and when pull
In memory or stack as data structure. These are different things
Junaid
In memory (Memory layout of C programming)
Igor🇺🇦
In memory (Memory layout of C programming)
https://gribblelab.org/CBootCamp/7_Memory_Stack_vs_Heap.html
Igor🇺🇦
Wikipedia as always has good enough explanation with links to more in-depth material https://en.wikipedia.org/wiki/Call_stack
Anonymous
hey guys
Anonymous
i need help
Anonymous
if(num = 0 && num < 0) how can i make it on switch
Phil
@alfonsephp you must use a proposition OR or XOR to make it work
Phil
and also there is a wrong check equality test
Anonymous
k thx
Phil
you add a single = and that affect to num the value num
Phil
what you wrote is : if the affectation of zero to num is okay and if num is strictly under 0 then
beluga_hz
Good afternoon guys. Could you explain me in a practical way how to add C / C ++ libraries on Linux? I searched on Google and unfortunately I didn't understand much ... For example: if I want to use conio.h in a C ++ program, how should I add this library to the system so that the compiler can find it?
beluga_hz
You don't want to use conio.h in C++
Actually I want, just for the "getch()" function
beluga_hz
But conio was an example
Anonymous
But conio was an example
It's a bad example
beluga_hz
It's a bad example
Not to me, thanks for letting me know tho
أسامة
Many students think getch and system("pause") as part of C/C++
Alex
How? sudo apt install lib_name?
sudo apt-get install <package name>
beluga_hz
Then i have to install the whole package, not just the single library