Suleyman
I have a function that reverses strings. I read some string arrays from a file with fgets function. When I wrote a string, it can reverse it very well. But when I used it in buffer that read from file, it adds a new line. How can I prevent it to print newline.
Anonymous
char* reverse_word(char *arr){ int i,c,k,length,temp; length=strlen(arr)-1; k=length; for (i=0; i<length/2 ;i++){ temp =arr[k]; arr[k]= arr[i]; arr[i]= temp; k--; if(k == (length / 2)) break; } return arr; }
1) this way of declaring variables is terrible. Declare variables as you need them and initialize right away. Do not declare all the variables in first line of function — this looks bad and it's error-prone
Robert
K
Robert
K
Nils
Kk
Anonymous
Anonymous
/warn screen photo with no reason
Anonymous
its notes
Anonymous
Anonymous
Do not send screen photos anymore
Anonymous
ok sorry
Anonymous
im writing notes on C currently as a reference. It shall help me alot. i could also use zeal.
Anonymous
after that i will start writing them for C++ :D
Anonymous
printf is not defined in stdio.h printf is declared in stdio.h (or some other file included by stdio.h)
Anonymous
ah ok
Nameful
worth noting is that return 0 means everything went well
Nameful
if you are returning from main because something went wrong you should return something else
Anonymous
alrighty
PReddy
/start@MissRose_bot
Anonymous
Hii
LadyNone
Nörüyonuz be ya 😁
Anonymous
any body plzz help me
Anonymous
while (scanf("%f", &num) < 1) { scanf("%*[^\n]"); ... }
Would you be so kind to break down every line and explain it to me, please?
Dima
Bruh
Anonymous
OwO
Nils
Hi, what is wrong with my code? How do I fix it? https://hastebin.com/kehayacumu.cpp Skipping . and .. doesn't work :-/
Nils
What did I do wrong?
Nils
I exactly described what is faulty in my code and just asked if anyone has an idea how I do fix this
Nils
This code is not enough to tell what's the problem
I want to display a list of all files in that directory skipping . and ..
Anonymous
We don't know types of the values you are using
Anonymous
What compiler do you use?
Nils
struct dirent *dp; DIR *dfd; char filename_qfd[264]; And I use gcc
Anonymous
Try using -Wall --pedantic flags It can say some errors
Nils
Try using -Wall --pedantic flags It can say some errors
warning: comparison with string literal results in unspecified behavior [-Waddress]
Anonymous
Now fix it
Nils
How would I fix it?
Nils
just char *filename = dp->d_name?
Nils
and then compare with filename?
Anonymous
What C++ standard do you use?
Anonymous
You cannot compare C-strings with == Either use strcmp or C++17 with std::string_view The last one is preferred way
Nils
strcm aah. But how would I use std::string_view? Oh wait before I ask i sahould use ddg
Dont
Would you please clarify why does the infinite loop happen and how adding that line solves the problem? im starting with C
The loop happens because temp equals 0 each time you enter a non numerical character. The condition always true, the loop doesn't stop. The second issue is with scanf, it takes automaticaly the new line character as parameter without asking the user to enter something. The expression while(getchar() != '\n') continue; make sure that scanf doesn't take \n as input and therefor wait for the user to enter something. I hope my explanation was clear enough. Good luck
Anonymous
strcm aah. But how would I use std::string_view? Oh wait before I ask i sahould use ddg
using namespace std::string_literal; ... "String Literal"sv == "Other String"sv
Nils
Can I use "String Literal"std::string_view::sv == "Other String"std::string_view::sv too?
Nils
I don't like changing the namespace
Nils
Ok I'll try
Anonymous
I would change it locally, in the function
Anonymous
And google for how to use std::string_view and what are user defined literals, @tuxifan
Nils
Look closely I changed namespace
hmm isn't that critical in case of libs?
Nils
I mean in headers
Anonymous
hmm isn't that critical in case of libs?
If you're using namespace not globally it's totally fine
Nils
Can I store functions in a map?
Anonymous
Nils
as value
Anonymous
as value
You can https://t.me/programminginc/268947 Read this discussion
Nils
My problem is that I have no ideas about labda xD
Nils
Can't I simply do: map[key] = functionname;?
Anonymous
Use function pointers
Nils
Ah, great. thx
Nils
Use function pointers
So *functionname?
Nils
Or &?
Anonymous
Google how to use function pointers :)
Nils
Okay.
Nils
Btw, *&variable == variable?
Nils
I have seen *&variable somewhere