Something wrong with cin/sstream

I made a code that had a y/n option in it. I was wondering why after I press 'n', instead of displaying

Enter name: (waits for person to input value for Name)
Enter class: (waits for person to input value for Class)
Enter attack: (waits for person to input value for atk)
Enter defense: (waits for person to input value for def)

which it did in main, it displays

Enter name: Enter class: (waits for person to input value for Class)
Enter attack: (waits for person to input value for atk)
Enter defense: (waits for person to input value for def)

instead.

It still displays the previous value of "Name" the second time around. It didn't give the person a chance to input a new value for Name, skipping to Class instead. And it prints "Enter name: Enter class" on the same line, even though I did not use \n or endl. Any suggestions?


-----------------------------------------------------------------------------


#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);
}

You shouldn't mix getline and cin. getline by default reads until a newline is found('\n') and cin leaves a newline in the buffer. To fix this, you are going to want to ignore anything left in the buffer from cin. with std::cin.ignore http://www.cplusplus.com/reference/istream/istream/ignore/?kw=cin.ignore

Another thing to mention for your editInfo function you should be passing by reference(&) if you wish to modify the variables. By the way why are you streaming to the "guildmember" object. You should read directly into the guildmember object.
Last edited on
Topic archived. No new replies allowed.