Any C++ geeks here?[views:3408][posts:18]______________________________ [May 2,2006 5:18pm - eddie ""] i have a bit of code that should work that doesn't, me and my teacher have been puzzling over it for 2 classes now. i have a while loop that contains 2 if statements and after that i have a call to a structure. The problem is the while loop completely skips over the structure and just continues to repeat the if statements. there is no problem with the structure, because i use it in the program already and it works fine. the spelling is also correct, almost all obvious problems that could be wrong with it i've gone over at least 4-5 times. It's really basic C++ and i need new ideas to try and fix it. I'd post the code but it's about 19 pages worth. I'm making a text rpg for a final. I'll post the segment if anyone thinks they can help. |
__________________________________ [May 2,2006 5:44pm - ShadowSD ""] eddie said:i have a bit of code that should work that doesn't, me and my teacher have been puzzling over it for 2 classes now. i have a while loop that contains 2 if statements and after that i have a call to a structure. The problem is the while loop completely skips over the structure and just continues to repeat the if statements. Logically, I would assume that the structure is skipped over because the call to it never occurs, which means that the if condition which leads to the call is never satisfied. The first approach would be to look at the code and test whether the condition in that if statement is ever true, for example by changing the second half of it to print a character on the screen when the condition is true. If the character doesn't ever print when you run the program, there's your problem: the if condition is never true, so the function is never called. If it does print the character on the screen, the problem must be something else (mostly likely a simple, hard to see typo hidden somewhere) That's my best guess without seeing the code. |
_____________________________________ [May 2,2006 5:47pm - the_reverend ""] you can't use gdb? |
_________________________________ [May 2,2006 6:19pm - Aegathis ""] Ive been getting a few C+'s latly, but by no means does that make me a geek :spineyes::spineyes: |
_____________________________________ [May 2,2006 6:24pm - the_reverend ""] I've been coding c++ since at least 1995. |
_____________________________________ [May 2,2006 7:09pm - DertoxiaNLI ""] I would say check to make sure your have compound if statements |
_____________________________ [May 2,2006 8:36pm - nick ""] programming with c++ is like building a space station out of legos and pipe cleaners. |
______________________________ [May 2,2006 8:45pm - eddie ""] gdb? is that some type of check software? it can't be a spelling error because if that were true the program i'm using wouldn't allow it to work at all. The program runs it jsut doesn't do what i want and skips over the call structure. |
______________________________ [May 2,2006 8:46pm - eddie ""] H.ehp1=8; while((H.hp>=1) && (H.ehp1>=1)) { getch(); //enemy attack srand(time(0)); y=rand()%10+1; if(y>=4) { cout<<"he bites you and takes away 2 health points"<<endl<<endl; cout<<"------------------------------------------------"<<endl<<endl; H.hp=H.hp-2; } if(y<=3) { cout<<"the Chupacabra screems in fury, and the top of it's lungs"<<endl; cout<<"It sucks your blood and cuts your health in half"<<endl<<endl; cout<<"------------------------------------------------"<<endl<<endl; H.hp=H.hp/2; } cout<<"your hp has dropped too:"<<H.hp<<endl<<endl; cout<<"------------------------------------------------"<<endl<<endl; //use func your attack H=fight1_sequence(H); } |
______________________________ [May 2,2006 8:48pm - eddie ""] the while loop works because you die after a number of attacks, The structure is skipped over and it pisses the living hell out of me. |
__________________________________ [May 2,2006 9:01pm - ShadowSD ""] From what line to what line is skipped? |
_____________________________________ [May 2,2006 9:03pm - the_reverend ""] gdb = linux debugger. you can't set a break point in there? what are you using to compile this? just set a breakpoint at: y=rand()%10+1; and see what y gets set to. or, if you don't have a debugger, print out y below the line I just pasted. also, if(y<=3) change this to else since there is no reason to hit both the first if and the second if. |
__________________________________ [May 2,2006 9:05pm - ShadowSD ""] Oh wait, I get it, only one line is skipped... the enemy attack, right? |
__________________________________ [May 2,2006 9:07pm - ShadowSD ""] I now realize I read the original problem wrong, listen to Rev... |
_____________________________________ [May 2,2006 9:13pm - the_reverend ""] I've been writing software since I was like 7. that was 1982-3-ish. |
______________________________ [May 2,2006 9:23pm - eddie ""] ShadowSD said:Oh wait, I get it, only one line is skipped... the enemy attack, right? no, H=fight1_sequence(H); is skipped the srand and if work fine i'm gonna try everything rev suggested tomorrow in school, i don't have a compiler on my home computer. rev i'm using turbo C++ and some other dos based compiler in school. i don't have any idea what a break point is, but i'll be sure to find out. |
__________________________________ [May 2,2006 9:32pm - ShadowSD ""] I started programming Basic on my Atari in early 1984. I was 4 at the time. Of course, Basic is useless, and the languages I learned later on (C, Javascript, a tiny bit of C++) I haven't used in years, so I'm actually totally incompetent at this point; without some serious refreshment, I couldn't write an interactive program for you right now in any language. I only tried to answer the question because I've always had a good mind for the logic of it, and in that sense all computer languages are the same (it always helps to read the question right, though). In conclusion, listen to Rev... |
__________________________________ [May 2,2006 9:39pm - ShadowSD ""] eddie said:ShadowSD said:Oh wait, I get it, only one line is skipped... the enemy attack, right? no, H=fight1_sequence(H); is skipped Oh, right, the enemy attack was just a note for the programmer, whereas as the above line is the name of the function (structure) being called with the variable being sent to the function in the paranthesis. Of course, it's all coming back to me now... |
_____________________________________ [May 3,2006 4:09pm - the_reverend ""] turbo c++? geez.. I used that back in college. that's wicked old. right now, I use MSDN 2003. I was using 2005, but the MFC 2.0 aren't out of beta so most people only have MFC 1.1 so.. put a break point here: H=fight1_sequence(H); and step inside it, see what it's doing. if you are command line on a linux system, do a man gdb it's a gnu debugger. a "break point" is just a point at which your program is stopped by a debugger. then you can check values, evaluate and change variables. I'm knee deep in a java debugger right now cause I can't get this stupid abstract panel to be loaded when it's underlying UML model is update and it's components are reloaded. no that makes no sense~! anyhow, ShadowSD: javascript is not a programing language, it's a scripting language. BASIC is a interpreted language, but those are basically scripting laguages. on a typical day, I use: C++, C#, java, and php(w/ jacavript, vbscript, mysql, html), but my job requires me to know perl, tcl/tk, VB, asp/x/a, C, c-script, and anything else that might annoying come up. eddie: from your code, there are a million easier ways to do this. 19 pages of code is too long. if I were doing it, I would make a Model and then store my rules/data inside a XML file. then you just have to jump through a XML file and your program would interpret what to do. sort of like a pick-a-path with reasoning, data, and whatever else in a XML node. If you really get stuck, I could debug the code in MSDN I'm sure. |