Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Reading Full Lines
Index -> Programming, C++ -> C++ Help
Goto page 1, 2, 3  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Prince Pwn




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Prince Pwn




PostPosted: Sun Dec 10, 2006 11:02 pm   Post subject: (No subject)

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




PostPosted: Sun Dec 10, 2006 11:14 pm   Post subject: (No subject)

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




PostPosted: Mon Dec 11, 2006 12:05 am   Post subject: (No subject)

December
rdrake




PostPosted: Mon Dec 11, 2006 12:23 am   Post subject: (No 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?
Prince Pwn




PostPosted: Mon Dec 11, 2006 12:44 am   Post subject: (No 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++.
Prince Pwn




PostPosted: Mon Dec 11, 2006 12:48 am   Post subject: (No 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.
r.3volved




PostPosted: Mon Dec 11, 2006 1:48 pm   Post subject: (No 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.
Sponsor
Sponsor
Sponsor
sponsor
ownageprince




PostPosted: Tue Jan 02, 2007 7:57 pm   Post subject: (No 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.
abcdefghijklmnopqrstuvwxy




PostPosted: 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.
wtd




PostPosted: 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
abcdefghijklmnopqrstuvwxy




PostPosted: 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?
rdrake




PostPosted: 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.
wtd




PostPosted: 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.
ownageprince




PostPosted: 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..
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 3  [ 38 Posts ]
Goto page 1, 2, 3  Next
Jump to:   


Style:  
Search: