• What Do You Need Help With? V6
    7,544 replies, posted
Has anyone had any luck using Recast/Detour navigation? I have a few questions and would love to bother you on steam about them.
[QUOTE=confinedUser;45393708]anyone care to explain why this is not working like it should? i mean in some cases it does but in this case shown below does not count words, or even tell me what the repeated words are. i've been reading programming principles and practice using c++ this was one of it's code snippets i decided to toss it into the compiler and check it out for myself. To my surprise it doesn't work aswell as i thought it would :v: <image> here is the code [code] #include <iostream> #include <string> using namespace std; int main() { string previous = " "; string current; int number_of_words=0; cout << "enter sentence: "; while(cin>>current) { ++number_of_words; if(previous==current) { cout << "word number: " << number_of_words << endl; cout<< "repeated word: "<< current<< "\n"; } previous=current; } return 0; } [/code][/QUOTE] The only repeated word in your test is 'he', and you are only checking if words are repeated one after the other (checking if previous == current). In order to check whether a word was seen before you should use a set and see if it already contains the current word. [url]http://en.cppreference.com/w/cpp/container/set[/url] [url]http://www.cplusplus.com/reference/set/set/[/url] If you're having trouble figuring out why code isn't doing what you expect you should debug it. You can use a debugger, but what I normally do is put a bunch of print statements in the code. In your code nothing is printed out if there aren't any repeated words so you have no way of knowing the flow of the program. By the way, do you guys (who use C/C++) prefer cppreference.com or cplusplus.com? I used to use the latter but I switched to the former after someone said it was better, but I don't really see much difference in the quality.
[QUOTE=Fredo;45401895]The only repeated word in your test is 'he', and you are only checking if words are repeated one after the other (checking if previous == current). In order to check whether a word was seen before you should use a set and see if it already contains the current word. [URL]http://en.cppreference.com/w/cpp/container/set[/URL] [URL]http://www.cplusplus.com/reference/set/set/[/URL] If you're having trouble figuring out why code isn't doing what you expect you should debug it. You can use a debugger, but what I normally do is put a bunch of print statements in the code. In your code nothing is printed out if there aren't any repeated words so you have no way of knowing the flow of the program. By the way, do you guys (who use C/C++) prefer cppreference.com or cplusplus.com? I used to use the latter but I switched to the former after someone said it was better, but I don't really see much difference in the quality.[/QUOTE] i use cplusplus i like how it has references to libraries and such, well atleast the style they have it in.. Although it's explanations sometimes lose me. also i did not write that code it was made by stroustrup. Funny that the inventor can't write code that wants to work :P
[QUOTE=Fredo;45401895]The only repeated word in your test is 'he', and you are only checking if words are repeated one after the other (checking if previous == current). In order to check whether a word was seen before you should use a set and see if it already contains the current word. [url]http://en.cppreference.com/w/cpp/container/set[/url] [url]http://www.cplusplus.com/reference/set/set/[/url] If you're having trouble figuring out why code isn't doing what you expect you should debug it. You can use a debugger, but what I normally do is put a bunch of print statements in the code. In your code nothing is printed out if there aren't any repeated words so you have no way of knowing the flow of the program. By the way, do you guys (who use C/C++) prefer cppreference.com or cplusplus.com? I used to use the latter but I switched to the former after someone said it was better, but I don't really see much difference in the quality.[/QUOTE] cppreference.com has more stuff and updates much more quickly. The only reason cplusplus.com hasn't died already is because of the ridiculous SEO it gets from its domain name.
Is there a respectable of using non-blocking sockets on windows without using threads? The first problem I've run into is that because I'm polling accept as fast as my processor will allow it, one core of my processor is constantly maxed out. Are there any sleep functions in the C++ library that I could use that won't get me laughed at?
Is there a way to stop c++ strings from deleting themselves on return? I'm trying to return a wchar_t* constant using c_str() on the string, but it ends up corrupting/deleting itself before I can even use the value returned by the function, which causes read access violations.
Why not just return a wchar_t? You'll realize it's impossible to do something like this, and with good reason. Read up on RAII ([url]http://en.wikipedia.org/wiki/RAII[/url]) for reasons as to why C++ works like this. It's actually pretty great and fixes a lot of problems.
[QUOTE=Sombrero;45404934]Is there a way to stop c++ strings from deleting themselves on return? I'm trying to return a wchar_t* constant using c_str() on the string, but it ends up corrupting/deleting itself before I can even use the value returned by the function, which causes read access violations.[/QUOTE] why not return the whole string (as const wstring if you need it to be constant)?
[QUOTE=Sombrero;45404934]Is there a way to stop c++ strings from deleting themselves on return? I'm trying to return a wchar_t* constant using c_str() on the string, but it ends up corrupting/deleting itself before I can even use the value returned by the function, which causes read access violations.[/QUOTE] Don't return a pointer to the data of an object that is going out of scope, return the object itself.
[QUOTE=Sombrero;45404934]Is there a way to stop c++ strings from deleting themselves on return? I'm trying to return a wchar_t* constant using c_str() on the string, but it ends up corrupting/deleting itself before I can even use the value returned by the function, which causes read access violations.[/QUOTE] No. You almost definitely shouldn't being doing that.
[QUOTE=Sombrero;45404934]Is there a way to stop c++ strings from deleting themselves on return? I'm trying to return a wchar_t* constant using c_str() on the string, but it ends up corrupting/deleting itself before I can even use the value returned by the function, which causes read access violations.[/QUOTE] If you don't want to return a wstring as suggested before, take the target buffer as in argument to your function and write to it.
[QUOTE=confinedUser;45402182][...] also i did not write that code it was made by stroustrup. Funny that the inventor can't write code that wants to work :P[/QUOTE] This may sound a bit harsh but... if you post code you don't understand it doesn't matter who wrote it. The advice about debugging it still applies. In this case you're even mistakenly blaming the original creator, since it was you who misunderstood what the program is [I]supposed[/I] to do. (I looked it up since it would be unlikely for such an error to slip through in the whoknowswhich revision of the book.) [URL="http://page-book.ru/i565365#page"]As Stroustrup puts it:[/URL] [quote]TRY THIS Get the "repeated word detection program" to run. Test it with the sentence "[B]She she laughed He He He because what he did did not look very very good good[/B]". How many repeated words were there? Why? What is the definition of [I]word[/I] used here? [U]What is the definition of [I]repeated word[/I]?[/U] (For example, is "[B]She she[/B]" a repetition?)[/quote][noparse][underscore mine][/noparse]
[QUOTE=false prophet;45403206]Is there a respectable of using non-blocking sockets on windows without using threads? The first problem I've run into is that because I'm polling accept as fast as my processor will allow it, one core of my processor is constantly maxed out. Are there any sleep functions in the C++ library that I could use that won't get me laughed at?[/QUOTE] I know there's some way to register a callback and get that back through the (I think) event loop, since from C# you can use this to [I]await[/I] read operations without blocking a thread or busy waiting. No idea how to use this from C++ though.
If I wanted to draw lines in OpenGL, what would be easiest? I have a start and endpoint, but I could also make a matrix that transforms some arbitrary values to be the same as the start and endpoint. However, is there a faster way than recalculating this matrix every time I move these points? Could I plug in these values without writing them to the GPU buffer each time?
Depends on what you need them for. Are they for debugging purposes? Are they constantly moving?
[QUOTE=layla;45411965]Depends on what you need them for. Are they for debugging purposes? Are they constantly moving?[/QUOTE] No and yes. They are projectiles.
[QUOTE=false prophet;45403206]Is there a respectable of using non-blocking sockets on windows without using threads? The first problem I've run into is that because I'm polling accept as fast as my processor will allow it, one core of my processor is constantly maxed out. Are there any sleep functions in the C++ library that I could use that won't get me laughed at?[/QUOTE] [url=http://msdn.microsoft.com/en-us/library/windows/desktop/ms741669%28v=vs.85%29.aspx]WSAPoll[/url] might help? Look into std::future/[url=http://en.cppreference.com/w/cpp/thread/async]std::async[/url] for equivalents to C#'s asynchronous stuff, such as "await". They do launch threads, but you don't really have to worry about that (just make sure the function you're calling is thread-safe and that you can toss sockets across threads, otherwise you're going to run into issues!). Oh, and use std::this_thread::sleep and std::this_thread::sleep_until. Just toss them a std::chrono::duration or std::chrono::time_point, respectively.
[QUOTE=false prophet;45403206]Is there a respectable of using non-blocking sockets on windows without using threads? The first problem I've run into is that because I'm polling accept as fast as my processor will allow it, one core of my processor is constantly maxed out. Are there any sleep functions in the C++ library that I could use that won't get me laughed at?[/QUOTE] select can be used to check the state of one or more sockets prior to doing operations such as accept, recv or send. It takes a variable waiting time (which can be zero for non-blocking applications). [editline]17th July 2014[/editline] If you're waiting on multiple sockets on a single thread, select is the way to go.
Binary search tree in java... [CODE] import java.util.ArrayList; class BinarySearchTree<T extends Comparable<? super T>> extends ArrayList<Node<T>> { private Node<T> root; public BinarySearchTree() { root = null; } public BinarySearchTree(ArrayList<T> tree) { for (int i = 0; i < tree.size(); i++) { this.add(tree.get(i)); } } public void add(T item) { if (this.size() == 0) { root = new Node(item); this.add(root); } else { Node<T> newNode = new Node(item); addNode(root, newNode); } } private void addNode(Node<T> parent, Node<T> data) { if (data.getData().compareTo(root.getData()) < 0) { if (parent.hasLeftChild()) { addNode(parent.leftChild, data); } else { parent.setLeftChild(data); this.add(data); } } else if (data.getData().compareTo(root.getData()) > 0) { if (parent.hasRightChild()) { addNode(parent.leftChild, data); } else { parent.setRightChild(data); this.add(data); } } } public boolean remove(T obj) { Node<T> toBeRemoved = root; Node<T> parent = null; boolean found = false; while (!found && toBeRemoved != null) { if (toBeRemoved.getData().compareTo(obj) == 0) { found = false; } else { parent = toBeRemoved; if (toBeRemoved.getData().compareTo(obj) > 0) { toBeRemoved = toBeRemoved.leftChild; } else { toBeRemoved = toBeRemoved.rightChild; } } } if (!found) { return false; } if (toBeRemoved.leftChild == null || toBeRemoved.rightChild == null) { Node newChild; if (toBeRemoved.leftChild == null) { newChild = toBeRemoved.rightChild; } else { newChild = toBeRemoved.leftChild; } if (parent == null) { root = newChild; } else if (parent.leftChild == toBeRemoved) { parent.leftChild = newChild; } else { parent.rightChild = newChild; } return false; } Node<T> smallestParent = toBeRemoved; Node<T> smallest = toBeRemoved.rightChild; while (smallest.rightChild != null) { smallestParent = smallest; smallest = smallest.leftChild; } toBeRemoved.setValue(smallest.getData()); if (smallestParent == toBeRemoved) { smallestParent.rightChild = smallest.rightChild; } else { smallestParent.leftChild = smallest.rightChild; } return false; } public T getNode(T item) { Node<T> val = root; while (val != null) { System.out.println(val.getData()); if (val.getData().compareTo(item) < 0) { val = val.rightChild; } else if (val.getData().compareTo(item) > 0) { val = val.leftChild; } else { return val.getData(); } } return null; } public boolean found(T item) { if (getNode(item) == item) { return true; } return false; } } [/CODE] in main: [CODE] BinarySearchTree<Integer> bst = new BinarySearchTree<>(); bst.add(3); bst.add(4); bst.add(0); bst.add(2); bst.add(5); System.out.println("Getting 8 " + bst.getNode(8)); System.out.println("Finding 8 " + bst.find(8)); System.out.println("Removing 4 " + bst.remove(4)); System.out.println("Finding 4 " + bst.find(4)); [/CODE] [QUOTE] 3 4 Getting 8 null 3 4 Finding 8 false Removing 4 Node{value=5, leftChild=null, rightChild=null} 3 4 Finding 4 true[/QUOTE] Why is it still finding a reference to 4 even though I removed it? Why am I printing 3 and 4 again and again as well as Node 4's toString? All I want to do is eat a BLT not make a BST...
[QUOTE=WTF Nuke;45412007]No and yes. They are projectiles.[/QUOTE] It's perfectly fine to just upload triangle strips to the GPU to draw your effects. You can reallocate the buffer when you need to or just allocate a big buffer up front if you know how many lines there can be.
[QUOTE=blacksam;45418564]Binary search tree in java... <lots of code> Why is it still finding a reference to 4 even though I removed it? Why am I printing 3 and 4 again and again as well as Node 4's toString? All I want to do is eat a BLT not make a BST...[/QUOTE] The BST code doesn't seem to match your main code. There isn't a find method, did you mean found? The reason why it's printing 3 and 4 out is because you print out each node you look at in the getNode method. Did you write this code? As to why the 4 isn't removed, I'm not too familiar with the algorithm to remove a node from a BST, but in your code the variable found is never set to true (I think it's a typo), so it ends up returning without removing anything. I'm not sure why it's printing out the node though, because the remove method returns a boolean (again, is the code you posted the latest version?). Like I said to a previous user, debugging can solve a ton of your problems. If you don't want to use a debugger just throw a bunch of print statements in there to see where the program is getting to. You should find that it doesn't get past the first big chunk of code in the remove method.
[QUOTE=Fredo;45422592]The BST code doesn't seem to match your main code. There isn't a find method, did you mean found? The reason why it's printing 3 and 4 out is because you print out each node you look at in the getNode method. Did you write this code? As to why the 4 isn't removed, I'm not too familiar with the algorithm to remove a node from a BST, but in your code the variable found is never set to true (I think it's a typo), so it ends up returning without removing anything. I'm not sure why it's printing out the node though, because the remove method returns a boolean (again, is the code you posted the latest version?). Like I said to a previous user, debugging can solve a ton of your problems. If you don't want to use a debugger just throw a bunch of print statements in there to see where the program is getting to. You should find that it doesn't get past the first big chunk of code in the remove method.[/QUOTE] Yeah, I did it all in one sitting. So I probably should clean it up before asking why it doesn't work... Thanks for catching the print. I'm going back and doing it all over again on paper this time. The remove is frustrating as all get out. The different cases are meant to be drawn out. So that's what I'll do.
If I compile a program in C++ that looks like something this: [code] int main() { char Text[] = "This is a test text!"; printf("%c",Text[0]) return 0; } [/code] Where will the Text variable be stored in the pe file, and where will it be loaded in the memory after running the program?
Where the compiler decides and where the OS decides respectively?
[QUOTE=reevezy67;45428501]Where the compiler decides and where the OS decides respectively?[/QUOTE] True, but on modern systems that support DEP the constants are usually put in a separate page with the NX bit set, which means they also appear in a different chunk of the PE file.
I've always wondered how full-screen post processing is done with pixel shaders. My guess is that you first render everything to a texture, then draw the texture to the screen with your post processing fragment shader. Is that correct? Is there a better/simpler way to do it?
[QUOTE=Larikang;45431581]I've always wondered how full-screen post processing is done with pixel shaders. My guess is that you first render everything to a texture, then draw the texture to the screen with your post processing fragment shader. Is that correct? Is there a better/simpler way to do it?[/QUOTE] Pixel shaders don't need render to texture, they are processed as stuff renders directly to screen.
[QUOTE=Larikang;45431581]I've always wondered how full-screen post processing is done with pixel shaders. My guess is that you first render everything to a texture, then draw the texture to the screen with your post processing fragment shader. Is that correct? Is there a better/simpler way to do it?[/QUOTE] Post-processing is any processing done after the scene is rendered, so yes. Different effects have to be done at different times, or can be done at multiple times.
I guess what I had in mind was any shader that requires referencing pixels in other positions. It seems like you would need that to be a post-process since pixels are evaluated in parallel.
Looking for C++/C# book recommendations. I want to buy a nice big selection of books of all kinds. Reference, introductory programming, etc. I've looked myself, but there's a pretty huge selection. I'm still mostly a beginner, if that factors in. Also other languages are fine, those are just two I want to focus on right now
Sorry, you need to Log In to post a reply to this thread.