Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,173 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,942 people online right now. Registration is fast and FREE... Join Now!




cin.get problem

 
Reply to this topicStart new topic

cin.get problem

Rating  5
dan_ram
14 Dec, 2007 - 09:40 AM
Post #1

D.I.C Head
**

Joined: 15 Aug, 2007
Posts: 76


My Contributions
CODE

#include<iostream.h>
void main()
{
    char s1[20],s2[20],s0[20];
    cin.get(s0,20).getline(s1,20).getline(s2,20);
    cout<<"s1 "<<s1<<endl<<"s2 "<<s2<<endl
          <<s0<<endl;
}

i jus happened 2 try tis out n the output is quiet problematic..
only 2 inputs were allowed

the user screen:
3254
yhtr

s1
s2 yhtr
3254


pls explain how the values r being assigned.

User is offlineProfile CardPM
+Quote Post

dan_ram
RE: Cin.get Problem
14 Dec, 2007 - 09:59 AM
Post #2

D.I.C Head
**

Joined: 15 Aug, 2007
Posts: 76


My Contributions
ne1 there??????????????????/
User is offlineProfile CardPM
+Quote Post

Delta_Echo
RE: Cin.get Problem
14 Dec, 2007 - 10:12 AM
Post #3

D.I.C Regular
***

Joined: 24 Oct, 2007
Posts: 439


My Contributions
I am, but i can't help sry sad.gif
i too am a noob...but consider this: i don't see any input to assign a value and i don't see you assigning a value so....thats about all i can say.
lol

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Cin.get Problem
14 Dec, 2007 - 10:13 AM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,199



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Please be patient when receiving answers. We have a lot of people asking and answering and we get questions when we can. Thanks! smile.gif

The answer to your question is that when you use cin.get it reads in characters up to the first carriage return, but leaves the carriage return in the buffer. So you get s0 containing the first line. When you use getline, it takes in the carriage return also. So being that there is a carriage return sitting in the buffer still, it takes that in and only the carriage return, leading to s1 containing no string (just "eating up" the carriage return). Lastly since the carriage return is now eaten, the second getline gets the rest of the line and puts it in s2.

The result.... s0 contains the first line, s1 contains a carriage return and s2 contains the second line. smile.gif
User is offlineProfile CardPM
+Quote Post

dan_ram
RE: Cin.get Problem
14 Dec, 2007 - 11:06 AM
Post #5

D.I.C Head
**

Joined: 15 Aug, 2007
Posts: 76


My Contributions
thnx......smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 12:45AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month