Takes Two Inputs To Exit.

Hello! I have some code here that outputs a menu of options to calculate the area of a circle, rectangle, or triangle. It performs these options however long the user wishes by looping main and each individual function in a do while loop. In each function there is a drop down menu much like the one in main that gives the option to calculate the area for the same type of object again, quit the program as a whole, and to return to the main menu. The user must input an integer 1-3 to choose. However, it appears it takes two inputs for it to actually perform. Can anybody explain this? I also have the warnings that were displayed from the debugger below if it helps. One warning also says that the function OptionThree(); will never return a NULL. How can I fix these problems in my code?

Warnings:
Compiling: C:\Users\Owner\Desktop\C++ Files\Geometric Calculator.cpp
C:\Users\Owner\Desktop\C++ Files\Geometric Calculator.cpp: In function 'int main()':
C:\Users\Owner\Desktop\C++ Files\Geometric Calculator.cpp:175: warning: the address of 'int nOptionThree()' will never be NULL
C:\Users\Owner\Desktop\C++ Files\Geometric Calculator.cpp: In function 'int nOptionThree()':
C:\Users\Owner\Desktop\C++ Files\Geometric Calculator.cpp:138: warning: control reaches end of non-void function
C:\Users\Owner\Desktop\C++ Files\Geometric Calculator.cpp: In function 'int nOptionTwo()':
C:\Users\Owner\Desktop\C++ Files\Geometric Calculator.cpp:97: warning: control reaches end of non-void function
C:\Users\Owner\Desktop\C++ Files\Geometric Calculator.cpp: In function 'int nOptionOne()':
C:\Users\Owner\Desktop\C++ Files\Geometric Calculator.cpp:54: warning: control reaches end of non-void function
Linking console executable: C:\Users\Owner\Desktop\C++ Files\Geometric Calculator.exe
Process terminated with status 0 (0 minutes, 2 seconds)
0 errors, 4 warnings




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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
  #include <cstdio>
#include <ctime>
#include <cstdlib>
#include <iostream>
#define PI 3.141592653589793238462643383279502884197169399375

using namespace std;
void displayProgramExplanation()
{
    cout << "This program calculates four things; the area of a circle, the area of a rectangle,\n and the area of a triangle."
         << "These options are displayed below. Select 1-4 to choose what you would like to do." << endl;
}
    int nOptionSelect;
    int nSecondOS;
    int nThirdOS;

int nOptionOne()
{
    do
  {
   system("CLS");

   cout << "Please enter the radius of the circle:";
   double dRadius = 0.0;
   cin >> dRadius;
   double dCArea = PI * (dRadius*dRadius);
   cout << "The area of the circle is " << dCArea << endl;

   cout << "What would you like to do? Choose 1-3.\n"
        << "1. Quit?\n"
        << "2. Go back to the menu?\n"
        << "3. Find another circle's area?";
        cin >> nOptionSelect;

        switch(nOptionSelect)
        {
           case 1:
           return 0;
           break;

           case 2:
           return 1;
           break;

           case 3:
           break;

           default:
           cout << "You didn't enter a valid number.";

        }

}while(nOptionSelect == 3);
}

int nOptionTwo()
{

    do{
    system("CLS");
   cout << "What is the base of the rectangle?\n";
   double BoR = 0.0;
   cin >> BoR;

   cout << "What is the hieght of the rectangle?\n";
   double HoR = 0.0;
   cin >> HoR;

   double dRArea = BoR * HoR;

   cout << "The area of the rectangle is " << dRArea;

   cout << "What would you like to do? Choose 1-3.\n"
        << "1. Quit?\n"
        << "2. Go back to the menu?\n"
        << "3. Find another rectangle's area?";
   cin >> nSecondOS;

   switch(nSecondOS)
   {
           case 1:
           return 0;
           break;

           case 2:
           return 1;
           break;

           case 3:
           break;

           default:
           cout << "You didn't enter a valid number.";

    }
    } while(nSecondOS == 3);
}
int nOptionThree()
{
    do{
    system("CLS");
   cout << "What is the base of the triangle?\n";
   double dBoT = 0.0;
   cin >> dBoT;

   cout << "What is the hieght of the triangle?\n";
   double dHoT = 0.0;
   cin >> dHoT;

   double dTArea = dBoT* dHoT;

   cout << "The area of the rectangle is " << dTArea;

   cout << "What would you like to do? Choose 1-3.\n"
        << "1. Quit?\n"
        << "2. Go back to the menu?\n"
        << "3. Find another rectangle's area?";
   cin >> nThirdOS;

   switch(nThirdOS)
   {
           case 1:
           return 0;
           break;

           case 2:
           return 1;
           break;

           case 3:
           break;

           default:
           cout << "You didn't enter a valid number.";

    }
    } while(nThirdOS == 3);
}
int main()
{
    do
    {
    displayProgramExplanation();
   cout << "1. Calculate the Area of Circle.\n"
        << "2. Calculate the Area of Rectangle.\n"
        << "3. Calculate the Area of Triangle.\n"
        << "4. Quit.\n"
        << "Enter your choice:" << endl;
    int nOption = 0;
    cin >> nOption;
    switch(nOption)
    {
       case 1:
       nOptionOne();
       if(nOptionOne() == 0)
       {
           system("PAUSE");
           return 0;
       }
       else
       break;

       case 2:
       nOptionTwo();
       if(nOptionTwo() == 0)
       {
           system("PAUSE");
           return 0;
       }
       else
       break;

       case 3:
       nOptionThree();
       if(nOptionThree == 0)
       {
           system("PAUSE");
           return 0;
       }
       else
       break;

       case 4:
       system("PAUSE");
       return 0;

    }
    } while(nThirdOS == 2 || nSecondOS == 2 || nOptionSelect == 2);


}
For nOptionThree in the main function, add the bracekts () so it'll be the return value you check.
Also in the case 3 of the option three function add a return 3

Aceix
Last edited on
Some of those compiler warnings point to issues which need to be fixed.
C:\Users\Owner\Desktop\C++ Files\Geometric Calculator.cpp: In function 'int main()':
C:\Users\Owner\Desktop\C++ Files\Geometric Calculator.cpp:175: 
warning: the address of 'int nOptionThree()' will never be NULL

Here is the code which causes that:
173
174
175
176
177
178
179
180
181
       case 3:
       nOptionThree();
       if(nOptionThree == 0)
       {
           system("PAUSE");
           return 0;
       }
       else
       break;

Function nOptionThree() returns an integer, but when the function is called at line 174 nothing is done with that integer. in addition at line 175, the bare name nOptionThree gives simply the address of the function (the location in memory where the code for the function begins). Iinstead, the code might look like this:
 
int option3 = 0; // declare an integer variable and initialise it. 


173
174
175
176
177
178
179
180
181
    case 3:
        option3 = nOptionThree(); // call function and assign returned value to the variable
        if (option3 == 0) // check the value we just stored
        {
            system("PAUSE");
            return 0;
        }
        else
            break;


Alternatively, you can call the function and check its returned value like this, without storing it:
173
174
175
176
177
178
179
180
    case 3:
        if (nOptionThree() == 0) // call function and check the value returned
        {
            system("PAUSE");
            return 0;
        }
        else
            break;



Ok, that's one problem covered. Now the next one.
C:\Users\Owner\Desktop\C++ Files\Geometric Calculator.cpp: In function 'int nOptionThree()':
C:\Users\Owner\Desktop\C++ Files\Geometric Calculator.cpp:138: 
warning: control reaches end of non-void function


Inside the body of function nOptionThree(), at lines 123 and 127 a value is returned. However, it may be possible to reach the closing brace of the function at line 138 without executing a return x, thus the function which is supposed to return an integer might not return a value after all. There should always be some value returned, so add a statement at line 138, such as
 
    return -1; // return a default value 

I put -1 here to indicate that the function has unexpectedly reached the end without returning anything sensible. The choice is up to you, as to whether reaching the last line is a normal or abnormal situation, that's a matter of how you intend the function to work.

Other comments.

Well done for using an accurate value for pi. Though #define is more of a C idiom, in C++ it is preferred to use a constant instead:
 
const double PI = 3.1415926535897932385;


There's a lot of calls to system() which is probably better avoided, firstly is is slow and clunky, it is also generally regarded as bad practice. See http://www.cplusplus.com/articles/j3wTURfi/

edit: at line 13 there are some global variables declared.
13
14
15
    int nOptionSelect;
    int nSecondOS;
    int nThirdOS;
Generally that's something you should avoid. Instead, declare the variables in the function where they will be used. Communicate between functions by passing of parameters when calling the function, and by making use of the returned value.

An aim is to limit the visibility (scope) of the variable to only the part of the program where it will be needed. That reduces confusion as each function has access only to those variables it is supposed to be using, so won't mistakenly read or write something it has no right to be using.

Last edited on
Thank you both! I really appreciate the help!
Topic archived. No new replies allowed.