By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,060 Members | 1,893 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,060 IT Pros & Developers. It's quick & easy.

Error: invalid operands of types `double' and `double' to binary `operator>>'

P: 3
Expand|Select|Wrap|Line Numbers
  1. #include<cstdlib>
  2. #include<iostream>
  3. #include<iomanip>
  4. #include<vector>
  5.    #include<fstream>
  6. using namespace std;
  7.  
  8. double sum(vector<double> x)
  9.  {
  10.     double total = 0.0; 
  11.     for (int i=0; i<x.size(); i++) {
  12.         total = total + x[i];  
  13.     }
  14.     return total;
  15. }
  16.  
  17.  
  18.  
  19. int main()
  20. { vector<double> numbers;
  21.     double number;
  22.  int n;
  23.     ifstream in("data.txt");
  24.     while(in>>number)
  25.     {numbers.push_back(number);      }
  26.      for(int i=0;i<numbers.size();i++) 
  27. n= numbers.size();    
  28.     double k=sum(numbers);
  29. double y;
  30.   y=k/n;
  31.  
  32.      cout<<"mean of the values is:"<<" "<<y<<endl;
  33.  
  34.  for(int i=0;i<numbers.size();i++) 
  35. { double m= ((numbers[i]-y)*(numbers[i]-y));
  36.      vector<double> digits;
  37.      double digit;
  38.      while(m>>digit)
  39.      {digits.push_back(digit);}
  40.      double f=sum(digits);
  41.      double w=f/n;  
  42. cout<<"variance of the values is:"<<w<<endl;}
  43.  
hmmm,trying to calculate variance from values in vectors.but keep getting error saying invalid operands of types `double' and `double' to binary `operator>>' what does tt mean n what shld i do?
Sep 17 '07 #1
Share this Question
Share on Google+
2 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
The line 38 above, 'while (m>>digit)' . You declare m as a double, and then try to use it as you would a filehandle and read data in from it. So your types don't match, which is giving you that error.
Sep 17 '07 #2

sicarie
Expert Mod 2.5K+
P: 4,677
Also, I've changed the thread title, please have a look at the Posting Guidelines, as we ask that when you post you do things like use a good thread title, code tags, etc... Thanks!
Sep 17 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.