Nov 21, 2011 at 7:35pm UTC
Ok, so the way I understand it, getline is used to get user input that may contain spaces, but I'm not entirely sure of how it works. I tried looking it up on this website, but in my code, it kinda skips past it and goes back to it.
like:
string word;
getline (cin, word);
cout << "display text\n";
that displays the output, and then asks for input. I know I'm doing something wrong, but I dont know what...
Nov 21, 2011 at 8:47pm UTC
I dunno if i'm being annoying by doing this so soon, but bump .-.
Nov 21, 2011 at 10:03pm UTC
After the getline(cin, word);
, the first line you input at console gets loaded into the string word
. Then you can output word
using cout
.
Nov 21, 2011 at 10:22pm UTC
But, shouldnt it try to do that before it goes on to other parts of the code?
Nov 21, 2011 at 11:08pm UTC
Alright. Try This:
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
#include <string>
using namespace std;
int main (void )
{
string word;
getline (cin, word);
cout << word;
return 0;
}
If the problem still continues, please show all of your syntax(if any).
Last edited on Nov 21, 2011 at 11:08pm UTC
Nov 22, 2011 at 5:13am UTC
string action;
do
{
getline(cin, action);
cout << action;
if (action == "Attack")
{
break;
}
else if (action != "Run Away")
{
cout << "Nope. Try Again.\n";
cin >> action;
}
}while (action != "Attack" && action != "Run Away");
This displays "Nope. Try Again." and then asks me for input, when i type "Run Away", it only displays " Away" and continues to say "Nope. Try Again"
Nov 22, 2011 at 6:56pm UTC
Thanks for that, you got my Run Away command up and running :P I dont know what I was thinking when I typed that up xD I still have it displaying "Nope. Try Again." the first time it goes through the loop though O_o
Nov 22, 2011 at 7:04pm UTC
The "Nope" sentence won't be displayed unless your input action is illegal (i.e. neither "Attack" nor "Run Away"), since the "if else" statement is mutually exclusive.
Last edited on Nov 22, 2011 at 7:05pm UTC
Nov 22, 2011 at 7:19pm UTC
But I dont input anything, much less anything illegal, yet it still displays it. Do you want to see the entire function?
Nov 22, 2011 at 7:38pm UTC
If you don't input anything, a NULL character is loaded into action, which is ILLEGAL. =)
Nov 22, 2011 at 7:43pm UTC
then how do I keep it from doing that DX
Nov 22, 2011 at 7:54pm UTC
Any reason you don't want to input a command? If there is such a circumstance, you might need a "if else" statement before the loop.
Nov 22, 2011 at 7:58pm UTC
Wait, what? No, I'm saying that before it even asks me for input, it displays the "Nope" sentence
Nov 22, 2011 at 8:04pm UTC
Post that particular segment of codes here so that I can take a look. Shouldn't be a big problem.
Nov 22, 2011 at 8:08pm UTC
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");
Nov 22, 2011 at 8:16pm UTC
Works totally fine to me. I think you have some misunderstanding of the semantics, but I don't know which part.
Nov 22, 2011 at 8:18pm UTC
Maybe you tend to hit the Enter button every time before inputting a string?.. XD
Nov 22, 2011 at 8:33pm UTC
No, I don't think thats it... maybe it has something to do with something else that surrounds it... is there anything wrong with this?:
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;
}
}