From: DarioProgramer on
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!

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
const int SIZE=20;
int age;
char name[8], cityName[6], profession[14], animal[10], petName
[12];
char collegeName[SIZE];

cout << "Enter your name: ";
cin >> name;
cout << "Enter your age: ";
cin >> age;
cout << "Enter a city name: ";
cin >> cityName;
cout << "Enter name of a college: ";
cin.getline(collegeName,SIZE);
cout << "Enter a profession: ";
cin >> profession;
cout << "Enter an animal: ";
cin >> animal;
cout << "Enter a pet's name: ";
cin >> petName;

cout << "\n\n\nThere once was a person named " << name << " who
lived in " << cityName << ".\n";
cout << "At the age of " << age << ", " << name << " went to
college at " << collegeName << ".\n";
cout << name << " graduated and went to work as a " << profession
<< ".\n";
cout << "Then, " << name << " adopted a(n) " << animal << " named
" << petName << ".\n";
cout << "They both lived happily ever after!" << endl;

return 0;
}

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: ag_123 on
(I think this is more appropriate to alt.comp.lang.learn.c-c++)

On Jan 9, 5:20 pm, DarioProgramer <oiradran...(a)bellsouth.net> 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!

If you are an absolute beginner, it would serve you better down
the road to start using string now, instead of char. Here's a
version of your code that uses strings, and in which you don't
need to use getline.

-----

#include <iostream>
#include <string>

using std::string;
using std::cin;
using std::cout;
using std::endl;

int main()
{
int age;
string name, cityName, profession, animal, petName, collegeName;

cout << "Enter your name: ";
cin >> name;
cout << "Enter your age: ";
cin >> age;
cout << "Enter a city name: ";
cin >> cityName;
cout << "Enter name of a college: ";
cin >> collegeName;
cout << "Enter a profession: ";
cin >> profession;
cout << "Enter an animal: ";
cin >> animal;
cout << "Enter a pet's name: ";
cin >> petName;

cout << "\nThere once was a person named " << name
<< " who lived in " << cityName << "." << endl
<< "At the age of " << age << ", " << name
<< " went to college at " << collegeName << "." << endl
<< name << " graduated and went to work as a "
<< profession << "." << endl
<< "Then, " << name << " adopted a(n) " << animal
<< " named " << petName << "." << endl
<< "They both lived happily ever after!" << endl;

return 0;

}

------

Regarding getline: this is used to read the input an entire line at
a time from an input stream. Assuming this is what you want to do,
you can say:

getline(cin,s)

after you have already written

using std:getline;

and

string s;

This string, s, will now contain a whole line of input, not including
the newline.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Francis Glassborow on
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()
>
> #include <iostream>
> #include <iomanip>
> using namespace std;
>
> int main()
> {
> const int SIZE=20;
> int age;
> char name[8], cityName[6], profession[14], animal[10], petName
> [12];
> char collegeName[SIZE];
>
> cout << "Enter your name: ";
> cin >> name;
> cout << "Enter your age: ";
> cin >> age;
> cout << "Enter a city name: ";
> cin >> cityName;
> cout << "Enter name of a college: ";
> cin.getline(collegeName,SIZE);
> cout << "Enter a profession: ";
> cin >> profession;
> cout << "Enter an animal: ";
> cin >> animal;
> cout << "Enter a pet's name: ";
> cin >> petName;
>
> cout << "\n\n\nThere once was a person named " << name << " who
> lived in " << cityName << ".\n";
> cout << "At the age of " << age << ", " << name << " went to
> college at " << collegeName << ".\n";
> cout << name << " graduated and went to work as a " << profession
> << ".\n";
> cout << "Then, " << name << " adopted a(n) " << animal << " named
> " << petName << ".\n";
> cout << "They both lived happily ever after!" << endl;
>
> return 0;
> }
>

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Jens Schmidt on
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!

First:
Well done to give a complete example program where the problem is shown.

Now to the problems:
Your example contains a lot of stuff that has nothing to do with the
problem. Please try to cut down your examples to the minimum size where
the problem still happens. This reduces the processing time for all the
readers here and therefore will get you more and better answers. Even
better: often the problem becomes so small that you yourself will find
the answer. I expect this may well be the case here, at least very close.

Next, you just say "is not working". Please state what makes you think
so. Usually this is done by giving some example input, the resultant
output and what you don't like in the output. Without that, we can only
guess.

Now, here my guess: You entered some collegeName, but got empty output
for that. The reason for this is that getline() is working as specified:
read everything up to the next line end or until the destination space
(one less than the size provided) is exhausted and deposit it into the
destination. Then append a '\0' character to the destination and discard
the line terminator.

The problem with that is: previous operator>> left a line end as the
next character to read. So getline() immediately is done with its reading.
Solution: Use getline() for all your data.

There are other problems in your code:
You use fixed length buffers for your data, and even very small lengths.
This is the stuff security nightmares are made of.
Just forget about char arrays and use std::string or even better
std::wstring for that. These are higher level and avoid some common
traps.

You don't check for errors. Each operator>> may run into the end of
input, after that every further read will fail and your variables are
unitialized. Printing them results in undefined behaviour.

A similar problem will happen if something not numeric is entered for
the age.
--
Greetings,
Jens Schmidt


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Francis Glassborow on
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.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]