Undefined variable solution?

If I set to zero, outputs zero.
What else can I do?


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
#include "stdafx.h"
#include <iostream>

using namespace std;

// ========
// Constants
   const double PI = 3.14;
// =======================

// ==========
// Prototypes
   void Banner();
   void ComputeArea(double, double);
   double ComputeCircumference(double);
   void GetValue(double&);
   bool GoAgain();
   void OutputData(double, double, double);
// ========================================

// ===========
    int main()
{
	Banner();

	double radius;
	double area;
	double circumference;

	do {
		radius = 0;
		area = 0;
		circumference = 0;

		GetValue(radius);
		circumference = ComputeCircumference(circumference, radius);
		ComputeArea(area, radius);
		OutputData(radius, area, circumference);

	} while (GoAgain());

	return 0;
}// Function Main()// ================

// ==============
    void Banner()
    {
        cout << " Welcome to my Circle Game! "               << endl;
        cout << " You can input a radius of a circle,"       << endl;
        cout << " I will compute the area and circumference" << endl;
        cout << " to the nearest tenth."                     << endl;
        cout << " Shall we play a game?"                     << endl;
    }// Function Banner()
// ======================

// =================================
    void ComputeArea(double radius, double area)

    {
        
        area = PI * (radius * radius);

    }// Function ComputeArea()
// ===========================

// ====================================
    double ComputeCircumference(double circumference)
    {
        
        double radius;
        circumference = 2 * PI * radius;

        return circumference;


}// Function ComputeCircumfrence()
// ===============================

// ======================
    void GetValue(double& radius)
    {
        
        cout << " Please enter your circles radius: " << endl;
        cin >> radius;

}// Function GetValue()
// ====================

// ===============
    bool GoAgain()
    {
        char input;
        bool validInput;
        do
        {
            cout << " Enter another radius? ";
            cout << " [y/Y] to go again. Or [n/N] to exit: ";
            cin >> input;
            
            if ((input == 'y') || (input == 'Y') ||
                (input == 'n') || (input == 'N'))
                validInput = true;
            else
            {
                validInput = false;

                cout << " I am not programmed to respond in that area." << endl;
                cout << " Please try again." << endl;

            }//else

        } while (!validInput);

        if ((input == 'y') || (input == 'Y')) {
            return true;
        }
        else if
            ((input == 'n') || (input == 'N')) {
            return false;
        }
}// Function GoAgain()
// ===================

// =========================================
    void OutputData(double radius, double area, double circumference)
    {
        cout << " Your results are:";
        cout << endl;
        cout << " You entered: " << radius << " units for the radius." << endl;
        cout << endl;
        cout << " Area:" << area << " units."                          << endl;
        cout << endl;
        cout << " Circumference: " << circumference << " units."       << endl;
        cout << endl;

}// Function OutputData()
// ====================== 



Last edited on
If you set what to zero?
The variables in main were set to zero and they output zero.
I noticed couple of errors you have with your code, on line 36 you send the circumference and the radius to the ComputeCircumference() method even though the function only takes one argument. Another thing, on line 37, you send the area and the radius to the ComputeArea() method in the wrong order according to the function's definition. I also noticed in your ComputeCircumference() method, you multiply 2 * PI * radius where radius is an uninitialized variable.

Also remember that when you pass a variable to a function, the function takes a copy of that variable and doesn't modify the original variable. So in that case, remember to return the result to reflect the change or pass the variable by reference to directly modify the variable.

Hope this helps.
Last edited on
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
#include <iostream>
#include <iomanip>

const double PI = 3.14;

void banner();

// accept radius as input and return the area as the result
double area( double radius ) ;

// accept radius as input and return the circumference as the result
double circumference( double radius ) ;

double get_radius(); // return the value entered by the user

bool again(); // return true to run it again

void output( double radius, double area, double circumference );

int main()
{
    banner();

    do
    {
        const double radius = get_radius() ;

        // pass the radius, area (returned by the function) and
        // circumference (returned by the function) to output
        output( radius, area(radius), circumference(radius) ) ;
    }
    while( again() ) ;
}

void banner()
{
    std::cout << "Welcome to my Circle Game!\n"
    "You can input a radius of a circle,\n"
    "I will compute the area and circumference\n"
    "to the nearest tenth.\n\n" ;
}

double area( double radius ) { return PI * radius * radius ; }

double circumference( double radius ) { return 2 * PI * radius ; }

double get_radius()
{
    std::cout << "Please enter your circles radius: " ;
    double radius ;

    if( std::cin >> radius ) // if the user entered a number
    {
        if( radius > 0.0 ) return radius ; // valid radius was entered, return it
        else std::cout << "please enter a positive value for the radius.\n" ;
    }

    else // non-numeric input eg. "abcd"
    {
        std::cout << "please enter a number.\n"  ;
        std::cin.clear() ; // clear the failed state of the stream
        std::cin.ignore( 1000, '\n' ) ; // and throw the bad input away
    }

    return get_radius() ; // try again
}

bool again()
{
    std::cout << "Enter another radius? [y/Y] to go again. Or [n/N] to exit: " ;
    char input ;
    std::cin >> input;

    if( input == 'y' || input == 'Y' ) return true ;
    else if( input == 'n' || input == 'N' ) return false ;

    std::cout << "invalid input.\n" ;
    return again() ; // try again
}

void output( double radius, double area, double circumference )
{
    std::cout << "Your results are:\n"
              << "You entered: " << radius << " units for the radius.\n\n"
              << std::fixed << std::setprecision(1) // print to to the nearest tenth.
              << "         Area: " << area << " units.\n"
              << "Circumference: " << circumference << " units.\n\n" ;
}
Last edited on
Topic archived. No new replies allowed.