• C++ what is the exit code 4294967295? aka I need help with a program crash.
    7 replies, posted
I was testing my program and ran into a issue, I read up on it and it is a return exit code, other than that I don't know what it is. this number, "4294967295", gets returned from string.find(" "), when a string that has no space is used. I need to do error handling because my application crashes if the following code can find no space: [code] string ucmd::getcmd(string full){ return full.erase(full.find(" "),full.length()-full.find(" ")); }; [/code] If it cannot find a space( aka if i supply it a string like "fun" ), the entire program will crash and give a message in the DOS window that says [quote] This application has requested the Runtime to terminate it in an unusual way. Please contact the applications support team for more information. [/quote] Basically what is the best way to do a check, to make sure the string has a space before I call the getcmd function, or even better check it inside the getcmd function. thanks also: [url]http://bytes.com/topic/c/answers/217841-return-code-4294967295-uint_max[/url]
I suggest you take a look at [url=http://www.cplusplus.com/reference/string/string/]cplusplus's string reference[/url] if you have anymore questions. [code]string str = "Hell o"; if (str.find(" ") != -1) { cout << "There is a space" << endl; }[/code] or [code]string str = "Hell o"; if (str.find(" ") != str.npos) { cout << "There is a space" << endl; }[/code] I'm not sure which is better practice.
[QUOTE=Ltp0wer;32823357]I'm not sure which is better practice.[/QUOTE] Generally it's better to not toss in "magic numbers." If there's a define, constant, or in this case a member that serves the purpose, you should use it instead of what it represents. In other words, "str.npos" instead of "-1" is better practice.
worked perfectly, thanks guys [editline]17th October 2011[/editline] while i'm here, how can i delete something that i've defined, i don't have the words to explain it but I can show you. [code] // All of this shit will stay, obviously class item{ bool reach; public: void setreachable(bool); bool getreachable(); }; bool item::getreachable(){ return reach; }; void item::setreachable( bool reachable ){ reach = reachable; }; // now lets say i did item book; book.setreachable(true); // now lets say i want 'book' completely removed, gone. how do i do that. in lua you just do like // book = nil; how do i do something like that in c++? [/code]
If it isn't a pointer it should delete itself when it goes out of scope AFAIK (assuming it was created in the same scope). With pointers you have to type "delete book;" to prevent a memory leak. Atleast i think so, someone correct me if i'm wrong as i haven't written anything in C++ for months :v:
[QUOTE=LauScript;32823712]worked perfectly, thanks guys [editline]17th October 2011[/editline] while i'm here, how can i delete something that i've defined, i don't have the words to explain it but I can show you. [code] // All of this shit will stay, obviously class item{ bool reach; public: void setreachable(bool); bool getreachable(); }; bool item::getreachable(){ return reach; }; void item::setreachable( bool reachable ){ reach = reachable; }; // now lets say i did item book; book.setreachable(true); // now lets say i want 'book' completely removed, gone. how do i do that. in lua you just do like // book = nil; how do i do something like that in c++? [/code][/QUOTE] If you're not using 'new' and are declaring the object in a function, here's what happens: - Processor starts running function, allocates some space on the stack for it - Processor executes other code from the function - Processor gets to 'item book', calls constructor - Processor executes other code from the function - Processor hits 'return'. Right before it, the compiler automagically added a destructor call to your item object - Processor exists from function, the space that was allocated for the object is now marked as free
Alright, I see. How can I somehow match a string to a class i've created, again I can't explain it, but I can show you. [code] #include <cstdlib> #include <iostream> #include <string> using namespace std; class ucmd{ public: string getarg(string,string); string getcmd(string); }; string ucmd::getcmd(string full){ if ( full.find(" ") != full.npos ){ return full.erase(full.find(" "),full.length()-full.find(" ")); }else{ return "CMD_FAIL"; }; }; string ucmd::getarg( string cmd, string full){ string str = full.substr(cmd.length()+1); string err = "Invalid command arguments!"; if ( str.length() < 1 ){ return err; }else if ( str == " " ){ return err; }else if ( str == "" ){ return err; }else{ return str; }; }; class item{ bool reach; string usetext; string uid; public: void setreachable(bool); bool getreachable(); string getusetext(); void setusetext(string); void setuid(string); }; void item::setuid(string name){ uid = name; }; void item::setusetext(string text){ usetext = text; }; string item::getusetext(){ return usetext; }; bool item::getreachable(){ return reach; }; void item::setreachable( bool reachable ){ reach = reachable; }; class character{ item holding; public: void take(item); bool canReach(item); }; bool character::canReach(item name){ return name.getreachable(); }; void character::take( item name){ if ( name.getreachable() == true ){ holding = name; //cout << name.getusetext() << endl; }else{ cout << "You can't reach that!" << endl; }; }; int main(int argc, char *argv[]) { // for storage. string cinline; // so we can use single argument commands :) ucmd cmd; // The player. character player; // Lets do the introduction... cout << "You smell the damp, chemical air.. you awake..\n" << endl; cout << "As you look around you see a table set afront you, on the table sits a glass beaker, a bunsen burner, a vial labeled as sulphuric acid and a glass dropper \n" << endl; cout << "You try to stand up, you find your left hand is handcuffed to the chair, which is bolted to the ground.. what will you do?\n" << endl; // Alright, now lets create the items we stated in the intro. item beaker; item acid; item glassdropper; item burner; // set their information. beaker.setuid( "item_beaker" ); beaker.setreachable(true); beaker.setusetext( "You grab the glass beaker." ); acid.setuid( "item_acid" ); acid.setreachable(true); acid.setusetext( "You grab the vial of acid, don't spill!" ); glassdropper.setuid( "item_glass_dropper" ); glassdropper.setreachable(true); glassdropper.setusetext( "You grab the glass dropper, it has a small rubber pad on the top." ); burner.setuid( "item_burner" ); burner.setreachable(true); burner.setusetext( "You turn the burner on, and it slowly heats up." ); while( true ){ // Ghetto commands system.. getline( cin, cinline ); if ( cmd.getcmd(cinline) == "open" ){ cout << cmd.getarg( "open", cinline ) << endl; }else if ( cmd.getcmd(cinline) == "take" ){ // i need the cmd.getarg to somehow link back to the item's UID, and that would go back to the 'item <whatever arg>' player.take( cmd.getarg( "take", cinline) ); }else{ cout << "Invalid command!" << endl; }; }; // So it doesn't close incase we pass the while loop. system("PAUSE"); }; [/code] The important line i'm talking about is [code] player.take( cmd.getarg( "take", cinline) ); [/code]
The way you are currently doing it, you'd need to check what was taken in the character::take function. If I were you, I'd have the different items as different classes that all inherit from a base item class, and have the player's inventory just be a std::list of items. Also, Look into what class constructors and destructors are. If you have anymore simple questions, you should post in the very active help thread. [url]http://www.facepunch.com/threads/1092921[/url]
Sorry, you need to Log In to post a reply to this thread.