Working with functions

I have tried writing a code which takes two numbers from the user and calculates their square root then the roots are added up to return the sum. The program is coming out with loads of errors. any help will be appreciated. Thanks

#include<iostream>
#include<cmath>
float main()
{
using namespace std;
float m1,m2,m3,m4,m5;
cin >> m1 >> m2;
m3=rooter(m1);
m4=rooter(m2);
cout << "The square root of the two provided no is " << m3 <<"and" << m4
cout <<"respectively" <<endl;
m5=additioner(m1,m2);
cout <<"The sum of above numbers is" << m5 << endl;
system("pause");
return 0;
}


float rooter(float x)
{
using namespace std;
x=sqrt(x);
return x;
}



float additioner(float y,float z)
{
using namespace std;
double a;
a=y+z;
return a;
}

float main() That's a new one, main should return int

As for the errors they are probably coming from you not having a semi colon on this line cout << "The square root of the two provided no is " << m3 <<"and" << m4 It probably mentioned that when you tried to compile. I would also advise against using system("anything"); as it can leave holes in security and is pretty heavy. There are much better ways to keep the console open if that is what you are trying to do. http://www.cplusplus.com/forum/beginner/1988/

Forgot to mention this you need to prototype your functions before main. So you would copy the first line of the function and put that before the main function with a semi-colon at the end for example:
1
2
3
4
5
6
7
float rooter(float x); //<---prototype
//the prototype doesn't need to have the parameter names but the type is a must
//float rooter(float) <--- so this would work also
int main()
{
    ...
}


Also, please put code tags around your code. Either edit and press the <> button or type [ code] before and [/code] after (without the space)

TL;DR
3:12: error: '::main' must return 'int' In function 'int main()'
: 8:13: error: 'rooter' was not declared in this scope 
11:1: error: expected ';' before 'cout' 
12:20: error: 'additioner' was not declared in this scope
Last edited on
Still encountering errors

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

#include<iostream>
#include<cmath>
float ro(float);
float ad(float);

int main()
{
using namespace std;
float m1,m2,m3,m4,m5;
cin >> m1 >> m2;
m3 = ro(m1);
m4 = ro(m2);
cout << "The square root of the two provided no is " << m3 <<"and" << m4 <<"respectively" <<endl;
m5 = ad(m3,m4);
cout <<"The sum of above numbers is" << m5 << endl;
cin.get();
return 0;
}


float ro(float x)
{
using namespace std;
x=sqrt(x);
return x;
}



float ad(float y,float z)
{
using namespace std;
float a;
a=y+z;
return a;
}


errors encountered are

1. In function"int main()"
2. Too many arguments to function'float ad(float)'

Do you think the errors may be compiler dependent. I am using bloodshed dev c++

Thanks
@skbcoder

Put only one instance of using namespace std; after you prototype your functions. You can remove them from the functions, and in main().

You declared float ad(float) with one float variable, but you're sending two. Add another float in the declaration, so it matches what is being called.
Last edited on
Thanks a lot for all the help the code is now working flawlessly

@whitenite1
@giblit
Topic archived. No new replies allowed.