• What Do You Need Help With? V6
    7,544 replies, posted
[QUOTE=proboardslol;46487772]Have you encountered pointers? A string is simply an array of characters, and an array is simply a pointer to the memory location of various characters. So if you want an array of strings (an array of arrays, or a pointer to more pointers), you want: [code] char *a[2]; [/code] [/QUOTE] So say for an array that contains the name of a user, I would use [code]a *[x][/code] instead of [code]a[x][y][/code] where y is the maximum number of characters that array can take. Because if [code]a *[x][/code] is used, it will place a string instead of individual characters, correct?
[QUOTE=Cold;46487729]Thats a pretty big question. There is no "correct" way to do it, but something simple/generic would look something like this. So lets start at the core and our work our way out. So lets say you have a Goomba, this goomba has a unique texture and maybe some unique behaviour, it derives from a base NPC class, which has some of this NPC functionality in it, that will on its own derive from something like an Object/Entity class, which for example has some collision functionality and the rendering code in it. So to manage all these objects, most games have something thats often referred too as the "scene" or in small things something like a "game", which is basically a class that manages a list of entities and calls their various update/render functions. Then all other classes can be abstracted to "interfaces" basically a utility class that's mainly only called by functionality on the entities, stuff like Drawing Input Physics (Window) are good examples of these kind of classes. For example an draw loop would look something like: 1. CDrawing->Predraw() // Clear buffers, prepare some other stuff. 2. CScene->Draw() // Make a call to every object in our object list to draw. 3.Goomba->Draw() // Virtual function could be overwritten by the class if they want to do a custom draw or do something before/after they draw. An example of what the parent function of Object/NPC would look like be simple like this Object->Draw() { this.shader->Draw(this.texture, this.x, this.y); } If you're doing things in something like SDL Or SFML this could also be the place where you simply invoke SDL/SFML functions, and you don't need to abstract things into a shader. 4. CDrawing->PostDraw() // Swap buffers, do some other post draw stuff. The integration of a scripting language, generally just means that when an relevant class(Object or System) is instanced, you'll create the matching object in your scripting language, And then when a function on the object is called by CScene, the object can pass the call to the scripting language, and then the scripting language can call the functions exposed to the scripting language on the systems to do various tasks. EDIT: Like integrating a scripting language can be a lot of work, and you need quite a lot of scriptable content for it to be worth the work, you should consider that.[/QUOTE] I've got the idea behind scripting down, I've done it in other languages, I just need to know how implementation exactly works because I'm not so experienced with OOP (i've been doing procedural languages like C and BASIC [dont laugh] ). Also for a list of entities to draw, how would you do that? What comes to mind for me is to have an array of NPC objects, and each time a new NPC is created, to load it into the next available space in that array, then to draw each one would be some kind of for loop like: [code] for(int i = 0; i < totalNumberOfNPCs; i++){ listOfNPCs[i]->update(); } [/code] where update reports on their x and y coordinates and other such things. This is how I handled things in procedural languages
[QUOTE=mastersrp;46487977]What you want is this: [code] #include <stdio.h> #include <string.h> int main( int argc, char **argv ) { char largelocker[5][3] = {"1L", "2L", "3L", "4L", "5L" }; printf( "%s\n", largelocker[0] ); getchar(); return 0; } [/code] What you were doing was accessing a single character, but you really want to print each string in the string array. char string is a single character. char string[] is an array of characters (a string). char string[][] is an array of strings (an array of arrays of characters).[/QUOTE] Ah, so I should have placed an %c instead of %s since I'm accessing a single character. I understand now, many thanks.
[QUOTE=frdrckk;46488197]Ah, so I should have placed an %c instead of %s since I'm accessing a single character. I understand now, many thanks.[/QUOTE] Yes, however please note that "1L" is TWO characters, which means it is a string (or an array of characters), and so printing "1L" would require you to use the code I wrote.
[QUOTE=proboardslol;46488065]I've got the idea behind scripting down, I've done it in other languages, I just need to know how implementation exactly works because I'm not so experienced with OOP (i've been doing procedural languages like C and BASIC [dont laugh] ). Also for a list of entities to draw, how would you do that? What comes to mind for me is to have an array of NPC objects, and each time a new NPC is created, to load it into the next available space in that array, then to draw each one would be some kind of for loop like: [code] for(int i = 0; i < totalNumberOfNPCs; i++){ listOfNPCs[i]->update(); } [/code] where update reports on their x and y coordinates and other such things. This is how I handled things in procedural languages[/QUOTE] Don't use an array use and std::vector. You shouldn't ever have an actual list of NPC objects, you should just have a list of w/e the lowest object type is, and make the functions virtual.
Hey, I am working on another assignment for class and I am having some issues getting my math to work at the very end. It seems to pass everything correctly, but when it calculates the bill, it just outputs $0, instead of outputting which item was ordered and then adding them up. Anyone care to take a look and see what I am doing wrong? I am sure I fucked something up. [code] #include <iostream> #include <string> #include <iomanip> using namespace std; struct menuItemType { string menuItem; double menuPrice; }; void getData(menuItemType items[]); void showMenu(menuItemType a[]); void order(int p[]); void printCheck(menuItemType c[], int p[]); int main() { menuItemType menuList[8]; int purchases[8]; getData(menuList); showMenu(menuList); order(purchases); printCheck(menuList, purchases); cin.get(); cin.get(); return 0; } void getData(menuItemType items[]) { items[0].menuItem = "Plain Egg"; items[0].menuPrice = 1.45; items[1].menuItem = "Bacon and Egg"; items[1].menuPrice = 2.45; items[2].menuItem = "Muffin"; items[2].menuPrice = 0.99; items[3].menuItem = "French Toast"; items[3].menuPrice = 1.99; items[4].menuItem = "Fruit Basket"; items[4].menuPrice = 2.49; items[5].menuItem = "Cereal"; items[5].menuPrice = 0.69; items[6].menuItem = "Coffee"; items[6].menuPrice = 0.50; items[7].menuItem = "Tea"; items[7].menuPrice = 0.75; } void showMenu(menuItemType a[]) { cout << "Welcome to Johnny's Restaurant" << endl; cout << "Here is our current menu: " << endl; for (int x = 0; x < 8; x++) { cout << (x + 1) << ". " << a[x].menuItem << " - $" << a[x].menuPrice << endl; } } void order(int p[]) { int selection; bool ordering = true; while (ordering) { cout << "Please choose an item to order. Enter 0 to finish ordering: "; cin >> selection; if (selection > 0 && selection <= 8) { p[selection - 1] += 1; } else { ordering = false; } } } void printCheck(menuItemType c[], int p[]) { double total = 0; const double TAX = 0; double totaltax = 0; double bill = 0; cout << "Customer receipt: " << endl; for (int x = 0; x < 8; x++) { if (p[x] > 0) { cout << c[x].menuItem << " - " << "$" << (c[x].menuPrice * p[x]) << endl; total = (c[x].menuPrice * p[x]); } } totaltax = total * TAX; bill = total + totaltax; cout << "The total is: $" << bill << endl; cout << "Thanks for eating at Johnny's Resturant!" << endl; } [/code]
[QUOTE=Cold;46489013]Don't use an array use and std::vector. You shouldn't ever have an actual list of NPC objects, you should just have a list of w/e the lowest object type is, and make the functions virtual.[/QUOTE] Note that virtual inheritance is perfectly fine for beginning, but in the future you will want to move away from it.
[QUOTE=Cold;46489013]Don't use an array use and std::vector. You shouldn't ever have an actual list of NPC objects, you should just have a list of w/e the lowest object type is, and make the functions virtual.[/QUOTE] How do I call every object to update then?
[QUOTE=proboardslol;46491191]How do I call every object to update then?[/QUOTE] [url]http://www.cplusplus.com/doc/tutorial/polymorphism/[/url] Read the section on virtual functions. If you have a Goomba object then add it to the scene using Scene->Add(Entity * C) a std::vector<Entity *> EnttiyList then Add entities using EntityList.push_back(C) Then loop the vector, you can still just call C->Update(), even though the compiler isn't runtime aware of the type it will still call Goomba::Update as long as the functions are virtual. Because for virtual functions the instance of the class holds the address of the virtual functions (inside a VTable or Jumptable). [editline]14th November 2014[/editline] [QUOTE=ECrownofFire;46490608]Note that virtual inheritance is perfectly fine for beginning, but in the future you will want to move away from it.[/QUOTE] Just curious, what is wrong with virtual inheritance?
[QUOTE=Cold;46491339][url]http://www.cplusplus.com/doc/tutorial/polymorphism/[/url] Read the section on virtual functions. If you have a Goomba object then add it to the scene using Scene->Add(Entity * C) a std::vector<Entity *> EnttiyList then Add entities using EntityList.push_back(C) Then loop the vector, you can still just call C->Update(), even though the compiler isn't runtime aware of the type it will still call Goomba::Update as long as the functions are virtual. Because for virtual functions the instance of the class contains the address of the "Update" function (inside a VTable or Jumptable). [editline]14th November 2014[/editline] Just curious, what is wrong with virtual inheritance?[/QUOTE] So the scene class will have the entitylist, and to add an entity to the world, I use entitylist.push_back(EntityToBeAdded); What might the loop look like?
[QUOTE=proboardslol;46491463]So the scene class will have the entitylist, and to add an entity to the world, I use entitylist.push_back(EntityToBeAdded); What might the loop look like?[/QUOTE] Look at the example here on how to iterate a std::vector [url]http://www.cplusplus.com/reference/vector/vector/begin/[/url]
[QUOTE=Cold;46491484]Look at the example here on how to iterate a std::vector [url]http://www.cplusplus.com/reference/vector/vector/begin/[/url][/QUOTE] so something like [code] for (std::vector<int>::iterator it = entityList.begin() ; it != entityList.end(); ++it){ *entityList->update(); } [/code] ?
[QUOTE=Gubbygub;46490319]Hey, I am working on another assignment for class and I am having some issues getting my math to work at the very end. It seems to pass everything correctly, but when it calculates the bill, it just outputs $0, instead of outputting which item was ordered and then adding them up. Anyone care to take a look and see what I am doing wrong? I am sure I fucked something up. [code]Johnny's restaurant code...[/code][/QUOTE] This (in terms of code logic) works perfectly for me, aside from one little thing (gotta be careful with C++ :)) [code]int purchases[8];[/code] This isn't initialized - this means the values inside are garbage, and could be read as any number that can fit in your data type! You could use memset() or a loop here to initialize every value to 0, but there's an easier way. If you do: [code]int purchases[8] = {0};[/code] because the compiler doesn't like partially initialized arrays and will fill the rest with 0 (this means int purchases[8] = {5} would be {5, 0, 0, 0, 0, 0, 0, 0}, so be careful!) Hope I helped!
[QUOTE=proboardslol;46491568]so something like [code] for (std::vector<int>::iterator it = entityList.begin() ; it != entityList.end(); ++it){ *entityList->update(); } [/code] ?[/QUOTE] That's how you used to have to do it, nowadays we have fancy things such as type deduction and range based for loops. These two things mean you can do this: [code]for(auto &entity : entityList) { entity.update(); }[/code]
[QUOTE=BackwardSpy;46492052]That's how you used to have to do it, nowadays we have fancy things such as type deduction and range based for loops. These two things mean you can do this: [code]for(auto &entity : entityList) { entity.update(); }[/code][/QUOTE] Can you translate to english? What is the range of &entity : entityList ?
It loops through the whole container. You should really read up on C++11 before just diving into it. Just follow this: Never use anything below C++11 at this day and age in a new project.
[QUOTE=proboardslol;46492329]Can you translate to english? What is the range of &entity : entityList ?[/QUOTE] The for just loops through all of entityList. Each loop, it'll update entity. entity is a reference to an entity in entityList, with it's type automatically deduced by the compiler (thus the "auto")
[QUOTE=Cold;46491339]Just curious, what is wrong with virtual inheritance?[/QUOTE] Among other things, it makes it impossible to add entities dynamically. You have to recompile. It's also bad for performance. But as I said, this will only matter when you want to do something a bit more advanced than fling a few bullets around. Bad performance is not so bad when you're barely using the capabilities of your CPU. Try looking into "entity-component systems" and "data oriented design" if you want to know more. Basically instead of having a singular Entity class with its position and model and whatever else, you have individual [I]components[/I] stored in arrays of components (because arrays are FAST), and your Entity class is now just a convenience wrapper around an index into those arrays (keeping the arrays consistent is a little annoying). Then when you need to go through and draw all your entities, you're only going through the necessary parts and not through whatever they drop when they die or whatever. But again, this is not something you want to look at yet. It's a fairly advanced optimization technique, though I find it also leads to very readable and extensible code. Lends itself very well to scripting too.
Has anyone here ever used FW1FontWrapper and run into problems with W8? For some people it just refuses to work and CreateFontWrapper fails. Not exactly sure how I'm supposed to solve it.
How can i use argv[1] as a parameter for fopen()? I get the following error when I try: [code] collin@collin-HP-15-Notebook-PC:~/Documents/CStuff/TileMapper/parsing$ gcc parser.c -o parser -std=c11 parser.c: In function ‘main’: parser.c:10:3: warning: passing argument 1 of ‘fopen’ makes pointer from integer without a cast [enabled by default] fp = fopen(argv[1], "r"); ^ In file included from parser.c:1:0: /usr/include/stdio.h:272:14: note: expected ‘const char * restrict’ but argument is of type ‘char’ extern FILE *fopen (const char *__restrict __filename, [/code] I understand the warning, but how do I turn argv[] into a const char*?
[QUOTE=proboardslol;46496204]How can i use argv[1] as a parameter for fopen()? I get the following error when I try: [code] collin@collin-HP-15-Notebook-PC:~/Documents/CStuff/TileMapper/parsing$ gcc parser.c -o parser -std=c11 parser.c: In function ‘main’: parser.c:10:3: warning: passing argument 1 of ‘fopen’ makes pointer from integer without a cast [enabled by default] fp = fopen(argv[1], "r"); ^ In file included from parser.c:1:0: /usr/include/stdio.h:272:14: note: expected ‘const char * restrict’ but argument is of type ‘char’ extern FILE *fopen (const char *__restrict __filename, [/code] I understand the warning, but how do I turn argv[] into a const char*?[/QUOTE] It isn't complaining about your char **argv, but about the "r", most likely. Otherwise I have no fucking idea how the shit you messed that up. The following code works: [code] #include <stdio.h> #include <stdlib.h> int main( int argc, char **argv ) { FILE *fp = fopen( argv[1], "r" ); fclose( fp ); return 0; } [/code] [editline]15th November 2014[/editline] Tested the code I wrote with the following [code] # gcc -std=c11 -Wall -Werror -o test test.c # clang -std=c11 -Wall -Werror -o test test.c [/code] Neither gave any errors or warnings, but completed without any issue. Can you confirm that this works for you as well? Try running my code in the same way I did, and report back if any issues came up.
[QUOTE=mastersrp;46496607]It isn't complaining about your char **argv, but about the "r", most likely. Otherwise I have no fucking idea how the shit you messed that up. The following code works: [code] #include <stdio.h> #include <stdlib.h> int main( int argc, char **argv ) { FILE *fp = fopen( argv[1], "r" ); fclose( fp ); return 0; } [/code] [editline]15th November 2014[/editline] Tested the code I wrote with the following [code] # gcc -std=c11 -Wall -Werror -o test test.c # clang -std=c11 -Wall -Werror -o test test.c [/code] Neither gave any errors or warnings, but completed without any issue. Can you confirm that this works for you as well? Try running my code in the same way I did, and report back if any issues came up.[/QUOTE] I had *argv, not **argv [editline]15th November 2014[/editline] How bad of an idea is it to do something like: [code] int* intPointer for(int i = 0; i < SomeNumber; i++){ intPointer = malloc(sizeof(intPointer) + sizeof(int)); } [/code] I wouldn't do THIS exactly, but how bad of an idea is it to iteratively malloc a pointer?
[QUOTE=proboardslol;46496949]I had *argv, not **argv [editline]15th November 2014[/editline] How bad of an idea is it to do something like: [code] int* intPointer for(int i = 0; i < SomeNumber; i++){ intPointer = malloc(sizeof(intPointer) + sizeof(int)); } [/code} I wouldn't do THIS exactly, but how bad of an idea is it to iteratively malloc a pointer?[/QUOTE] How would that even be remotely useful?
Yeah, that iterative malloc makes no sense to me either. Both the iterative part, and the fact you are allocating the size of intPointer itself each loop. Are you trying to do this to be able to resize an array or something? Using that approach, you're just gonna have a whole load of memory leaks, since you'd be mallocing without freeing the old memory (and also, all values intPointer pointed towards would become garbage) You'd want to do this, assuming I guessed right: [code]intPointer = realloc(intPointer, currentArraySize + sizeof(int) * someNumber)[/code] EDIT: Retroactively fixed sizeof(intPointer) - I've been spoiled by C++ where you can call delete[] on an array and have it automatically fully deleted (the compiler keeps track for you)!
[QUOTE=proboardslol;46496949]I had *argv, not **argv [editline]15th November 2014[/editline] How bad of an idea is it to do something like: [code] int* intPointer for(int i = 0; i < SomeNumber; i++){ intPointer = malloc(sizeof(intPointer) + sizeof(int)); } [/code] I wouldn't do THIS exactly, but how bad of an idea is it to iteratively malloc a pointer?[/QUOTE] sizeof(intPointer) does not give you the size of the allocated memory block. It gives you the size of the pointer itself, which is likely a constant 4 or 8 bytes depending on the architecture. There is no way in the language to get the size of the memory block pointed to by a pointer. If you want that, you need to keep track of the size yourself.
[QUOTE=ThePuska;46497655]sizeof(intPointer) does not give you the size of the allocated memory block. It gives you the size of the pointer itself, which is likely a constant 4 or 8 bytes depending on the architecture. There is no way in the language to get the size of the memory block pointed to by a pointer. If you want that, you need to keep track of the size yourself.[/QUOTE] I always thought for primitive types, the pointer is the same size as the type it points to. It's been years since I've done C++ though.
[QUOTE=Bladezor;46497684]I always thought for primitive types, the pointer is the same size as the type it points to. It's been years since I've done C++ though.[/QUOTE] No. The sizeof of a pointer is the size of a pointer. The sizeof of an array is the size of the array in bytes, unless the array has decayed into a pointer, in which case it's the size of the pointer. [code]unsigned char arr[2]; int n = sizeof(arr); // n will be 2.[/code] because arr is an array not decayed into a pointer.
[QUOTE=ThePuska;46497781]No. The sizeof of a pointer is the size of a pointer. The sizeof of an array is the size of the array in bytes, unless the array has decayed into a pointer, in which case it's the size of the pointer. [code]unsigned char arr[2]; int n = sizeof(arr); // n will be 2.[/code] because arr is an array not decayed into a pointer.[/QUOTE] Gotta be careful with that, though. If you pass the array into a function, the pointer itself is passed instead so sizeof(arr) would be the size of the pointer instead. C is so much fun! :suicide:
[QUOTE=proboardslol;46496949]How bad of an idea is it to do something like: [code] int* intPointer for(int i = 0; i < SomeNumber; i++){ intPointer = malloc(sizeof(intPointer) + sizeof(int)); } [/code] I wouldn't do THIS exactly, but how bad of an idea is it to iteratively malloc a pointer?[/QUOTE] Take a step backwards and think about why you would want to do this. Don't look at a specific solution and then look for flaws in it.
[QUOTE=mastersrp;46497373]How would that even be remotely useful?[/QUOTE] If I'm parsing a text file, and I want to add each character to a dynamically allocated multidimensional array, I could read the textfile once, get a bunch of other data from it, and at the same time add the characters to the array without having to get the total # of characters, the longest individual line in the text file, and the total number of carriage returns, then having to rewind the file, malloc the array based on the aforementioned data, and then load it into the array by running the same loop [i]again[/i]. I mean I'm probably going about the entire ordeal totally wrong, but this is just my train of that. edit: don't look at the fact that it's a for loop, since I wouldn't need to do something so obviously fucking stupid, I'm just wondering if you can iteratively malloc a multidimensional array using data that you don't know ahead of time
Sorry, you need to Log In to post a reply to this thread.