Need help with a main function.

I'm just trying to test my code to see if it works. The point is to enter an array of numbers and get the average out of all of them. I think the function is fine, but I'm having trouble coding the main method to test it.

Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
double ComputeAverage(double num[], int size)
{
  double sum = 0;
  double average = sum/size;
  for (int i = 0; i < size; i++)
    sum += num[i]; 
  return average;
}
 
int main()
{
  double number;
  std::getline(cin, number);
  bool success = ComputeAverage(number);
  std::cout << "Average Check: " << number;
 }


And here's the compile error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
$ make
/usr/bin/g++ -Wall -Wextra -g test.cpp  -o test
test.cpp: In function ‘int main()’:
test.cpp:31:27: error: no matching function for call to ‘getline(std::istream&, double&)’
   std::getline(cin, number);
                           ^
test.cpp:31:27: note: candidates are:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/string:53:0,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/locale_classes.h:40,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/ios_base.h:41,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/ios:42,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/ostream:38,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/iostream:39,
                 from test.cpp:8:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/basic_string.tcc:1068:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&, _CharT)
     getline(basic_istream<_CharT, _Traits>& __in,
     ^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/basic_string.tcc:1068:5: note:   template argument deduction/substitution failed:
test.cpp:31:27: note:   mismatched types ‘std::basic_string<_CharT, _Traits, _Alloc>’ and ‘double’
   std::getline(cin, number);
                           ^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/string:52:0,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/locale_classes.h:40,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/ios_base.h:41,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/ios:42,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/ostream:38,
                 from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/iostream:39,
                 from test.cpp:8:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/basic_string.h:2812:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&)
     getline(basic_istream<_CharT, _Traits>& __is,
     ^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/basic_string.h:2812:5: note:   template argument deduction/substitution failed:
test.cpp:31:27: note:   mismatched types ‘std::basic_string<_CharT, _Traits, _Alloc>’ and ‘double’
   std::getline(cin, number);
                           ^
test.cpp:32:39: error: cannot convert ‘double’ to ‘double*’ for argument ‘1’ to ‘double ComputeAverage(double*, int)’
   bool success = ComputeAverage(number);
                                       ^
test.cpp:32:8: warning: unused variable ‘success’ [-Wunused-variable]
   bool success = ComputeAverage(number);
        ^
makefile:10: recipe for target 'test' failed
make: *** [test] Error 1
getline works with strings. A double is not a string. getline gets a full line of text. A double's textual representation may or may not be a full line of text.
So what do I use to put in numbers?
Basically everything is wrong in your code.

You're trying to get multiple doubles by using getline
You should be using something like cin >> number; to get a number.

Even if you were using cin, you still need to declare an array of doubles, not a single double. How do you expect to store more than one double when you only have storage for one double?

For some reason you did
bool success = ComputeAverage(number);

This makes no sense whatsoever.
THIS IS WHY YOU SHOULDN'T JUST COPY/PASTE CODE INTO YOUR PROGRAM. YOU NEED TO ACTUALLY UNDERSTAND IT SINCE YOU OBVIOUSLY DIDN'T WRITE THIS FUNCTION.


First off, ComputeAverage returns a double. You are setting success to be equal to whatever your ComputerAverage function returns. Bool can either be 0 or 1. ComputeAverage is returning a double, so this makes no sense.

This is also wrong, because even if success was a double instead of a bool, you are passing in the wrong arguments into ComputeAverage. Your function clearly says that it takes an array of doubles for the first argument, and an integer for the size of that array for the second argument.

Please review how functions work and try this again.

Edit: Also you didn't #include <iostream> , but you're trying to std::cout

Link to tutorial on functions
http://www.cplusplus.com/doc/tutorial/functions/

Link to tutorial on arrays
http://www.cplusplus.com/doc/tutorial/arrays/
Last edited on
I did copy/paste the main method, but it was from previous my previous test. Forgot I didn't need to use bool in this situation and I have no experience testing numbers and assumed a lot of it was the same. Anyway updated the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
double ComputeAverage(double num[], int size)
{
  double sum = 0;
  double average = sum/size;
  for (int i = 0; i < size; i++)
    sum += num[i]; 
  return average;
}
 
int main()
{
  double number;
  const int size=4;
  cin >> number;
  ComputeAverage(number, size);
  std::cout << "Average Check: " << number;
 }


and got this compiler error

1
2
3
4
5
6
7
8
9
$ make
/usr/bin/g++ -Wall -Wextra -g test.cpp  -o test
test.cpp: In function ‘int main()’:
test.cpp:33:30: error: cannot convert ‘double’ to ‘double*’ for argument ‘1’ to ‘double ComputeAverage(double*, int)’
   ComputeAverage(number, size);
                              ^
makefile:10: recipe for target 'test' failed
make: *** [test] Error 1


You mentioned making another array, so I turned both "number" into number[]", but this screw up with the compiler too. What exactly did you mean in this case?
Topic archived. No new replies allowed.