My first C++ program!

Here is the first program we were to make for my intro to programming class. It is to calculate the area and perimeter of a rectangle. It works but I am not sure the code style is as good as it could be. What do you all think? Any tips or suggestions? it is a console apllication and each //module is a different file in the project. used Dev C++

// main module
include <iostream>

using namespace std;

// prototype statements

void extern input (float&, float&);

void extern output (float length, float width,

float area, float perimeter);

void extern calculate (float length, float width, float & area, float & perimeter);

int main () {

// Declaration (data)
float length, width;
float area, perimeter;

// Logic (executable)
input(length, width);

calculate (length, width, area, perimeter);

output (length, width, area, perimeter);

system ("pause");
return 0;

}

// Input module
include <iostream>

using namespace std;

void input (float & length, float & width)

{

cout << "Enter length and width of rectangle seperated by a space-> ";
cin >> length >> width;

}

// Calculate module
include <iostream>

void calculate (float length, float width, float & area, float & perimeter)

{

//executable coding - logic - algorithm

area = length * width; perimeter = (length + width) * 2;

}

// Output module
include <iostream>

using namespace std;

void output (float length, float width,

float area, float perimeter)

{

cout <<"the length is " << length << endl; cout <<"the width is " << width << endl;
cout <<"The area is " << area << endl; cout <<"The perimeter is " << perimeter << endl; }

_______________________________________________________________________________
Also, since I sort of just made this program by looking at a different simple calculation program example we had in class and plugged things in accordingly, I do not understand all of the code. Can anyone tell me why I needed to do void followed by data in all of the modules? thanks!
If you happen to put your code between code Tags we could tell you more about your programming style. http://www.cplusplus.com/articles/firedraco1/

void is the type of return value of the function, in case of void its none (int is an integer value etc) followed by the name of the function. The data in the brackets are the parameters of the function, each with a data type and a name.
I believe in this case you want to use float calculate(float length, float width, float &area, float &perimeter)
Because you'd want to return a value (so you can eventually show it).. However, if you'd not want that just stick with a void, since that doesn't return a value.

By putting each data type and a name (the name is optional i believe) the program knows what kind of arguments are allowed to be passed to the function (so you can't pass a char if there's an int declared).

You can also simplify this program in 1 file:

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
//main.cpp
#include <iostream> //note you need a # 
using std::cout;
using std::cin; //using standard namespace cin and cout.. In this case it's the same as using namespace std

void extern input (float&, float&);

void extern output (float length, float width, float area, float perimeter);

void extern calculate (float length, float width, float & area, float & perimeter);

int main () {

// Declaration (data)
float length, width;
float area, perimeter;

// Logic (executable)
input(length, width);

calculate (length, width, area, perimeter);

output (length, width, area, perimeter);

getchar(); //this is better then system("pause")
return 0;
}


void input (float & length, float & width)
{
cout << "Enter length and width of rectangle seperated by a space-> ";
cin >> length >> width;
}

void calculate (float length, float width, float & area, float & perimeter)
{
//executable coding - logic - algorithm
area = length * width; perimeter = (length + width) * 2;
}

void output (float length, float width, float area, float perimeter)
{
cout <<"the length is " << length << "\n"; cout <<"the width is " << width << "\n"; //note that there's a \n instead of endl, since \n is faster, and in this case does the same
cout <<"The area is " << area << "\n"; cout <<"The perimeter is " << perimeter << "\n";
 }


Note that you just remove the includes and namespace declarations, since you're still working in the same file (the include and namespace are declared in the global scope so it's passed on.) If you'd use this in separate files there'd be no use to prototyping (but then you'd have to redo the includes and namespacing)


Feel free to ask any questions :)

Hope it helps!

Cheers!
Topic archived. No new replies allowed.