I posted this problem in another thread, but the thread had been marked as solved for some reason. In my code, I have the user input a command, and it does stuff based on the command, and if the command is illegal, it says "Nope. Try Again." This is the function that does all that:
The first time it goes through the while (pEnemy->m_eCHp > 0 && pHero->m_hCHp > 0) loop, it displays the phrase "Nope. Try Again." before it allows for user input. I was just wondering why it does this, and how I can fix it.
1. Umm... can you explain that please DX
2. Alrighty, but shouldn't it delete automatically when the function ends? O.o
3 What do you mean? It is in the attack method O_o
1. When you read input from the user, they press enter when they are done. This is the '\n' character, and is left in the buffer, unread, when you get data from them. The next read sees this character first, and ends the input there.
2. No. What you manually new, you must manually delete.
3. No, it isn't. The attack method is called right above that line. The HP editing should happen inside that function call.
Oh, wait, I see what you mean. I thought you were talking about both of the lines put together werent in the attack method. And it's because the function is only trying to calculate the damage done, not actually fooling around with any stats :P
Good morning. XD
I don't think there can be any residual '\n' character in the string buffer, as it is re-declared every time entering the loop.
Since your code works fine to other people, I'd suggest you check your build, IDE, etc., maybe create a new empty project, copy over the source, and rebuild the whole thing.
It didnt work, and putting it right after it (which I would have guessed would have solved the problem I had, but made many more arise) didnt change anything either...
Maybe a cheap solution, but you can replace the "Attack" and "Run away" strings with 'A' and 'R' chars, and use
1 2
char cin;
cin >> action;
No risk of having newlines or anything else mess up your comparisons.
In any case: have you ever tried checking with your debugger to see what "action" contains? If there are any "random" characters in there, it should be easy to check. Or, if you're too lazy to debug, just have it output the thing itself. You'll immediately see if there's any newlines in there.
'pointerdot2.exe': Loaded 'C:\Users\Lisa\Desktop\C++ Projects ;D\pointerdot2\Debug\pointerdot2.exe', Symbols loaded.
'pointerdot2.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
'pointerdot2.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
'pointerdot2.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
'pointerdot2.exe': Loaded 'C:\Windows\SysWOW64\msvcp100d.dll', Symbols loaded.
'pointerdot2.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
The thread 'Win32 Thread' (0x25b4) has exited with code -1073741510 (0xc000013a).
The program '[3540] pointerdot2.exe: Native' has exited with code -1073741510 (0xc000013a).
I have no idea what that means though. I tried outputting action right after the getline line, but the console looked no different
That's not the debugger, that's your output window.
Any chance you're using MSVisual Studio/C++? Try clicking in the margin left of the code (thin white line between your actual code text and the next window (eg. Solution Explorer). You'll see a red dot appear. This means that when you run the code, it will stop before executing the line of the red dot.
If you put such a dot on line 28 (after your getline() line), you can see the current value of "action".
Make sure to run code by pressing F5, not ctrl+F5 (and not to be running in release mode but I'm guessing that means nothing to you so just ignore it for now). It should stop running at a breakpoint, which allows you to switch back to MSVStudio/C++ and access the Watch window. There, you can add variables to "watch", which means you'll be able to see their value at the moment of breaking.
Wait, so in the watch window, i put the name of the variable, then the value, then the type, like I would put action, Attack, and string? And what am I watching for after that?
Oh wait, I think I got it now. The watch window lets you type in the name of a variable, and it shows you its value and type, right? If so, then the value is described as "" with nothing...
If what I'm assuming is correct, then I guess its skipping the getline line completely at first, because the console goes to the debug screen before I can even input anything, even though the break point is after it