getline help

Pages: 12
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...
I dunno if i'm being annoying by doing this so soon, but bump .-.
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.
But, shouldnt it try to do that before it goes on to other parts of the code?
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
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"
Anybody? DX
Your logic seems messed up. Assuming the only acceptable commands are "Attack" and "Run Away", here's the code:
1
2
3
4
5
6
7
8
9
10
11
string action;
do
{
  getline(cin, action);

  if (action == "Attack" || action == "Run Away")
    cout << action << endl;

  else
    cout << "Nope. Try Again." << endl;
} while (action != "Attack" && action != "Run Away");
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
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
But I dont input anything, much less anything illegal, yet it still displays it. Do you want to see the entire function?
If you don't input anything, a NULL character is loaded into action, which is ILLEGAL. =)
then how do I keep it from doing that DX
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.
Wait, what? No, I'm saying that before it even asks me for input, it displays the "Nope" sentence
Post that particular segment of codes here so that I can take a look. Shouldn't be a big problem.
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");
Works totally fine to me. I think you have some misunderstanding of the semantics, but I don't know which part.
Maybe you tend to hit the Enter button every time before inputting a string?.. XD
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;
}
}
Pages: 12