Thread: cin object fails to read double

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    41

    cin object fails to read double

    cin returns fail on trying to read double value.
    please help me find the bug.
    Code:
    #include<iostream>
    #include<array>
    using namespace std;
    const int MAX = 10;
    void read_donations(array<double, MAX>& a, int& count);
    int main()
    {
    	array<double, MAX> donations;
    	int count;
    	read_donations(donations, count);
    	double sum = 0; int i;
    	for (i = 0; i < count; ++i)
    	{
    		sum += donations[i];
    	}
    	double average = sum / count;
    	cout << "average is " << average << endl;
    	int larger=0;
    	for (i = 0; i < count; i++)
    	{
    		if (donations[i]>average)
    			++larger;
    	}
    	cout << larger << " elements are larger than the average.\n";
    }
    void read_donations(array<double, MAX>& a,int& count)
    {
    	int i = 0; count = 0;
    	cout << "element 1: ";
    	while ((i<a.size())&&!(cin >> a[i]))
    	{
    		++i; ++count;
    		cout << "element " << i + 1 << ": ";
    	}
    	if (count != MAX)
    		cout << "read only upto " << count << " elements.\n";
    	else
    		cout << "all elements read.\n";
    }

  2. #2
    Registered User
    Join Date
    Jul 2014
    Posts
    41
    in read_donations function,cin can't read the first element

  3. #3
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    I think

    Code:
    while((i<a.size())&&!(cin >> a[i]))
    should be

    Code:
    while((i<a.size())&&(cin >> a[i]))

  4. #4
    Registered User
    Join Date
    Jul 2014
    Posts
    41
    thanks Aslaville.
    it was a silly mistake

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The i<a.size() part doesn't make sense to me. Should you be using MAX there instead of a.size()?

    There doesn't seem to be a need for the i variable, you can drop it and just use count instead.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to read 2 adjacents strings object
    By piero borrelli in forum C++ Programming
    Replies: 3
    Last Post: 09-03-2014, 11:54 AM
  2. Replies: 7
    Last Post: 12-07-2012, 10:44 PM
  3. unable to read double A[0] and A[1] when n=1
    By sweetarg in forum C Programming
    Replies: 2
    Last Post: 10-25-2005, 12:35 PM
  4. read from file and put into double array ...
    By rox in forum C Programming
    Replies: 5
    Last Post: 12-02-2003, 02:32 PM
  5. Setting Object Data *double* For Class ComboBox :: MFC
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 03-24-2002, 06:20 PM