how to properly use getline (cin, mystr)
Aug 20, 2010 at 8:54am UTC
I've tried using the tutorial for this (http://cplusplus.com/doc/tutorial/basic_io/) but I can't get it to work.
I need (of course) a string with spaces in it as a variable, so the regular
cin
would not work. When I use this my program doesn't stop to let me type in a variable.
Here's the bit I'm focusing on
1 2 3 4 5 6 7
void cTITLE (void )
{
string mystr;
cout << "Choose the title of your file" ;
getline (cin, mystr);
TITLEw = mystr;
}
I first tried using
mystr
as
TITLEw
but for consistency with the tutorial I used the
mystr
and then just made the
TITLEw
equal to
mystr
.
TITLEw
is a global string variable.
What am I doing wrong?
Thanks.
Last edited on Aug 20, 2010 at 8:56am UTC
Aug 20, 2010 at 10:29am UTC
A little compilable file (or a bit more of your code/ the full code) would help :)
I see nothing wrong with this code, so it could be some problem in your main or any other function..
Aug 20, 2010 at 11:05am UTC
If getline() doesn't wait for you to type in the input then there could be some input already waiting in the buffer from something you previously typed. Maybe just the return key.
Aug 20, 2010 at 4:23pm UTC
search the forums using the words "getline" and/or "ignore". There are probably 50 threads on this subject.
Sep 2, 2010 at 1:24am UTC
here is the full file:
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
// web_page_creator.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
using namespace std;
/* PREFIXES */
// c = create (ex. cDOCTYPE = creat DOCTYPE)
/* SUFFIXES */
// o = open tag (ex. HTMLo = HTML open tag)
// c = close tag (ex. HTMLc = HTML close tag)
// w = word(s)/content (ex. TITLEw = TITLE word(s)/content)
string DOCTYPE = "<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.1\/\/EN\" \"http:\/\/www.w3.org\/TR\/xhtml11\/DTD\/xhtml11.dtd\">" ;
string HTMLo = "<html xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\" lang=\"en\">" ;
string HTMLc = "<\/html>" ;
string HEADo = "<head>" ;
string HEADc = "<\/head>" ;
string TITLEo = "<title>" ;
string TITLEc = "<\/title>" ;
string TITLEw = "" ;
string METAw = "" ;
string BODYo = "<body>" ;
string BODYc = "<\/body>" ;
void cDOCTYPE (void )
{
int choice = 7;
cout << "Choose the DOCTYPE of your file" << endl;
cout << "1.\t HTML 4.01 Strict" << endl;
cout << "2.\t HTML 4.01 Transitional" << endl;
cout << "3.\t HTML 4.01 Frameset" << endl;
cout << "4.\t XHTML 1.0 Strict" << endl;
cout << "5.\t XHTML 1.0 Transitional" << endl;
cout << "6.\t XHTML 1.0 Frameset" << endl;
cout << "7.\t XHTML 1.1" << endl;
cin >> choice;
switch (choice)
{
case 1:
{DOCTYPE = "<!DOCTYPE HTML PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01\/\/EN\" \"http:\/\/www.w3.org\/TR\/html4\/strict.dtd\">" ; break ;}
case 2:
{DOCTYPE = "<!DOCTYPE HTML PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/html4\/loose.dtd\">" ; break ;}
case 3:
{DOCTYPE = "<!DOCTYPE HTML PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01 Frameset\/\/EN\" \"http:\/\/www.w3.org\/TR\/html4\/frameset.dtd\">" ; break ;}
case 4:
{DOCTYPE = "<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.0 Strict\/\/EN\" \"http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-strict.dtd\">" ; break ;}
case 5:
{DOCTYPE = "<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd\">" ; break ;}
case 6:
{DOCTYPE = "<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.0 Frameset\/\/EN\" \"http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-frameset.dtd\">" ; break ;}
case 7:
{DOCTYPE = "<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.1\/\/EN\" \"http:\/\/www.w3.org\/TR\/xhtml11\/DTD\/xhtml11.dtd\">" ; break ;}
default :
{DOCTYPE = "<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.1\/\/EN\" \"http:\/\/www.w3.org\/TR\/xhtml11\/DTD\/xhtml11.dtd\">" ; break ;}
}
}
void cTITLE (void )
{
string mystr;
cout << "Choose the title of your file" ;
getline (cin, mystr);
TITLEw = mystr;
}
void cMETA (void )
{
cout << "Choose the meta of your file" ;
cin >> METAw;
}
void cFile (void )
{
ofstream myfile;
myfile.open ("index.txt" );
myfile <<
DOCTYPE << "\n" <<
HTMLo << "\n" <<
HEADo << "\n" <<
TITLEo << "\n" << TITLEw << TITLEc << "\n" <<
HEADc << "\n" <<
BODYo << "\n" <<
BODYc << "\n" <<
HTMLc; /* Parts go here */
myfile.close();
}
int main()
{
cDOCTYPE();
cTITLE();
cFile();
return 0;
}
Topic archived. No new replies allowed.