C++ newbie

I am trying to write a program to find the max number for 15 numbers inputted.
I am getting these errors when I try to complile it:

In function `int main()':
expected init-declarator before "std"
expected `,' or `;' before "std"
[Build Error] [numbers.o] Error 1

Here is my code:

#include <cstdlib>
#include <iostream>

using namespace std;


double MaxNum(double *, double *);

int main()
{


double MaxNum(double num, double max)

std::cout << "The largest number is : " << max << std::endl;

system("PAUSE");
return EXIT_SUCCESS;

return 0;
}

double MaxNum(double num, double max)
{

int i = 0;
int counter = 1;
for (i; i <= 15; i++)

std::cout << "Enter 15 numbers: ";
std::cin >> num;
max = num;
counter = counter + 1;

if (num >= max)
num = max;
else
max = max;
}


I would greatly apprecciate any help.
1. You don't need
using namespace std;
since you use the explicit std:: - prefix anyways.

2.
double MaxNum(double num, double max)
What should that be? Why do you (a) declare a function with pointers (b) re-declare it within main() and (c) define it without pointers? That doesn't work.
There are several possibilities to solve the problem. The perhaps nicest and shortest:
1
2
3
4
5
6
7
8
9
10
#include <iterator>
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
        std::vector<double> numbers;
        std::copy(std::istream_iterator<double>(std::cin), std::istream_iterator<double>(), std::back_inserter(numbers));
        std::cout<<*std::max_element(numbers.begin(), numbers.end())<<std::endl;
}

(though it doesn't input 15 numbers, but as many as you type until <Ctrl-D> or whatever your OS uses as "End of File" character)
As you can see, the usage of the stl reduces the lines of code which actually do any work from ~20 to 3 (declare a container, fill the container, and output the maximum of the container).

However, you lack the basics of function declaration & definition, loop usage and parameter passing (and don't even manage to write such a simple program system-independent). Do younself a favor and read a book like the C++ Primer by Lippman, Lajoie & Moo. Or at least the tutorials on this page.
Topic archived. No new replies allowed.