implemented functions .. i don`t know what`s wrong

seriously i don`t know how am i going to take the test on Monday~~??

i can`t get what`s wrong with my implemented function??

i tried this code...

please anyone fix it for me!(+.+,)


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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<iostream>
#include <cmath>
using namespace std;


void twoDShape()
{
	public:
		double addonenumber(double rad); //--> Function Decleration

};

// .cpp file -  THE IMPLEMENTED FUNCTIOON


void twoDShape::addonenumber(double rad)
{
const double PI = 3.14;
    
    double circum=1;
    double area=1;
	
    // do the math

    circum = PI * 2 * rad;    // circumference of a circle
    area = PI * pow(rad,2.0);        // area of a circle

	return void;
}




int main()

{
	double rad;
	double circum;
	double area;
	int i=1;
    // display question
	while (rad != -1)
	{
    cout << "\nWhat is your radius (-1 to stop) : " << endl;
    cin >> rad;      // except input

    // display answer
	if (rad != -1)
	{
    cout << "the area is " << area <<" and the circumference is" << circum <<endl;
	cout<< twoDShape<<endl;
	}


	else
	cout<< "Thank You!"<<endl;
	

	i++;
	}

	return 0;

}
Have you studied the C C++ "class" feature in you course?


Last edited on
Try reading this tutorial page. It should answer your question better than I can.

http://www.cplusplus.com/doc/tutorial/functions/

If you still have questions, let us know.
no... i`m an electrical engineer.. all engineers here are demand to take 4 extra classes out of their major~!!

and they only teach us c++ not C!! so yeah ,i`m a lost pigeon:c
I confused by saying C class when I meant C++ class. C does not have classes. Corrected.

Have you read the tutorial to which doug4 showed you? (Or the equivalent in your native language.)

Some of your problems with this code is that you copied mutexe's code offered in another thread. You changed it before you understood it.

By the way, why do you start new threads? I realize this is a "new" program, but you copied most of it from another thread. If you are going to do that, just reply in that thread so the context is understood.

So what compilation errors and warnings is your code displaying?
I get the following errors:

1><snip>\areacircumference.cpp(8): error C2143: syntax error : missing ';' before 'public'
1><snip>\areacircumference.cpp(17): error C2653: 'twoDShape' : is not a class or namespace name
1><snip>\areacircumference.cpp(32): error C2062: type 'void' unexpected
=

Do you know how to interpret the warnings and errors your compiler gives you?
For example, my first error is in the file areacircumference.cpp at line 8. For this compiler the error is numbered C2143 but it does not matter. The error on line 8 is "syntax error : missing ';' before 'public'. The error stems from several lines before because we are in a function definition and not a class declaration.

Here is your code with many additional comments.
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream>
#include <cmath>
using namespace std;


void twoDShape()  // Defines (and declares) a function named twoDShape
{
	public:  // the "public:" keyword only belongs in a C++ class declaration.
		     // This is a
		double addonenumber(double rad); //--> Function Decleration
		/* So twoDShape has no executable code. So calling it does nothing. */
};

// .cpp file -  THE IMPLEMENTED FUNCTIOON


void twoDShape::addonenumber(double rad)  
	/* Defines a function named twoDShape::addonenumber which takes a double parameter named rad. 
	   However there is no namespace named nor class declared twoDShape. */
{
const double PI = 3.14;
    
    double circum=1;
    double area=1;
	
    // do the math

    circum = PI * 2 * rad;    // circumference of a circle
    area = PI * pow(rad,2.0);        // area of a circle


	return void;
	/* void is a keyword which is a type.  
	   The return statement expects return <expression>;.  
	   A type is not an expression. 
	   
	   Since this function is defined to return a void type, you could just use return; 
	   However, since this is the end of the function and the return type of your function is void, 
	   there is no need for a return statement.  
	   
	   circum and area will no longer exist or have values at the end of this function.  They will "go out of scope".  */

}


int main()

{
	double rad;
	double circum;
	double area;
	int i=1;
    // display question
	while (rad != -1)
	{
    cout << "\nWhat is your radius (-1 to stop) : " << endl;
    cin >> rad;      // except input

    // display answer
	if (rad != -1)
	{
    cout << "the area is " << area <<" and the circumference is" << circum <<endl;
	cout<< twoDShape<<endl;  /* Since twoDShape is defined above as a function,
							    this will display the pointer to the function twoDShape
								if cout, which is an object of ostream, 
								has a member function for operator << with the parameter is void (*)().  
								void (*)() is the type for a pointer to a function with no parameters 
								and does not return a value. */
	}


	else
	cout<< "Thank You!"<<endl;
	

	i++;
	}

	return 0;

}

Last edited on
lots of mistakes ,lots of errors i see:c

and yeah i read the tutorial but ,i didn`t find anything mentioned about the implemented functions!! or how to use them or how to write them properly...


and yeah.. i guess i do that cause i didn`t understand so i try everything available to see if it works or needs more details to work...
and yeah i check my errors but most of errors i can`t understand((i sound stupid)) ..

though i made it through the first classes ,,and most of problems by myself!! but as we go deeper in studying c++ it starts to sound more complicated .. and not being good in english is affecting me too in programming .. so yeah that`s yeah...

ow and i read the comments on my code ((i feel stupid TBH)) (u _u")
Last edited on
i got it hahaaaa:D:D

here is my code ...

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
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>
#include <cmath>
using namespace std;

void twoDShape(double ,double &, double &); //function prototype


int main()
{
	double rad;
	double area;
	double circum;

	cout<<"what is your radius (-1 to stop): ";
	cin>> rad;

	while (rad!= -1)
	{
		twoDShape(rad, area,circum);

		cout<<"the area is "<<area<<" and the circumference is "<< circum<<"\n";
		cout<<"\n";
        
		cout<<"what is your radius (-1 to stop): ";
		cin>> rad;

	}

	cout<< "Thank You!"<< endl;

	return 0;

}


void twoDShape(double rad,double  &area, double &circum)
{
	area= 3.14*rad*rad;

	circum=2*3.14*rad;

}


/*
what is your radius (-1 to stop): 2
the area is 12.56 and the circumference is 12.56

what is your radius (-1 to stop): 10
the area is 314 and the circumference is 62.8

what is your radius (-1 to stop): -1
Thank You!
Press any key to continue
*/
You said:
i read the tutorial but ,i didn`t find anything mentioned about the implemented functions

Line 5 "declared" a function twoDShape().

Lines 36 to 42 "implemented" or "defined" a function twoDShape().

Line 19 "calls" a function twoDShape().

I went to my instructor , then read it again!! 😅 cause I didn't even know what it meant!! He needed to explain it for me 3 times (´・_・`) so this is a new way to show my(( stupid brain functions[~;_;]~))

pheininger



And I have ADHD thingie
Last edited on
Topic archived. No new replies allowed.