6 Replies - 6832 Views - Last Post: 05 November 2009 - 02:38 AM Rate Topic: -----

#1 vyhnanek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-April 09

cin.eof() trouble

Posted 03 April 2009 - 03:13 AM

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.
Is This A Good Question/Topic? 0
  • +

Replies To: cin.eof() trouble

#2 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5723
  • View blog
  • Posts: 22,635
  • Joined: 23-August 08

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: :code:

Thanks.
Was This Post Helpful? 0
  • +
  • -

#3 vyhnanek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-April 09

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.

View PostJackOfAllTrades, 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: :code:

Thanks.

Was This Post Helpful? 0
  • +
  • -

#4 vyhnanek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-April 09

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?
#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

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5723
  • View blog
  • Posts: 22,635
  • Joined: 23-August 08

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.
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5723
  • View blog
  • Posts: 22,635
  • Joined: 23-August 08

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:
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;
}

Was This Post Helpful? 0
  • +
  • -

#7 randallin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-November 09

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1