M.
Could anyone suggest a good title to learn how works SDL library, not SDL2 I meant
M.
Hi
klimi
Could anyone suggest a good title to learn how works SDL library, not SDL2 I meant
Why do you want just SDL? I think that SDL2 is better in every case
Vlad
Really
M.
Use SDL2
But could i use it for C programming, and not just C++?
Vlad
If you want to render on the CPU via surfaces you can do that in SDL2 as well
klimi
Could anyone suggest a good title to learn how works SDL library, not SDL2 I meant
If you really need the old legacy SDL then http://lazyfoo.net/SDL_tutorials/
M.
Ha my bad, okay thanks you
Anonymous
Ha my bad, okay thanks you
@Elkira01 Deleted your old messages by mistake while clearing some other spam. Apologies.
Anonymous
Hey,,,someone to help me,,,,, int n; cout << "Enter an integer: "; cin >> n; if (n < 10) cout << "less than 10" << endl; else if (n > 5) cout << "greater than 5" << endl; else cout << "not interesting" << endl; what values of n would cause the output to be "not interesting"
Anonymous
this your homework?
Yeah,,i tried a lot but still don't get the values
Nameful
try entering 5, 7, and 10
Nameful
what results do you get?
Anonymous
5 = less than 10 7= greater than 5 10= greater than 5
Ale
Hi guys, I have a question, it is ok to post job opportunities?
Anonymous
Feruza
Hey there! I'm getting this message: "make: Nothing to be done for 'substitution'." while trying to compile this: #include <ctype.h> #include <cs50.h> #include <stdio.h> #include <string.h> #include <math.h> int main(int argc, string argv[]) { if(argc != 2) { printf("Usage: ./substitution key.\n"); } if(strlen(argv[1]) != 26) { printf("Key must contain 26 characters.\n"); } for(int i = 0; i < strlen(argv[1]); i++) { for(int n = i+1; n < strlen(argv[1]) + 1; n++) { if(isalpha(argv[1][i]) && tolower(argv[1][i]) == toupper(argv[1][n])) { //fine } } } } It was compiling yesterday just fine
Anonymous
Try a make clean && make substitution
Feruza
gotcha, thank you!
Anonymous
gcc, clang, msvc
Thanks much bro
Anonymous
#QuestionForBeginners Why were references added to C++ when you could do everything that a reference could do using pointers?
Anonymous
What is the better condition to check whether a given interger is a prime number or not,,,,,,,
Pavel
i could never understand the meaning of references tbh
After working with golang for some time I can say references is a bliss. Compared to having to check every pointer for being null, or crashing here and there
Diego
At least, as far as my meager understanding goes, pointers are addresses that point to locations in memory, and the type of pointer tells the program what to expect there, even if with ugly enough code something completely different could be lying in wait at that address References on the other hand, are also pointers to memory, but are also references to a point in memory that is known for a fact to have an object there
Official hooligan of Pius XII
I'd rather say that references are simply constant aliases for other variables
Amol
Let say we define a string variable in cpp, string s("Hello World!"); than what is the difference between 1. s.c_str(); 2. (char*)s.c_str();
Anonymous
Let say we define a string variable in cpp, string s("Hello World!"); than what is the difference between 1. s.c_str(); 2. (char*)s.c_str();
use C++ style casts instead of C style casts and the answer is obvious 2. const_cast<char *>(s.c_str());
bunny
Hello. I have chosen the topic "pointer" for my project. Now i have to make and show a program and i'm unable to think for a c++ program which can implement pointers. Any recommendations? Also the program should'n be too easy or too hard. I'm in second sem.
Official hooligan of Pius XII
nevertheless sorting algorithms or small games are nice point to start from
bunny
nevertheless sorting algorithms or small games are nice point to start from
🥴🥴 games 😂. Currently I have only basic knowledge upto File handling (how to create/read a txt file) 😅
Official hooligan of Pius XII
selection sort then maybe
bunny
selection sort then maybe
Ah! nice you mean replace array with pointer 👀
Official hooligan of Pius XII
Ah! nice you mean replace array with pointer 👀
declare an array of random integers in your main, then make a void function that takes a pointer to this array and sorts it
Official hooligan of Pius XII
it should suffice ig, and if not, go for dynamically allocating array of pointers to pointers lol
Official hooligan of Pius XII
Vlad
That's ub
Official hooligan of Pius XII
Jerryjay
libwinpthread-1.dll not found meanwhile i have it in my code blocks directories
@𝑺𝒐𝒃𝒌𝒂
#QuestionForBeginners Why were references added to C++ when you could do everything that a reference could do using pointers?
The reasons (I think) may be: - references are forced to refer to defined location (avoidance of memory leaks); - references are not objects. So, they do not occupy storage.
Aman
#QuestionForBeginners Why were references added to C++ when you could do everything that a reference could do using pointers?
I'd give the following 2 reasons:- 1. References can't be null (as also pointed by many others). 2. References , unlike pointers , can't be reseated, i.e., they can't be made to refer to an arbitrary memory location once they've been initialized. This is a huge benefit. Let me try to explain with an example:- Suppose we have a pointer to an integer variable i, named p (int* p =&i), and we are incrementing the value of 'i' through 'p' via successive iterations of a loop. Now , if a programmer forgets to use to dereference operator before p (*p) while accessing the content of variable i, the memory address pointed to by p will simply keep on changing with each iteration, rather than the actual value of 'i'. This can be disastrous due to the potential threat of reading/ writing unrelated data. There is no such danger with references as they are simply aliases to variables defined earlier . So, 'int& p= i; p++' is very much safe as it only modifies the value of i. As for size considerations, I believe a reference takes the same amount of storage as occupied by a pointer (e.g. 64 bits on a 64-bit machine). Please do let me know if I was right or wrong in my reasoning 😊
Anonymous
#QuestionForBeginners Why were references added to C++ when you could do everything that a reference could do using pointers?
I read through all the reasons here. Most of them were about references being safer because they have to refer to an object and that we dont have to check for null references. Though true, we yet still do read so many gotchas about returning references to local objects. So it is not like references address the problem of null pointers or invalid pointers completely. The other reasons mentioned were that references once initialized cannot be made to point to another variable. Well constant pointers can be used for the same thing. Infact references are implemented using constant pointers most often. Actually the reason why references were introduced was none of these. They were actually proposed by Alexander Stepanov and the reason why he wanted them to be added to C++ was to support operator overloading. References make using operators analogous to how we use them on built in types because they are just aliases (as mentioned by Aman). Let us take the assignment operator for example. Assignment operator on built in types returns a reference to its left hand side operand. So if we were to overload the assignment operator for our class, we would do something like this: class A { public: A& operator=(const A&); }; This allows us to do A obj, obj1, obj2; (obj = obj1)=obj2; If it were not for references, we would have to do something like class A{ public: A* operator=(const A* const); }; To use it like how we did earlier, we would have to do something like A obj, obj1, obj2; *(obj=&obj1)=&obj2; The same applies to all other operators as well. References being just aliases allows us to use them for operator overloading making it easier for the clients to call them just like they would do using variables of built in types. (From the Annotated C++ Reference Manual)
@𝑺𝒐𝒃𝒌𝒂
I read through all the reasons here. Most of them were about references being safer because they have to refer to an object and that we dont have to check for null references. Though true, we yet still do read so many gotchas about returning references to local objects. So it is not like references address the problem of null pointers or invalid pointers completely. The other reasons mentioned were that references once initialized cannot be made to point to another variable. Well constant pointers can be used for the same thing. Infact references are implemented using constant pointers most often. Actually the reason why references were introduced was none of these. They were actually proposed by Alexander Stepanov and the reason why he wanted them to be added to C++ was to support operator overloading. References make using operators analogous to how we use them on built in types because they are just aliases (as mentioned by Aman). Let us take the assignment operator for example. Assignment operator on built in types returns a reference to its left hand side operand. So if we were to overload the assignment operator for our class, we would do something like this: class A { public: A& operator=(const A&); }; This allows us to do A obj, obj1, obj2; (obj = obj1)=obj2; If it were not for references, we would have to do something like class A{ public: A* operator=(const A* const); }; To use it like how we did earlier, we would have to do something like A obj, obj1, obj2; *(obj=&obj1)=&obj2; The same applies to all other operators as well. References being just aliases allows us to use them for operator overloading making it easier for the clients to call them just like they would do using variables of built in types. (From the Annotated C++ Reference Manual)
Good to know it. Thanks. There is a few moment I began to learn operator overloading; and I haven't digested it yet 😩. But I noticed that the structure of some operators was a bit different(doesn't return a reference to its left hand operand?). For example: *, + and == Class B { public: B operator +(const B&) const; B operator *(const B&) const; bool operator == (const B&) const; } Client program: B b1, b2, b3, b4; b1 = b2 + b3; b4 = b1*b2; if(b1 == b2) do something; work as well (without using the ampersand symbol). May you clarify me here, please?
Anonymous
Good to know it. Thanks. There is a few moment I began to learn operator overloading; and I haven't digested it yet 😩. But I noticed that the structure of some operators was a bit different(doesn't return a reference to its left hand operand?). For example: *, + and == Class B { public: B operator +(const B&) const; B operator *(const B&) const; bool operator == (const B&) const; } Client program: B b1, b2, b3, b4; b1 = b2 + b3; b4 = b1*b2; if(b1 == b2) do something; work as well (without using the ampersand symbol). May you clarify me here, please?
Only some expressions return a glvalue. For example assignment, dereferencing operator (*) and so on. The operators +(binary as well as as unary), *(binary), == all return rvalues when used with built in types like int etc (they return prvalues and for objects of class types in C++17 terms they should return either an xvalue or a prvalue). You can just assume rvalues as those values to which an rvalue reference can be bound. Each operator has a well defined meaning when applied to built in types as in like whether it returns a glvalue or a rvalue. When you overload these operators for your classes, you should ensure that the behavior of these operators is the same as when applied to built in types. The compiler wont enforce this. For example while overloading binary + operator, nothing prevents you from returning a pointer as a return value but clients of your code would be surprised by such behavior. To see when you should return a reference and when you should not, you can just use the built in meaning of these operators as an example and check it out. int a , b , c; (a = b) = c; //assigns c to a finally This compiles fine. This means that built in operator= returns a reference and so your overloaded operator= should return a reference as well. a*b = c; This fails to compile which means the built in binary * operator does not return a glvalue. Hence your overloaded operator should not either which is why you return a value rather than a reference when you overload this operator.
@𝑺𝒐𝒃𝒌𝒂
Only some expressions return a glvalue. For example assignment, dereferencing operator (*) and so on. The operators +(binary as well as as unary), *(binary), == all return rvalues when used with built in types like int etc (they return prvalues and for objects of class types in C++17 terms they should return either an xvalue or a prvalue). You can just assume rvalues as those values to which an rvalue reference can be bound. Each operator has a well defined meaning when applied to built in types as in like whether it returns a glvalue or a rvalue. When you overload these operators for your classes, you should ensure that the behavior of these operators is the same as when applied to built in types. The compiler wont enforce this. For example while overloading binary + operator, nothing prevents you from returning a pointer as a return value but clients of your code would be surprised by such behavior. To see when you should return a reference and when you should not, you can just use the built in meaning of these operators as an example and check it out. int a , b , c; (a = b) = c; //assigns c to a finally This compiles fine. This means that built in operator= returns a reference and so your overloaded operator= should return a reference as well. a*b = c; This fails to compile which means the built in binary * operator does not return a glvalue. Hence your overloaded operator should not either which is why you return a value rather than a reference when you overload this operator.
Understood. Thank you
@𝑺𝒐𝒃𝒌𝒂
I have other question (not related to the first one). I have a simple code that read data stored in a file. It works well when I use Qt creator, but doesn't work when using codeblocks. Why? I changed nothing in the code
@𝑺𝒐𝒃𝒌𝒂
Do you use the same compiler? Same working directory?
Same compiler, not same directory All code are in main.cpp. I created on project for qt creator, one for codeblocks and in both cases I put the file containing the data(test.txt) in the same directory that exe file
@𝑺𝒐𝒃𝒌𝒂
Post your code on pastebin.com
https://pastebin.com/npstZdC7
Anonymous
https://pastebin.com/npstZdC7
This should work irrespective of the IDE as long as the input file "test.txt" is accessible. Just check ifstream status after the open call using inFile.is_open() to see if the file was opened successfully. Check the permissions on the input file as well to see if your program has read access to it. Also check if the program has write permission to the directory concerned.
Anonymous
Hii
SS
This message couldn't be displayed on your device due to copyright infringement.
Aman
I read through all the reasons here. Most of them were about references being safer because they have to refer to an object and that we dont have to check for null references. Though true, we yet still do read so many gotchas about returning references to local objects. So it is not like references address the problem of null pointers or invalid pointers completely. The other reasons mentioned were that references once initialized cannot be made to point to another variable. Well constant pointers can be used for the same thing. Infact references are implemented using constant pointers most often. Actually the reason why references were introduced was none of these. They were actually proposed by Alexander Stepanov and the reason why he wanted them to be added to C++ was to support operator overloading. References make using operators analogous to how we use them on built in types because they are just aliases (as mentioned by Aman). Let us take the assignment operator for example. Assignment operator on built in types returns a reference to its left hand side operand. So if we were to overload the assignment operator for our class, we would do something like this: class A { public: A& operator=(const A&); }; This allows us to do A obj, obj1, obj2; (obj = obj1)=obj2; If it were not for references, we would have to do something like class A{ public: A* operator=(const A* const); }; To use it like how we did earlier, we would have to do something like A obj, obj1, obj2; *(obj=&obj1)=&obj2; The same applies to all other operators as well. References being just aliases allows us to use them for operator overloading making it easier for the clients to call them just like they would do using variables of built in types. (From the Annotated C++ Reference Manual)
Thanks for this explanation! While being aware of operator overloading concept, I failed to appreciate the utility of references to them this way before. This discussion was really helpful. Now I also recognize their role in copy and move constructors.. And may I request, kindly continue posting further thought-provoking questions for beginners which may help me in my learning !! 😊
klimi
This message couldn't be displayed on your device due to copyright infringement.
you don't really need to specify what IDE you are using, the language stays the same
klimi
well, i don't know courses that are based on IDEs, sorry
SS
well, i don't know courses that are based on IDEs, sorry
Do you have any course for learning c++ nd data structure?
klimi
Do you have any course for learning c++ nd data structure?
any popular on udemy / coursea will do it
@𝑺𝒐𝒃𝒌𝒂
This should work irrespective of the IDE as long as the input file "test.txt" is accessible. Just check ifstream status after the open call using inFile.is_open() to see if the file was opened successfully. Check the permissions on the input file as well to see if your program has read access to it. Also check if the program has write permission to the directory concerned.
I checked, but nothing happened (the message checking didn't print). But when I did the same deleting test.txt in Qt creator, the checking message print something... And curiously I opened the ouput file generated by the compiler. We can see Student name, Test scores and average test score with garbage values.
Hanz
Does anyone have an idea on how i could do this text formatting? Or is it just not exists? Image Link They produce link inside a text just like what Telegram did to my image link above
Anonymous
I checked, but nothing happened (the message checking didn't print). But when I did the same deleting test.txt in Qt creator, the checking message print something... And curiously I opened the ouput file generated by the compiler. We can see Student name, Test scores and average test score with garbage values.
If is_open failed then you should not continue with the rest of the operations. If you do, you will only see garbage values (undefined behavior) You should do something like this if(!inFile.is_open()){ cerr << "File opening for read failed"; exit(1); //or throw an exception } else { //rest of your code }