Skipping input?

Pages: 12
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
int battlePhase(Hero* pHero)
{
	srand(clock() * (unsigned int) time (0));
	int random = rand();

	int encounter = (random % 2) + 1;
	Enemy* pEnemy;
	if (encounter == 1)
	{
		pEnemy = new Mouse();
	}
	else if (encounter == 2)
	{
		pEnemy = new Rat();
	}

	cout << "Wild ";
	pEnemy->Name();
	cout << " appeared!\n";
	while (pEnemy->m_eCHp > 0 && pHero->m_hCHp > 0)
	{
		cout << "What will you do?\n";
		cout << "\nAttack\nRun Away\n\n";
		string action;
		do
		{
			getline(cin, action);
			if (action == "Attack" || action == "Run Away")
			{
				break;
			}
			else
			{
				cout << "Nope. Try Again." << endl;
			}
		} while (action != "Attack" && action != "Run Away");

		if (action == "Run Away")
		{
			cout << "You ran away.\n";
			break;
		}

		if (pHero->m_hSpd >= pEnemy->m_eSpd)
		{
			int damage = pHero->performAttack(pEnemy);
			pEnemy->m_eCHp = pEnemy->m_eCHp - damage;
			pHero->Name();
			cout << " dealt " << damage;
			cout << " damage to ";
			pEnemy->Name();
			cout << endl;
			
			if (pEnemy->m_eCHp <= 0)
			{
				break;
			}

			damage = pEnemy->performAttack(pHero);
			pHero->m_hCHp = pHero->m_hCHp - damage;
			pEnemy->Name();
			cout << " dealt " << damage;
			cout << " damage to ";
			pHero->Name();
			cout << endl;
		
			if (pHero->m_hCHp <=0)
			{
				break;
			}

			cout << "\nYour Hp: " << pHero->m_hCHp;
			cout << "\nEnemy Hp: " << pEnemy->m_eCHp << endl;
		}
		else if (pHero->m_hSpd < pEnemy->m_eSpd)
		{
			int damage = pEnemy->performAttack(pHero);
			pHero->m_hCHp = pHero->m_hCHp - damage;
			pEnemy->Name();
			cout << " dealt " << damage;
			cout << " damage to ";
			pHero->Name();
			cout << endl;
		
			if (pHero->m_hCHp <=0)
			{
				break;
			}

			damage = pHero->performAttack(pEnemy);
			pEnemy->m_eCHp = pEnemy->m_eCHp - damage;
			pHero->Name();
			cout << " dealt " << damage;
			cout << " damage to ";
			pEnemy->Name();
			cout << endl;

			if (pEnemy->m_eCHp <= 0)
			{
				break;
			}

			cout << "\nYour Hp: " << pHero->m_hCHp;
			cout << "\nEnemy Hp: " << pEnemy->m_eCHp << endl;
		}
	}
	if (pEnemy->m_eCHp <= 0)
	{
		cout << pEnemy->m_eName << " has been defeated!\n";
		cout << "You gained " << pEnemy->m_eExpDrop << " Exp!\n";
		Exp += pEnemy->m_eExpDrop;
		cout << "You gained " << pEnemy->m_eGoldDrop << " Gold!\n";
		gold += pEnemy->m_eGoldDrop;
	}
	else if (pHero->m_hCHp <= 0)
	{
		cout << "You died\n";
	}
	else
	{
		cout << endl;
	}
				
	return pHero->m_hCHp;
}


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.
Maybe you left an '\n' in the input buffer. (by doing a cin>>var;)
Also, don't forget to delete what you allocate.

By the way
1
2
int damage = pHero->performAttack(pEnemy);
pEnemy->m_eCHp = pEnemy->m_eCHp - damage; //¿why isn't this inside the Attack method? 
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.
1. But wouldnt that depend on the user? Im saying this happens every time, and only at that one point.

2. Okie Dokie :P

3. Ok, what are you referring to when you say attack method?
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
Any more help with this?
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.
Last edited on
Well, the build says nothing is wrong, and it does work completely fine, except for an out of place line O_o I'll try that though.

It still does it DX
Last edited on
Try adding cin.clear(); right before line27.
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...
Still no help for this? Also, I tried compiling it in a different compiler, and it still happened
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.
this is what the debug gives me:

'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".
I thought the output window is what appears when you run the program O_o

Yeah, thats what I'm using, but the red dot doesnt seem to be doing anything. The program still runs the same...
The black window is the console. It's not because you use it to print output that it becomes the output window.

Aaaanyhoo, check here:
http://msdn.microsoft.com/en-us/library/4607yxb0(v=VS.100).aspx (Breakpoints)
http://msdn.microsoft.com/en-us/library/0taedcee.aspx (Watch)

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
Odd...

Try putting cout << action; to see if it does anything.
Pages: 12