Dear all
i have to say tnx for your helps.
i will work hard and i will ask you if i have any question.
Regards,
Bahador Saket
> On Sun, Dec 13, 2009 at 12:50 AM, Wong Ya Ping <
wongyap...@gmail.com> wrote:
>
>
>
> > Ian Tam's explanation is correct.
>
> > The better way is to just use cin.ignore() to ignore one character (i.e.
> > newline character in this case) from the buffer.
>
> > Pls refer to my CP1 lecture slides on this issue, particularly on page 103:
> >
http://ypwong.homeip.net/cp1/YP/Lec13_YP_ver090823.pdf
>
> > If you are writing serious program for other people to use, then do not use
> > cin to read numbers directly into int, instead read it into string stream,
> > and then read the int out from the string stream. This is much safer
> > actually.
>
> > I am using a borrowed pc right now, so i can't give you an example. You may
> > google it and refer tohttp://
www.cppreference.com/wiki/io/sstream/start
>
> > Hope this help.
>
> > On Sat, Dec 12, 2009 at 11:05 PM, Ian Tam <
cerea...@gmail.com> wrote:
>
> >> Whenever possible, use getline().
>
> >> The problem you are having is probably is the cin >> buffer input problem.
> >> Don't worry. It is very common problem that is encountered by new learners.
>
> >> The problem with cin>> or any other buffered input is that when the
> >> program reads a type integer from the buffered input, it takes only the
> >> integer and leave the newline still in the buffer. Thus, this will result
> >> your next cin>> starts with a newline. If your next input is an integer, it
> >> will automatically discard the newline. however, if your next input is for
> >> string or character, it will read the newline. This is where it give problem
> >> to the getline() because it stops at the first newline it encounter. Do take
> >> note that when you press enter, it is registered into the buffer as newline.
>
> >> *example from you program without the last getline():*
> >> buffer in refers to what you have typed on your keyboard and supplied to
> >> the console
> >> buffer out refers to data in buffer in that has been pushed to your
> >> variable
> >> buffer refers to the current content of the buffer after pushing the data
> >> into your variable
> >> *first loop*
>
> >> cout<<" Enter name of student :"<<i+1<<endl;
> >> getline(cin,name);
> >> *//buffer in: John Doe\n
> >> //buffer out to name: John Doe\n
> >> //buffer: <<empty>>
> >> *
> >> of_output<<name<<endl;
> >> cout<<" Enter major of student :"<<i+1<<endl;
> >> getline(cin,major);
> >> *//buffer in: SEGD\n
> >> //buffer out to major: SEGD\n
> >> //buffer: <<empty>>*
>
> >> of_output<<name<<endl;
> >> cout<<" Enter ID number of student :"<<i+1<<endl;
> >> cin>>id_number;
> >> *//buffer in: 1011100881\n
> >> //buffer out to id_number: 1011100881 (this is where you problem begin
> >> because your program will only read in integer, not the newline)
> >> //buffer: \n*
>
> >> of_output<<id_number<<endl; // please attend to this part
>
> >> *second loop*
>
> >> cout<<" Enter name of student :"<<i+1<<endl;
> >> *//buffer: \n (on your second loop, because newline is still available
> >> inside the buffer, your next getline() will read in without waiting for your
> >> input)*
> >> getline(cin,name);
> >> *//buffer in: <<empty. You typed nothing. Your program will skip this.>>
> >> //buffer out to name: \n
> >> //buffer: <<empty>>*
>
> >> of_output<<name<<endl;
> >> cout<<" Enter major of student :"<<i+1<<endl;
> >> getline(cin,major);
> >> *//buffer in: SE\n
> >> //buffer out to major: SE\n
> >> //buffer: <<empty>>*
>
> >> of_output<<name<<endl;
> >> cout<<" Enter ID number of student :"<<i+1<<endl;
> >> cin>>id_number;
> >> of_output<<id_number<<endl; // please attend to this part
>
> >> One way to fix this problem is to use cin.clear() to clear the buffer
> >> after using cin>>variable.
>
> >> Hope this help. This is as far as my limited knowledge on C and C++ I can
> >> understand. Perhaps CP lecturers will able to explain more.
>
> >> email:
cerea...@iantam.net /
cerea...@gmail.com /
> >>
ian.tam.yee.yun...@mmu.edu.my