Hello,
I guess I need a help. I need to write simple loop in which user types single numbers and after he types ctrl+z, it would write on the screen "end of typing" and program would close. Thanks.
cin.eof() trouble
Page 1 of 16 Replies - 6832 Views - Last Post: 05 November 2009 - 02:38 AM
Replies To: cin.eof() trouble
#2
Re: cin.eof() trouble
Posted 03 April 2009 - 04:29 AM
Smells like homework, so let's move it. Here's a link for you to read that might help.
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.
Post your code like this:
Thanks.
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.
Post your code like this:
Thanks.
#3
Re: cin.eof() trouble
Posted 03 April 2009 - 05:02 AM
lol actually that's a part of homework when user fetch data into array, than it goes with binary search which is in that loop which I written before and after user type a symbol it searches in that array and goes with found it or not found. All I need is after you don't want to search anymore you just press ctrl+z and program will exit. And I am just having trouble with that cin.eof() and ctrl+z so I don't understand why you blame me.
JackOfAllTrades, on 3 Apr, 2009 - 03:29 AM, said:
Smells like homework, so let's move it. Here's a link for you to read that might help.
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.
Post your code like this:
Thanks.
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.
Post your code like this:
Thanks.
#4
Re: cin.eof() trouble
Posted 03 April 2009 - 05:23 AM
Hello,
I am gonna try to describe my problem again since my first topic was moved. In basic this is just part of my program but all I need is when user enters numbers in a loop and after user decides he doesn't want to enter anymore he just types ctrl+z and program will exit. My problem is when I type ctrl+z it goes like never-ending loop and keep showing "end of entry". Could someone help me with that?
I am gonna try to describe my problem again since my first topic was moved. In basic this is just part of my program but all I need is when user enters numbers in a loop and after user decides he doesn't want to enter anymore he just types ctrl+z and program will exit. My problem is when I type ctrl+z it goes like never-ending loop and keep showing "end of entry". Could someone help me with that?
#include <iostream>
using namespace std;
int main()
{
while(1){
int i;
cout<<"enter number"<<endl;
cin>>i;
while(cin.eof()){
cout<<"end of entry"<<endl;
cin.get();
exit(1);
}
}
}
This post has been edited by vyhnanek: 03 April 2009 - 05:24 AM
#5
Re: cin.eof() trouble
Posted 03 April 2009 - 06:56 AM
It's a homework question, I'm not "blaming" you...I just moved it to the C++ Homework forum, where it belongs. And I'll shortly merge the thread you started in the other forum into this one.
EDIT: Done.
EDIT: Done.
#6
Re: cin.eof() trouble
Posted 03 April 2009 - 07:19 AM
Seems to work for me, but yet it doesn't seem quite right from a logical process point-of-view.
Think about it, this is what you want to do, in pseudocode:
Here's an example using a vector:
Think about it, this is what you want to do, in pseudocode:
read data entry while not eof if the entry is good add entry to array read next data entry
Here's an example using a vector:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> numbers;
int i;
cout<<"enter number: ";
cin>>i;
while (!cin.eof()) // No Ctrl-Z (or Ctrl-D on Unix)
{
if (cin.good()) // The data entry was valid, i.e., an integer
{
numbers.push_back(i);
}
else
{
// The failbit was set, so this entry must be invalid.
cout << "Invalid entry!" << endl;
cin.clear(); // clear error flag
cin.sync(); // Re-sync stream
}
// Get next data entry
cout<<"enter number: ";
cin>>i;
}
for (size_t i = 0 ; i < numbers.size(); ++i)
cout << numbers[i] << endl;
return 0;
}
#7
Re: cin.eof() trouble
Posted 05 November 2009 - 02:38 AM
Hi,
Don't know if this thread is relevant anymore, but since I spent a little time figuring out
just exactly what is triggering the endless loop, I will post my answer.
One importnant thing first:
The endless loop is only triggered when you write a number followed by (ctrl +z) followed by enter, as in
5 + (ctrl +z) + enter.
Otherwise the program will work flawlessly.
The problem basically arises, because (ctrl + z) is a character, and not an integer. (Ctrl +z) is the "End of file"-character.
In your code you have:
cin >> i.
, where i is an integer.
The program will just skip this line if cin is (ctrl+z), and this is what is causing the endless loop.
Well, this is the mainline, now you can figure out the details yourself (This is homework
)
Regards, Jakob.
Don't know if this thread is relevant anymore, but since I spent a little time figuring out
just exactly what is triggering the endless loop, I will post my answer.
One importnant thing first:
The endless loop is only triggered when you write a number followed by (ctrl +z) followed by enter, as in
5 + (ctrl +z) + enter.
Otherwise the program will work flawlessly.
The problem basically arises, because (ctrl + z) is a character, and not an integer. (Ctrl +z) is the "End of file"-character.
In your code you have:
cin >> i.
, where i is an integer.
The program will just skip this line if cin is (ctrl+z), and this is what is causing the endless loop.
Well, this is the mainline, now you can figure out the details yourself (This is homework
Regards, Jakob.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|