Jump to content
  • Advertisement


  • Content Count

  • Joined

  • Last visited

Community Reputation

102 Neutral

About Shruubi

  • Rank
  1. Shruubi

    function pointers?

    i use conio.h for kbhit() and getch() which, may be old and not standardized but seem to do the job, especially when i am basing movement around the arrow keys without having to delve into winapi.
  2. Shruubi

    function pointers?

    oh wow, thats the kind of stupid mistake you make when your up at 2am after working customer service all day. thanks for spotting my blunder!
  3. i'm working on a console game, and am trying to implement a menu system, what i have so far is a function that will draw out the menu and take the input and return a value based on the menu choice, and i wanna use that return value as the parameter of another function which will perform actions based on what the choice was (play, load, credits, quit). my first attempt was to chuck the function as a normal function call in the parameter assuming that the return would be used as the parameter, but what actually happens is that the return of the functions used as the parameter will cause the calling function to return with the same value as well. i'm assuming, therefore that i will need to use function pointers to pass the function in as a parameter and get the return value that way, however, for some reason my function parameters aren't working. any help would be much appreciated. //prototypes int menu(); int DoAction(int (*choice)()); //function implementation int menu() { int x = 14; int y = 0; char input = '\0'; cout << "1: new game" << endl << "2: load game" << endl << "3: credits" << endl << "4: quit" << endl; gotoxy(x, y); cout << "<-"; while(true) { if(_kbhit()) { input = _getch(); switch(input) { case UP_ARROW: if(y > 0 && y < 4) { gotoxy(x, y); cout << " "; y--; gotoxy(x, y); cout << "<-"; break; } break; case DOWN_ARROW: if(y > -1 && y < 3) { gotoxy(x, y); cout << " "; y++; gotoxy(x, y); cout << "<-"; break; } break; case 13: //enter was pressed switch(y) { case 0: return 0; case 1: return 1; case 2: return 2; case 3: return 3; default: break; } default: break; } } } return 0; } int DoAction(int (*choice)()) { int ch = (*choice)(); switch(ch) { case 0: case 1: case 2: case 3: exit(0); default: break; } return 0; } //function use (in main.cpp) - causes error: a value of type int (*)() cannot be used to initialize an entity of type int int DoAction(&menu); the error i am getting: error: a value of type int (*)() cannot be used to initialize an entity of type int thanks
  4. Shruubi

    algorithm for buying items

    Knapsack is the algorithm which finds "take N items of highest total value" I need to find all the possible ways I can buy X amount of items given X amount of dollars[/quote] Iterate over all possible combinations (not permutations), print those where total value of selected items is <= X. The problem now becomes finding all combinations. [/quote] edit: code tags are screwing around with my formatting. this is my current effort in c++: http://codepad.org/EQUtrrGc that is my current effort, i'm not quite sure about how to approach my next step.
  5. I do apologize first off if this has been posted in the wrong section, i was unsure as to where a question like this should go, and thought this board would be most appropriate. My problem is this: Given an input of X, I need to find all the possible ways I can buy X amount of items given X amount of dollars (given that the prices for the items are constants: 2, 7 and 0.5). Someone pointed me towards the knapsack problem, however all the material I have found on the subject is a little hard to decipher let alone translate from so that it works for price and not weight, and that the knapsack problem only provides one output. The only other solutions I have come across have been on maths websites which present the same problem, but give the explanation for only one input and in such a way as it seems near impossible to translate the explanation for a more general input. Any hints or guidance would be greatly appreciated. Thanks.
  6. Ok, so first off, i would like to extend a huge thank you to everyone who helped me, you guys are all great! I managed to fix the problem i was having once i jumped onto visual studio, the problem was that while i was checking the character above the current character to see if it was also an 'a', i was not checking the character before that as well. My solution is as follows: #include <iostream> #include <string> using namespace std; int main () { string input = ""; string vowel = "aaa"; string end = "."; getline(cin, input, '.'); for(int it = 0; it < input.length(); it++) { if(input[it] == 'a' && (input[(it+1)] != 'a' && input[(it-1)] != 'a')) // <- fixed this up { input.insert(it, vowel); it+=vowel.length(); //skip ahead 3 characters (length of 3 a's string), this stops the program going into an infinite loop of adding a's. } } input.append(end); //re-add the full-stop. cout << input << endl; cin.sync(); cin.get(); return 0; } Because the delimiter on the getline is a fullstop, it is not carried over into the actual string, so as such, i just appended a full stop onto the end of the string to re-add the fullstop to the string. Again, a big thanks to everyone who helped me. P.S - I guess i better learn how to use the gdb then for when i am on my linux machine!
  7. thanks to alvaro's advice, I managed to fix the problem, it indeed ended up being an infinite loop I was stuck in. And at the moment I don't really have access to a debugger (or if I do, then I don't know how to use it!) as I am compiling through the terminal using G++ on a Linux box. If I was using visual studio, that would have been my first port of call. finally, I am having one last problem with this program, when I type in a word which has multiple 'a' characters then it adds the extra 'a' characters into the string, I've tried adding 1 to the it variable directly after the adding of the characters, if that character is an 'a' so that when the loop comes back around, it will add another 1 onto the it variable and skip over that a, however it doesn't quite seem to work for some reason. Thank you so much to everyone who has helped me so far.
  8. I have a feeling that the second option seems the most likely, however, I'm not quite sure how I would fix that. Do you have any advice on what I could do to fix it?
  9. Oh, sorry, i should have included that, my bad! The idea is that for each occurrence of a single 'a' character should become 4 'a' characters. However, if there is two 'a' characters in sequence, then it will not be altered. Going by what I can gather from documentation, this code should be correct.
  10. I am doing some c++ excercises with the string library, and i seem to be stuck on the insert() function. My program compiles directly, and i am sure i haven't made any errors, yet when i give my input and press the delimiter key, nothing happens and i can seem to figure out why, so i would appreciate anyone to look over my code and pick out any errors i made. #include <iostream> #include <string> using namespace std; int main () { string input = ""; string vowel = "aaa"; getline(cin, input, '.'); for(int it = 0; it < input.length(); it++) { if(input[it] == 'a' && input[(it+1)] != 'a') { input.insert(it, vowel); } } cout << input << endl; cin.sync(); cin.get(); return 0; } any advice would be appreciated, thanks.
  11. no, it was clear, i guess i was just hoping for a different answer, but obviously one does not exist. thank you for your help.
  12. so there is no way to be able to write to a file: bob 18 180? i guess this would then apply to any variable, so while it is possible to write the variable value to a file, you cannot write the variable name?
  13. i have a class, for the purpose of this question it is a simple class called person which stores their age and height (in cm). Now say i wanted to write the objects of this class to a file, the name of the object would be the persons name, and the class contents would obviously be the relevant persons age and height. How would i go about writing all this information to a file (including the persons name), for recall without defining the persons name as a string within the class? #include <iostream> #include <fstream> using namespace std; class person { public: int age; int height; } int main() { person bob; ofstream myfile; myfile.open("file.txt", ios::app); bob.age = 18; bob.height = 180; myfile << /*write the object name somehow*/"" << bob.age << " " << bob.height << "\n"; myfile.close(); return 0; } i hope my question has made sense. Thanks in advance
  14. so, i've been messing around with maps in C++ and i've figured out how to output the values of a map, however i was wondering if it is possible to output the key as well as the value assigned to the specific key? this is what i have so far: #include <iostream> #include <string> #include <map> using namespace std; int main() { typedef map <string, int> MapType; MapType data; data["first"] = 10; cout << data["first"]; cin.get(); return 0; } thanks.
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!