Getline headache
Nov 8, 2013 at 3:56am UTC
I've got a class below that seems to skip the first getline call and I can't figure out why. All that is in the main function is the include for this class a call to the only function in it(mapgen).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#ifndef MAP_H
#define MAP_H
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class map
{
public :
char ** cmap;
int gridx;
int gridy;
string mapline;
bool mapgen()
{
mapline = "waffle" ; // a test value
cout << "x: " ;
cin >> gridx;
cout << endl <<"y: " ;
cin >> gridy;
cout << endl << endl << "x: " << gridx << " and y: " << gridy << endl << endl;
if (gridx == 0){return false ;}
for (int i = 0; i < gridy; i++)
{
cout << "map row:" << endl;
getline(cin,mapline); //this seems to be skipped when i = 0
cout << endl << "map report: " << mapline;
/*for(int j = 0; j < gridx; j++)
{
cmap[i][j] = mapline[j];
} */
}
return true ;
}
};
#endif
The output I get looks like this, with input in italics:
x:
3
y:
3
x: 3 and y: 3
map row:
map report: map row:
aaa
map report: aaamap row:
bbb
map report: bbb
x: (...)
Thanks for your time and assistance.
Nov 8, 2013 at 3:59am UTC
add cin.ignore()
after cin >> gridy;
Nov 8, 2013 at 6:52pm UTC
Thank you!!!
Topic archived. No new replies allowed.