Why doesn't the loop work?

I am trying to figure out data structures right now, so I decided to create a testrun program to try struct out. The problem is, I want this program to loop over and over again. But if I enter "n" for no, there is a slight error. The "enter name" and "enter class" are on the same line, with no way to change the value of "name."

Also, what does getline and stringstream do exactly, and what are the benefits of using it? Should I use it here, or is there a better way to input data?

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct guildmember {
string Name;
string Class;
int atk;
int def;
};
guildmember NewChar;

void printstats (string, string, int, int);
void editinfo (string, string, int, int);

int main ()
{
string mystr;

cout << "Enter name: ";
getline (cin,mystr);
stringstream(mystr) >> NewChar.Name;
cout << "Enter class: ";
getline (cin,mystr);
stringstream(mystr) >> NewChar.Class;
cout << "Enter attack: ";
getline (cin,mystr);
stringstream(mystr) >> NewChar.atk;
cout << "Enter defense: ";
getline (cin,mystr);
stringstream(mystr) >> NewChar.def;
cout << "\n";
printstats (NewChar.Name, NewChar.Class, NewChar.atk, NewChar.def);

cout << "Is this correct? (Y/N) ";
char n;
cin >> n;
cout << "\n";

if (n == 'n' || n == 'N')
editinfo (NewChar.Name, NewChar.Class, NewChar.atk, NewChar.def);

return 0;

}

void printstats (string Name, string Class, int atk, int def)
{
cout << "Your name is " << NewChar.Name << "\n";
cout << "Your class is " << NewChar.Class << "\n";
cout << "Your attack is " << NewChar.atk << "\n";
cout << "Your defense is " << NewChar.def << "\n";
}

void editinfo (string Name, string Class, int atk, int def)
{
string mystr;

cout << "Enter name: ";
getline (cin,mystr);
stringstream(mystr) >> NewChar.Name;
cout << "Enter class: ";
getline (cin,mystr);
stringstream(mystr) >> NewChar.Class;
cout << "Enter attack: ";
getline (cin,mystr);
stringstream(mystr) >> NewChar.atk;
cout << "Enter defense: ";
getline (cin,mystr);
stringstream(mystr) >> NewChar.def;
cout << "\n";
printstats (NewChar.Name, NewChar.Class, NewChar.atk, NewChar.def);

cout << "Is this correct? (Y/N) ";
char n;
cin >> n;
cout << "\n";

if (n == 'n' || n == 'N')
editinfo (NewChar.Name, NewChar.Class, NewChar.atk, NewChar.def);
}
Topic archived. No new replies allowed.