Computer Science Canada

Reading Full Lines

Author:  Prince Pwn [ Sun Dec 10, 2006 11:00 pm ]
Post subject:  Reading Full Lines

Ok I understand cin and getline(cin), but there is a small problem in my code:
code:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#define SKIP '\n'

struct information
{
  string name,town,month;
  int day,year;
}
info;

int main ()
{
  cout << "Please enter your name: "; getline(cin, info.name);
  cout << "What town were you born in? "; getline(cin, info.town);
  cout << "What year were you born? "; cin >> info.year;
  cout << "What month were you born? "; cin >> info.month;
  cout << "What day were you born? "; cin >> info.day;
  cout << SKIP;
  cout << info.name << " was born in " << info.town << " on " << info.month << ' ' << info.day << ", " << info.year;
  return 0;
}


When I attempt to get info.month using getline(cin, info.month); it skips to asking the year they were born. And if I use cin >> info.month;, it cannot read the full string (i know months don't have spaces).

Any help here? Thanks.

Author:  Prince Pwn [ Sun Dec 10, 2006 11:02 pm ]
Post subject: 

I mean it skips and asks the day you we're born.

Author:  rdrake [ Sun Dec 10, 2006 11:14 pm ]
Post subject: 

Just out of curiosity, does the user enter in "December" or 12 for the month?

Author:  Prince Pwn [ Mon Dec 11, 2006 12:05 am ]
Post subject: 

December

Author:  rdrake [ Mon Dec 11, 2006 12:23 am ]
Post subject: 

Using your code as specified in your first post, I get this:
code:
rdrake@localhost ~ $ ./test
Please enter your name: rdrake
What town were you born in? Yellowknife
What year were you born? 2038
What month were you born? December
What day were you born? 05

rdrake was born in Yellowknife on December 5, 2038rdrake@localhost ~ $
Editing it I get...
code:
rdrake@localhost ~ $ ./test
Please enter your name: rdrake
What town were you born in? Yellowknife
What year were you born? 2038
What month were you born? What day were you born? 05

rdrake was born in Yellowknife on  5, 2038rdrake@localhost ~ $
Your first version seems to work fine for me... what compiler are you using? Since months don't have spaces in them, does it matter if you get the entire line? Can't you just get the first word before the space?

Author:  Prince Pwn [ Mon Dec 11, 2006 12:44 am ]
Post subject: 

Yeah I know, months don't have spaces, but if I were to ask for your father's full name it would mess up abit. I guess it's ok, just seems a bit odd that it would do that. I'm using Dev C++.

Author:  Prince Pwn [ Mon Dec 11, 2006 12:48 am ]
Post subject: 

Here run this, and see what I mean:
code:


#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#define SKIP '\n'

struct information
{
  string name,town,father;
  int day,year;
}
info;

int main ()
{
  cout << "Please enter your name: "; getline(cin, info.name);
  cout << "What town were you born in? "; getline(cin, info.town);
  cout << "What year were you born? "; cin >> info.year;
  cout << "What's your father's full name? "; getline (cin, info.father);
  cout << "What day were you born? "; cin >> info.day;
  cout << SKIP;
  cout << info.name << " was born in " << info.town << "and his father is " << info.father<< " on "  << ' ' << info.day << ", " << info.year;
  return 0;
}


The only thing I can think of is it messes up because I just asked for an int then a string.

Author:  r.3volved [ Mon Dec 11, 2006 1:48 pm ]
Post subject: 

Try cleaning and building again.
You might be currupting the cin buffer.
The >> may be leaving the carriage return in the cin buffer and the next time you use getline it is seeing a delimiter already in the buffer. Try flushing cin and use a string to grab any garbage data left in the buffer.

Author:  ownageprince [ Tue Jan 02, 2007 7:57 pm ]
Post subject: 

hey prince pawn just use:
cin.ignore(100,'\n');

it ignores next line characters for about 100. put it before or after the command that asks for month..i think its after month i forget..try it out n reply back.

Author:  abcdefghijklmnopqrstuvwxy [ Wed Jan 17, 2007 2:01 am ]
Post subject:  RE:Reading Full Lines

Is it true that structs are bad form in c++? Shouldn't they never be used in c++?
Only asking because of the use of a struct in prince's code.

Author:  wtd [ Wed Jan 17, 2007 2:40 am ]
Post subject:  RE:Reading Full Lines

If you know what structs in C++ actually are, then you won't need to ask that question. Smile

Author:  abcdefghijklmnopqrstuvwxy [ Wed Jan 17, 2007 3:16 am ]
Post subject:  RE:Reading Full Lines

What was the point of that post? It didn't answer my question and I even posted it in the "Help" section. Or maybe you thought this was the "We already know all the answers" section?

Author:  rdrake [ Wed Jan 17, 2007 10:46 am ]
Post subject:  Re: RE:Reading Full Lines

abcdefghijklmnopqrstuvwxy @ Wed Jan 17, 2007 3:16 am wrote:
What was the point of that post? It didn't answer my question and I even posted it in the "Help" section. Or maybe you thought this was the "We already know all the answers" section?
It was a simple post saying that basically you have to use structs first before you really understand them. If you use them and generally dislike them, then chances are they are not so great to use.

In calculus you don't decide to use the half-angle or double-angle formulas either until you really know what they are and need to use them.

Author:  wtd [ Wed Jan 17, 2007 1:36 pm ]
Post subject:  Re: RE:Reading Full Lines

rdrake @ Wed Jan 17, 2007 11:46 pm wrote:
abcdefghijklmnopqrstuvwxy @ Wed Jan 17, 2007 3:16 am wrote:
What was the point of that post? It didn't answer my question and I even posted it in the "Help" section. Or maybe you thought this was the "We already know all the answers" section?
It was a simple post saying that basically you have to use structs first before you really understand them. If you use them and generally dislike them, then chances are they are not so great to use.


Not quite.

Big word warning!

We are talking about C++, so the structs vs. classes thing is a false dichotomy. Anyone who has even the most elementary understanding of structs in C++ knows this. Anyone who does not should probably learn about them before commenting on their use in criticism of another.

Author:  ownageprince [ Wed Jan 17, 2007 6:26 pm ]
Post subject:  Re: Reading Full Lines

i agree with wtd..why bother with structs when you can easily make class with much more useful as they are reuseable..

Author:  md [ Wed Jan 17, 2007 7:12 pm ]
Post subject:  Re: Reading Full Lines

ownageprince @ Wednesday 2007-01-17 6:26 pm wrote:
i agree with wtd..why bother with structs when you can easily make class with much more useful as they are reuseable..

THAT'S NOT WHAT HE SAID.

Good god people. Wtd is saying that classes and structs are almost entirely the same. Either you know this, or you don't. And if you don't you should stop sprouting bullshit until you know what you are talking about.

Author:  abcdefghijklmnopqrstuvwxy [ Wed Jan 17, 2007 11:27 pm ]
Post subject:  RE:Reading Full Lines

I just asked a simple question. Is structs considered bad form in C++?

My confusion stems from seeing many C++ sources making use of a struct. I am not a C programmer migrating to C++, and I was told never to use structs so why would I be an expert on structs?

I just wanted to know what the consesus, if there is one, on using structs is... Can someone give me a straight answer?

Author:  ericfourfour [ Thu Jan 18, 2007 12:57 am ]
Post subject:  Re: Reading Full Lines

How about I give you 3 links and you can draw your own conclusions about structs?

www.cprogramming.com
www.cplusplus.com
www.gamedev.net (go to the articles/tutorials section)

Author:  md [ Thu Jan 18, 2007 2:49 am ]
Post subject:  RE:Reading Full Lines

There is nothing wrong with structs. Nothing. Anyone who tells you never to use them just doesn't know what they are talking about.

Author:  wtd [ Thu Jan 18, 2007 3:01 am ]
Post subject:  RE:Reading Full Lines

I would tend to agree with md.

Author:  abcdefghijklmnopqrstuvwxy [ Thu Jan 18, 2007 3:04 am ]
Post subject:  RE:Reading Full Lines

Okay, from reading wtd's post on structs in the tutorial section it sounds like structs have been modified in c++ so that they are exactly like classes except public by default. I had to read it twice to get that... At first I thought he was saying to avoid using structs...

So for the record, structs = classes except public by default? That's the only difference? (In c++)

Author:  wtd [ Thu Jan 18, 2007 3:47 am ]
Post subject:  RE:Reading Full Lines

Yes.

Author:  md [ Thu Jan 18, 2007 1:04 pm ]
Post subject:  RE:Reading Full Lines

That and struct is one letter longer then class when typing Wink

Author:  abcdefghijklmnopqrstuvwxy [ Thu Jan 18, 2007 8:45 pm ]
Post subject:  RE:Reading Full Lines

One letter longer? Golly, now I know why I've been avoiding structs all this time! Thank God for classes!

Author:  ownageprince [ Fri Jan 19, 2007 8:01 am ]
Post subject:  Re: Reading Full Lines

md @ Wed Jan 17, 2007 7:12 pm wrote:


Good god people. Wtd is saying that classes and structs are almost entirely the same. Either you know this, or you don't. And if you don't you should stop sprouting bullshit until you know what you are talking about.


they are not almost entirely the same...in classes you have public, private and protected this enables you to hide data from the user depending under which heading you put it under.

also you can't define functions in structs...so they are not almost entirely the same my friend..

Author:  abcdefghijklmnopqrstuvwxy [ Fri Jan 19, 2007 9:45 am ]
Post subject:  RE:Reading Full Lines

lol... you can declare public private or protected in c++ structs. you can define functions in c++ structs. I know that is what you meant to say Razz

Note: In C you cannot do those things, but in C++ you can. They changed struct for C++.

So as md said it: stop sprouting bull shit... lmao...

Author:  md [ Fri Jan 19, 2007 11:01 am ]
Post subject:  Re: Reading Full Lines

ownageprince @ Friday 2007-01-19 8:01 am wrote:
md @ Wed Jan 17, 2007 7:12 pm wrote:


Good god people. Wtd is saying that classes and structs are almost entirely the same. Either you know this, or you don't. And if you don't you should stop sprouting bullshit until you know what you are talking about.


they are not almost entirely the same...in classes you have public, private and protected this enables you to hide data from the user depending under which heading you put it under.

also you can't define functions in structs...so they are not almost entirely the same my friend..


You are wrong. Structs can contain public, private and protected data and functions. In C++ structs are the exact same thing as classes, except that structs are public by default. I would also really recommend knowing what you're talking about before you go saying that two of the more knowledgeable people are wrong; especially about a language that one (me) uses almost daily.

Author:  deville75 [ Fri Jan 19, 2007 11:15 am ]
Post subject:  RE:Reading Full Lines

Is a struct only used to create simple types?

Author:  Andy [ Fri Jan 19, 2007 12:48 pm ]
Post subject:  RE:Reading Full Lines

err what? clearly you dont know what structs are then.

Author:  wtd [ Fri Jan 19, 2007 12:56 pm ]
Post subject:  RE:Reading Full Lines

It's a shame there isn't a thread where people have recently had a fairly thorough discussion on this subject.

Author:  deville75 [ Fri Jan 19, 2007 1:14 pm ]
Post subject:  RE:Reading Full Lines

hmm yes it is.. I dream of a day when forums are used to help each other rather than criticize each other. When I post something that is incorrect, or just a question, it would be nice if someone were to help me rather than criticize.


An exampleof the wrong behaviour:
Quote:
err what? clearly you dont know what structs are then.


thanks a lot Andy Razz Working at NVidia may have increased your ego.

Author:  deville75 [ Fri Jan 19, 2007 1:17 pm ]
Post subject:  Re: RE:Reading Full Lines

Another example of destructive criticism:
wtd @ Wed Jan 17, 2007 2:40 am wrote:
If you know what structs in C++ actually are, then you won't need to ask that question. Smile


he doesn't know what structs are, which is clearly why he asked it...

Author:  wtd [ Fri Jan 19, 2007 1:51 pm ]
Post subject:  RE:Reading Full Lines

It is an example of constructive criticism, actually. It is pointing out that the wrong question is being asked.

Author:  deville75 [ Fri Jan 19, 2007 1:55 pm ]
Post subject:  RE:Reading Full Lines

Look.. I don't want to get into an argument. But you yourself know tehre is a page on the difference between struct and class. You could have simply linked it in your post. Your comment makes it seem like your looking down on him/her. Thats constructive criticism in it's worst form.

Author:  wtd [ Fri Jan 19, 2007 2:02 pm ]
Post subject:  RE:Reading Full Lines

It's plain text. No special formatting was used at all. Any emotional context you read into it came from yourself, rather than me. Something to consider, perhaps?

Author:  md [ Fri Jan 19, 2007 2:25 pm ]
Post subject:  RE:Reading Full Lines

I believe an answer has already been provided. Multiple times.

Author:  deville75 [ Fri Jan 19, 2007 2:35 pm ]
Post subject:  RE:Reading Full Lines

All I'm stating is my dream. I may have been right or wrong in mentioning your comment in my hope of good forums. I don't want to argue anymore. I'm just frustrated with things I've seen on some forums. This forum (compsci.ca) is particularly very good though. People are very helpful and knowledgable, which is why I'm here. Anyway I don't want to argue anymore.

Author:  wtd [ Fri Jan 19, 2007 2:51 pm ]
Post subject:  Re: RE:Reading Full Lines

deville75 @ Sat Jan 20, 2007 3:35 am wrote:
This forum (compsci.ca) is particularly very good though. People are very helpful and knowledgable, which is why I'm here.


Thank you.

Now, trust that we are not simply trying to be antagonistic. Smile


: