file and cin.getline
Jan 17, 2010 at 8:37am UTC
hi i got a problem. i'm writing a tool that generates xml files with data,
but the only input that works is regular cin, and i need to use cin.getline()
here's my code:
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
//dir is the directory it will put the xml in. there's more code before this, but the black sheep is in this block
strcpy(file, dir);
strcat(file, "//quest.xml" );
{
ofstream questxml(file);
questxml << "<quests>\n" ;
bool bq;
while (!bq)
{
questxml << "<quest>\n" ;
char qname[50];
print1("enter the name of the quest" ,true );
cin >> qname;
questxml << "<name>" ;
if ( qname != NULL)
{
questxml << qname;
}
questxml << "</name>\n" ;
questxml << "<description>\n" ;
bool bd(true );
while (bd)
{
char description[256];
cin.getline(description, 256); //here's the black sheep
if (description != NULL)
{
questxml << description;
}
char yn;
cout << "do you want to enter more on the description? y/n " ;
cin >> yn;
switch (yn)
{
case 'y' :
bd = true ;
break ;
case 'n' :
bd = false ;
break ;
default :
break ;
}
}
questxml << "</description>\n" ;
char yn;
cout << "do you want to enter another quest? y/n" << endl;
cin >> yn;
switch (yn)
{
case 'y' :
bq = false ;
break ;
case 'n' :
bq = true ;
break ;
default :
cerr << "invalid input!!" ;
break ;
}
}
questxml << "</quest>" ;
questxml.close();
}
it compiles, but when it runs past
cin.getline(description, 256) , it doesn't wait for input, and just goes directly to the cout.
Jan 17, 2010 at 10:20am UTC
Jan 17, 2010 at 5:33pm UTC
this will require enormous rewriting!!
Jan 17, 2010 at 5:45pm UTC
You only need to replace the >> calls
Jan 18, 2010 at 3:38pm UTC
thank you for the help! it works now!
Topic archived. No new replies allowed.