What is the problem of my project?

4 views
Skip to first unread message

bahador saket

unread,
Dec 11, 2009, 6:45:42 AM12/11/09
to FIT Cafeteria
Dear all
I wrote the program that give name of student ,his/her major and his
id number and save it in file.
and secondly, i want to input name and my program search and find it
in my file.
but unfortunately, my program work wrong.
download : http://www.filefront.com/15109737/file.rar
can you help me?


Regards,
Bahador Saket

Ognjen Regoje

unread,
Dec 11, 2009, 6:53:33 AM12/11/09
to fit-cafeteria
Hey,

first, you have to close the output stream and then you have to open the file for reading.
And secondly, I am not really sure that cause of this but the buffer for getline needs you to execute it twice once you change the input stream that you are using.

I attached the file and I think it is working as you imagined.

Regards
Ognjen



Untitled1.cpp

bahador saket

unread,
Dec 11, 2009, 7:30:54 AM12/11/09
to FIT Cafeteria

Dear Ognjen Regoje

i found my answer :D
we should type getline after inputing number. I think every thime that
wewant to input numbers in file we should not forget
getline() after it.
for(int i=0;i<3;i++)
{
cout<<" Enter name of student :"<<i+1<<endl;
getline(cin,name);
of_output<<name<<endl;
cout<<" Enter major of student :"<<i+1<<endl;
getline(cin,major);
of_output<<name<<endl;
cout<<" Enter ID number of student :"<<i+1<<endl;
cin>>id_number;
getline(cin,name);
of_output<<id_number<<endl; // please attend to this part
}

tnx for your helps.

Best Regards,
Bahador Saket

On Dec 11, 3:53 am, Ognjen Regoje <ore...@gmail.com> wrote:
> Hey,
>
> first, you have to close the output stream and then you have to open the
> file for reading.
> And secondly, I am not really sure that cause of this but the buffer for
> getline needs you to execute it twice once you change the input stream that
> you are using.
>
> I attached the file and I think it is working as you imagined.
>
> Regards
> Ognjen
>
> On Fri, Dec 11, 2009 at 19:45, bahador saket <bahador.sa...@gmail.com>wrote:
>
> > Dear all
> > I wrote the program that give name of student ,his/her major and his
> > id number and save it in file.
> > and secondly, i want to input name and my program search and find it
> > in my file.
> > but unfortunately, my program work wrong.
> > download :http://www.filefront.com/15109737/file.rar
> > can you help me?
>
> > Regards,
> > Bahador Saket
>
> > --
> > Post: fit-ca...@googlegroups.com
> > Unsubscribe: fit-cafeteri...@googlegroups.com
> > Visit:http://groups.google.com/group/fit-cafeteria?hl=en
>
>
>
>  Untitled1.cpp
> 1KViewDownload

Ian Tam

unread,
Dec 12, 2009, 10:05:56 AM12/12/09
to fit-ca...@googlegroups.com
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.
--
Regards,

Ian Tam Yee Yung
Editor-in-Chief Students' Publication Organization 09/10
President ITSociety MMU - Cyberjaya 08/09
Open Source Alliance - Cyberjaya 07/08-09/10
Multimedia University
Cyberjaya
email: cere...@iantam.net / cere...@gmail.com / ian.tam.y...@mmu.edu.my
Tel: +6019.214.2887

Wong Ya Ping

unread,
Dec 12, 2009, 11:50:41 AM12/12/09
to fit-ca...@googlegroups.com
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 to http://www.cppreference.com/wiki/io/sstream/start

Hope this help.
YP
The fool thinks himself to be wise, but the wise man knows himself to be a fool. --William Shakespeare

tee jia hen

unread,
Dec 13, 2009, 8:25:05 AM12/13/09
to fit-ca...@googlegroups.com
Ian Tam , nice one!
Add my facebook!
http://www.facebook.com/wizz.tee?ref=profile

bahador saket

unread,
Dec 13, 2009, 10:30:11 AM12/13/09
to FIT Cafeteria
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 Dec 13, 5:25 am, tee jia hen <wizz...@gmail.com> wrote:
> Ian Tam , nice one!
>
> 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
Reply all
Reply to author
Forward
0 new messages