Saying hi and posting my first code.

Hi,
Like most people posting here in beginners i am new to C++ .. Although I do not have a specific question in mind I would like to post my first code (besides "hello world") for input and what not.. although I know their are easier and more effective ways to write this I am at my limits as to what i've learned so far (using the cplusplus.com tutorial) with the exception of pointers which i am still trying to wrap my nugget around.

basically this code does simple geometric math for 4 different shapes.. I am using Bloodshed Dev-C++ and this is a console program.

I assume I could use pointers to ensure that the memory allocation uses as little memory as possible (if im understanding pointers correctly) and i know in this small program it doesn't matter but i am sure it does in larger ones.. well on to the 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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Simple Geometry calculator.

#include <iostream>
#include <string>
using namespace std;

/// The one constant////////////////////
#define PI 3.14159

/// Variables///////////////////////////
char fmenu [] = "\n1: Perimeter \n2: Area \n0: Return to Root \n";   //menu used many times didn't want to have to write it each time.
int Userchoice1 = 0;                                                 
int Userchoice2 = 0;

/// Function prototype//////////////////
int GetUserChoice (); // Get number option from user. 
int Root ();          // Root menu.
int Circle ();        // circle menu and functions.
int Square ();        // Square menu and functions.
int Para ();          // Parallelogram menu and functions.
int Rect ();          // Rectangle menu and functions.
/// Functions////////////////////////////

int GetUserChoice () {
    int uchoice;
    uchoice = 0;
    cin >> uchoice;
    return (uchoice);
    uchoice = 0;
} 

int Root () {
      Userchoice1 = 0;
      cout << "\n1: Square \n2: Rectangle \n3: Parallelogram \n";
      cout << "4: Circle \n0: Quit \n";
      Userchoice1 = GetUserChoice();
      if (Userchoice1 == 0){
         cout << "\nGoodbye!\n";
         cin.ignore(256, '\n');
         cout << "Press ENTER to continue..." << endl;
         cin.get();
        return 0;}
      else switch (Userchoice1) {
           case 1: Square (); break;
           case 2: Rect (); break;
           case 3: Para (); break;
           case 4: Circle (); break;
           }     
}

int Square () {
    float sidea, answer;
    cout << fmenu;
    Userchoice2 = GetUserChoice();
    if (Userchoice2 == 0)
       Root();
    else switch (Userchoice2) {
         case 1: cout << "Enter length of one side of the square : ";
                 cin >> sidea;
                 answer = sidea * 4;
                 cout << "The perimeter of the square is : " << answer << "\n";
                 Square();
                 break;
         case 2: cout << "Enter length of one side of the square : ";
                 cin >> sidea;
                 answer = sidea * sidea;
                 cout << "The area of the square is : " << answer << "\n";
                 Square(); 
                 break;
         }
}         

int Rect () {
    float sidea, sideb, height, answer;
    cout << fmenu;
    Userchoice2 = GetUserChoice();
    if (Userchoice2 == 0)
       Root();
    else switch (Userchoice2) {
         case 1: cout << "Enter length of side one of the rectangle : ";
                 cin >> sidea;
                 cout << "Enter lenght of side two of the rectangle : ";
                 cin >> sideb;
                 answer = (sidea * 2)+(sideb * 2);
                 cout << "The perimeter of the rectangle is : " << answer << "\n";
                 Rect();
                 break;
         case 2: cout << "Enter length of side one of the rectangle : ";
                 cin >> sidea;
                 cout << "Enter lenght of side two of the rectangle : ";
                 cin >> sideb;
                 answer = sidea * sideb;
                 cout << "The area of the rectangle is : " << answer << "\n";
                 Rect(); 
                 break;
         }
}         

int Para () {
    float sidea, height, answer;
    cout << fmenu;
    Userchoice2 = GetUserChoice();
    if (Userchoice2 == 0)
       Root();


    else switch (Userchoice2) {
         case 1: cout << "Enter length of one side of the parallelogram : ";
                 cin >> sidea;
                 answer = sidea * 4;
                 cout << "The perimeter of the parallelogram is : " << answer << "\n";
                 Para();
                 break;
         case 2: cout << "Enter length of one side of the parallelogram : ";
                 cin >> sidea;
                 cout << "Enter height the parallelogram : ";
                 cin >> height;
                 answer = sidea * height;
                 cout << "The area of the parallelogram is : " << answer << "\n";
                 Para(); 
                 break;
         }
}         

int Circle () {
    float radius, diameter, circ, answer;
    cout << "1: Circumference (Diameter) \n2: Circumference (Radius) \n3: Area (Diameter) \n";
    cout << "4: Area (Radius) \n0:Return to root \n";
    Userchoice2 = GetUserChoice();
    if (Userchoice2 == 0)
       Root();
    else switch (Userchoice2) {
         case 1: cout << "Enter the diameter of the circle : ";
                 cin >> diameter;
                 answer = PI * diameter;
                 cout << "The circumference of the circle is : " << answer << "\n";
                 Circle ();
                 break;
         case 2: cout << "Enter radius of the circle : ";
                 cin >> radius;
                 answer = 2*PI*radius;
                 cout << "The circumference of the circle is : " << answer << "\n";
                 Circle ();
                 break;
         case 3: cout << "Ender the diameter of the circle : ";
                 cin >> diameter;
                 radius = diameter/2;
                 answer = PI * (radius * radius);
                 cout << "The area of the circle is : " << answer << "\n";
                 Circle ();
                 break;  
         case 4: cout << "Ender the radius of the circle : ";
                 cin >> radius;
                 answer = PI * (radius * radius);
                 cout << "The area of the circle is : " << answer << "\n";
                 Circle ();
                 break;    
            }        
}





////Main procedure//////////////////////    
int main () {
    Root ();   
    
}


thanks
J
Last edited on
I would say you are developing some pretty good habits for a beginner - you have good indentation, are trying to make the program modular, and are trying to reuse code as much as possible.

A few small things:
1 - There are a lot of accepted ways to line up your braces ( {, } ), but all of them have one thing in common - you should line the ending brace up vertically with the start of the structure that opens it.
Ex
[code=c++]
// Example 1
if( condition ) {
code
} // lines up with the "if" that open's the brace

// Example 2
if( condition )
{
code
} // again, lines up with the "if"
[/code]

2 - It's a good idea to avoid NOT including braces in single statement if's. This is because if you go back later and add more code into the if, and you forget to add the braces, you will have a VERY hard time debugging what is going on. I would suggest always putting on braces.

3 - The only major "design" issue I would have, is that the Square(), Circle(), etc. functions should NOT call Root(). This is because you should be able to call these functions from anywhere, not just Root(). To solve this problem while removing the Root() call, you should instead have a loop inside the Root() function to continuously redisplay the menu and call the appropriate functions.
Also, the purpose of pointers is not to optimize memory usage. The main reason to use pointers (although not the only one), has to do with function scope. If you don't know about scope, you should read up on functions and scope more first. Basically, the scope of a function is the variables a function can "see" and access. A function can't access variables that are outside of itself, and although you can send variables as parameters to a function from outside, if they are passed normally (by value), then the function only gets a copy of the variables, so if you alter it, it will not change the variable outside the function. If you instead pass a pointer to the variable you want to change to the function (passing by reference), you can now directly change the variable's value in memory from inside the function, and when the function is over, these changes will be seen outside the function as well.
Thanks for the input. Most of my habits are from prior coding experience (basic and pascal although those were more than 11 years ago) however this is my first crack at C++ (probably should have said this in my original post).

I see what your saying about lining up the brackets ( {} ) I thought of that while I was writing this but after scrapping the first two design ideas it got lost in the process.

I think I understand what your getting at with the Root() calls however i am coming to the end of a 12 hr shift and my brain is running on empty so once I get some sleep i will try to rehash out the issue ..

what you said about pointers is what I've been trying to wrap my head around in the tutorials. It will probably be one of those things that I'll have to use once or twice before I really get it. The reason I was thinking about using less memory or better memory allocation was because instead of letting the computer assign a different memory address for each variable each time it is called you could reuse the same memory address over and over.. once again this assumption has a lot to do with not know how or when a addressed location by a variable is cleared or how C++ assigns variables in a memory aspect. Once again I could still be thinking of that idea wrong.

thanks again
Be careful how you present your advice. If it is opinion, don't state it as fact.

1. His indent style is called Banner style. (And a couple instances of Pico style.)
http://en.wikipedia.org/wiki/Indent_style

There are several things to remember about indent styles
- there is no such thing as "The One True Brace Style" --no matter how much K&R advocates (or <your favorite style advocates>) would like you to believe it.
- when coding, consistency is more important than pretty-printers
- when working on a team project, use the team project's style guidelines
- everything else is opinion and fluff

2. It is a good suggestion, but not a good idea. Readable code does not depend on braces. The following are equally valid:

if (foo) if (foo) {
baz(); baz();
}

...and equally easy to modify with more or less statements. Anyone with minimal competency in C or C++ can recognize the differences.

3. Agreed. The fatal issue is that the stack always grows but never shrinks. For a small program like this it probably isn't an issue, but it is a bad habit.

C++ forbids users from calling main() in their programs (and it has always been a bad thing in any case) in part for this very reason. The Root() function is just a substitute for main(), so that same flaw is not obviated.

When doing something multiple times, a loop is almost always the better choice.

You can fix it easily enough, due to your nice, modular design:

1. Change all calls to Root() to a simple return. (Your functions all say they return int, but only GetUserChoice() and Root() actually do. You should also change all those to void.)

2. Change main() to use a loop:
1
2
3
4
5
int main()
  {
  while (Root());
  return 0;
  }


3. Add a non-terminating return code to the end of Root() (between lines 48 and 49):
1
2
3
4
           case 4: Circle (); break;
           }
      return 1;  // not zero, so main() won't quit
}


Hope this helps.
Duoas thanks for the fix for the root() flaw in this. I didn't know that return could be used like that.

Er, yeah. Just to make sure we aren't mis-reading each other, my main() lines 3 and 4 are distinct (unrelated) statements.
1
2
3
4
5
  while (Root())
    ;  // <-- semicolon indicates empty body, or null statement
       //      so essentially: while Root() returns non-zero, do nothing...

  return 0;
Oh i was talking about use of return in place of the root() calls..
Return is used to make the function...er...return the value...i.e.:

1
2
3
4
5
6
7
8
9
10
//include headers blah blah

string StringFunc(void) {
    return "String";
}

int main() {
    string test = StringFunc(); //<-- test becomes "String" because that is what StringFunc() returns "String"
    return 0;
}
Good discussion guys! OP (owner of the post) has a good understanding as a beginner. All the best.
Rajendra
Agreed. :-)
Topic archived. No new replies allowed.