Hmmm... My guess is that it works when your hero's name, weapon and shield have no space characters. And it doesn't work when at least one of these variables has at least one space character. Check your previous topic and see what Galik and R0mai said about getting strings.
#include <iostream>
#include <string>
#include <sstream>
usingnamespace std;
void input_f1(string & str, int & num);
void input_f2(string & str, int & num);
void input_f3(string & str, int & num);
void clear_cin();
int main()
{
string str;
int num;
cout << "enter a string and a number ""(in separate lines):\n";
input_f1(str,num);
cout << "\nyou entered:\n" << str << '\n' << num << endl;
clear_cin();
cout << "\nenter another string and a number ""(in separate lines):\n";
input_f2(str,num);
cout << "\nyou entered:\n" << str << '\n' << num << endl;
clear_cin();
cout << "\nenter yet another string and a number ""(in separate lines):\n";
input_f3(str,num);
cout << "\nyou entered:\n" << str << '\n' << num << endl;
cout << "\nhit enter to quit...";
cin.get();
return 0;
}
void input_f1(string & str, int & num)
{
cin >> str;
cin >> num;
}
void input_f2(string & str, int & num)
{
getline(cin,str);
cin >> num;
}
void input_f3(string & str, int & num)
{
string input;
getline(cin,str);
getline(cin,input);
istringstream(input)>>num;
}
void clear_cin()
{
cin.clear();
while (cin.get()!='\n');
}
Try entering strings containing spaces and see the difference between f1 and f2. f3 is a somewhat more sophisticated version of f2; it doesn't affect your input stream's status.