Prev: reasoning of ::std::cout (ostream) conversion w.r.t. plain, signed and unsigned chars...
Next: reasoning of ::std::cout (ostream) conversion w.r.t. plain, signed and unsigned chars...
From: DarioProgramer on 11 Jan 2010 02:22 On Jan 10, 2:35 pm, Francis Glassborow <francis.glassbo...(a)btinternet.com> wrote: > Francis Glassborow wrote: > > DarioProgramer wrote: > >> I am teaching my self C++ and this program is very simple, but I do > >> not understand why the cin.getline() command is not working. Thank you > >> for your help! > > > It is but not the way you expect because the immediately previous use of > > cin has not extracted the most recent carriage return as well as any > > white space that may have proceeded it. getline90 extracts all up to and > > including the next carriage return. Think about it. > > > Your code also make assumptions that are dangerous. For example, it will > > incorrectly handle the response from someone living in New York for two > > reasons. It will enter undefined behaviour if I use it (check the number > > of letters in my surname) > > > C++ provides a safer and IMO much better way to handle words, names etc. > > Replace the char arrays with string and your program will be much more > > robust. Deal with the problem that responses may have white space in > > them (as for New York). Indeed every one of your questions asking for a > > text response could result in a response with embedded white space. > > > Check out std::getstring() > > Sorry that should have been: > > std::getline(std::istream&, std::string&) > Sorry if I wasted anyone's time. Thank you all for your help! I have not tried this yet, I will try it tonight. The reason I did not know about String or wstring is that I am using a book to do these problems. In the book they did not say anything about strings, but use only char. I have a feeling I will learn a lot from this forum! Thank you again. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Francis Glassborow on 11 Jan 2010 05:03
DarioProgramer wrote: > Thank you all for your help! > I have not tried this yet, I will try it tonight. > The reason I did not know about String or wstring is that I am using a > book to do these problems. > In the book they did not say anything about strings, but use only > char. > I have a feeling I will learn a lot from this forum! > Thank you again. > > You might also ask what books we would suggest. Books that have you using antique mechanisms inherited from C rather than C++'s own methods may not be helping you as much as they should. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |