Functions inside while loop; random outputs?

Hi. I have this project for school. I thought I had the idea of functions understood but the responses I'm getting from the console seem completely random. It's kind of hard to explain, I'm sure you guys would know immediately what the problem was if you ran this. Thanks.

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
  //Nightmarish Area Program
//Stephan Pichardo - 10/17/14

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

void title(void);
char shape(void);
double triangle(void);
double rectangle(void);
double circle(void);

double total = 0;
const double PI = 4 * atan(1.0);

int main()
{
	void title();
	char shape();
	
	while ((shape() == 'C')||(shape() == 'R')||(shape() == 'T'))
	{
		if (shape() == 'C')
		{
			double circle();
			total += circle();
		}
		else if (shape() == 'R')
		{
			double rectangle();
			total += rectangle();
		}
		else 
		{
			double triangle();
			total += triangle();
		}
		cout << "\nThe area so far is " << total << ".";

		char shape();
	}

	cout << "\nThe total area is " << total << ".\n";
	return 0;
}

void title()
{
	cout << "Computes the area of an irregular shape made up of smaller user-defined";
	cout << "\ncircles, rectangles, and triangles.";
}

char shape()
{
	char choice;

	cout << "\nInput a shape (C/T/R): ";
	cin >> choice;
	if (choice > 90)
		choice -= 32;

	return choice;
}

double circle()
{
	double radius, fraction, area;

	cout << "\nInput radius: ";
	cin >> radius;
	cout << "\nInput fraction: ";
	cin >> fraction;

	area = PI * radius * radius * fraction;

	return area;
}

double rectangle()
{
	double length, height, area;

	cout << "\nInput length: ";
	cin >> length;
	cout << "\nInput height: ";
	cin >> height;

	area = length * height;

	return area;
}

double triangle()
{
	double base, area;

	cout << "\nInput base: ";
	cin >> base;

	area = (base * sqrt(base - (base / 2.0))) / 2.0;

	return area;
}



1
2
3
4
5
6
7
8
9
10
11
12
double circle()
{
	double radius, fraction, area;

	cout << "\nInput radius: ";
	cin >> radius;
	cout << "\nInput fraction: ";
	cin >> fraction;

	area = PI * radius * radius * fraction;

	


Go look at the formula for the area of a circle. Its not what you have coded on this section of the code

Your also not storing the user input anywhere.

 
	while ((shape() == 'C')||(shape() == 'R')||(shape() == 'T'))


For example say the user inputs 'C'

Well your program kinda just doesn't do anything with it, because the program doesn't store it in any variable. So that's why it keeps double repeating the command "Input a shape (C/T/R):"
Last edited on
Apart from the "fraction" bit, the area of a circle formula looks fine to me. But yeah.. what is that fraction thing supposed to be?


Anyway... you are doing very strange things with your functions, which is why you are getting strange behavior:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// These are all prototypes.  There are no problems here:
void title(void);
char shape(void);
double triangle(void);
double rectangle(void);
double circle(void);
//  They simply tell the compiler that the functions exist in your program.  So now you can
//  call them.


//  Although the 'void' in the parameter list is entirely unnecessary... you could just do this:
void title();
char shape();
double triangle();
double rectangle();
double circle();



What gets weird is in your main function:

1
2
3
4
5
6
7
8
9
int main()
{
    void title();   // <- this prototypes 'title' AGAIN, even though you just did it above.
    char shape();  // <- same with 'shape'.  These two lines do absolutely nothing
         // and are completely unnecessary
         
    // You do this again on lines 26, 31, and 36 with circle/rectangle/triangle.
    // And again on line 41 with shape.
    //  Get rid of that crap. 


And this is probably the biggest goof:

 
    while ((shape() == 'C')||(shape() == 'R')||(shape() == 'T'))


Each one of those shape()s is you calling the function. IE: the function name + parenthesis means you are calling the function. Remember that when you call a function, your program effectively "jumps" to the code inside that function, then "jumps back" once the function is complete.

Knowing that... notice how your 'shape' function prompts the user for input. This means that every time you call the 'shape' function, you will be prompting the user.

On that above line... you are calling the function three times. This means you will be prompting the user 3 times [max], once for each call.

Now it's not ALWAYS 3 because of the way the || operator works. Basically it'll do the first expression first (shape == 'C') and will only do the next one if that is false.

So.... let's walk through what the computer is doing.

First... the computer will do the underlined portion:
 
while (   (shape() == 'C')  ||(shape() == 'R')||(shape() == 'T'))


Here, it calls 'shape', which prompts the user for input.
If they input 'C', then this entire line of code is done (because the condition is true), and the loop body will execute.

If they input anything else... then it moves on to the next portion:

 
while (   (shape() == 'C') || (shape() == 'R') ||(shape() == 'T'))

Here, it calls shape again, which prompts the user for input again.
This time, if they input 'R', then the condition is true and the loop body will execute.

Otherwise, it moves onto the last portion:
 
while (   (shape() == 'C') || (shape() == 'R') || (shape() == 'T') )

Here, it calls shape yet again, which prompts the user for input yet again.
This 3rd time, if they input 'T', then the condition is true and the loop body will execute.

Otherwise, the entire loop condition is false, and the loop will exit.




You probably do not want to call the function 3 times like this. You should be calling it once, and be putting the results in a variable which you can look at several times.

Example:

1
2
3
4
5
char usershape = shape();  // call the shape function to ask the user for input, then
       // put their input in a 'usershape' variable.
       
// THEN you can do this without calling the function several times:
while( (usershape == 'C') || (usershape == 'R') || (usershape == 'T') )
Wow, thanks for your time in that write up. It was helpful. Funny you should mention that weird fraction thing; I thought it was stupid too but it might help to actually link to the assignment so you can see it is in fact required http://www2.sunysuffolk.edu/lambiar/Homeworks/HW06/hw06.htm .

So I sorted some of those things out, now I'm down to I think just one problem. The first selection works well, but no matter what you input as your shape the second time around it always goes to the function for the shape you selected first. You'll see. I imagine it has something to do with line 43 but I have no idea what to do about it.

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
//Nightmarish Area Program
//Stephan Pichardo - 10/17/14

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

void title();
char shape();
double triangle();
double rectangle();
double circle();

double total = 0;
const double PI = 4 * atan(1.0);

int main()
{
	title();

	char usershape = shape();
	
	while( (usershape == 'C') || (usershape == 'R') || (usershape == 'T') )
	{
		if (usershape == 'C')
		{
			double usercircle = circle();

			total += usercircle;
		}
		else if (usershape == 'R')
		{
			double userrectangle = rectangle();
			total += userrectangle;
		}
		else 
		{
			double usertriangle = triangle();
			total += usertriangle;
		}
		cout << "\nThe area so far is " << total << ".\n";

		char usershape = shape();
	}

	cout << "\nThe total area is " << total << ".\n";
	return 0;
}

void title()
{
	cout << "Computes the area of an irregular shape made up of smaller user-defined";
	cout << "\ncircles, rectangles, and triangles.\n";
}

char shape()
{
	char choice;

	cout << "\nInput a shape (C/T/R): ";
	cin >> choice;
	if (choice > 90)
		choice -= 32;

	return choice;
}

double circle()
{
	double radius, fraction, area;

	cout << "\nInput radius: ";
	cin >> radius;
	cout << "\nInput decimal fraction: ";
	cin >> fraction;

	area = PI * radius * radius * fraction;

	return area;
}

double rectangle()
{
	double length, height, area;

	cout << "\nInput length: ";
	cin >> length;
	cout << "\nInput height: ";
	cin >> height;

	area = length * height;

	return area;
}

double triangle()
{
	double base, area;

	cout << "\nInput base: ";
	cin >> base;

	area = (base * sqrt(base - (base / 2.0))) / 2.0;

	return area;
}


I'm having a hard time with this professor. He's crazy about making us do pseudocode on paper first, and I see the benefit and do what I can. But then he insists that if it makes sense in pseudocode, it will make sense in C++ which I have of course found to be lies. What makes sense to me will never make sense in C++ because I'm new to programming and I don't know what C++ wants from me... Then instead of going over even basic syntax and stuff, he spends class time lecturing more about how you need to do pseudocode and leaves you to teach yourself C++ so you can complete the homework assignments. What you said about making a variable out of the function value was NEVER TOUCHED in class this week- not even close. I appreciate your help.

Last edited on
What you said about making a variable out of the function value was NEVER TOUCHED in class this week- not even close.


Well you can assign anything to a variable. Variables are just like boxes that a value can be stored in. Since a function returns a value... you can put it in that box just like anything else.


The first selection works well, but no matter what you input as your shape the second time around it always goes to the function for the shape you selected first. You'll see. I imagine it has something to do with line 43 but I have no idea what to do about it.


line 43 is very close to what you want... but you are doing something slightly wrong.


Whenever you use a type name (like char or int) you are declaring something. (this isn't always true, but it's a good general rule for a beginner -- just know that there will be exceptions to that rule when you learn more about the language)

int foo; <- This declares an integer named foo. That creates a new variable.

Now take a look at lines 21 and 43:

1
2
3
4
5
// line 21:
char usershape = shape();  // <- declares a variable named usershape, fills it with whatever shape() returns

// line 43:
char usershape = shape();  // <- same thing 


Notice that you are declaring a variable twice. This will actually create 2 separate variables.

The while loop on line 23 is looking at the 1st variable since that was the only one declared so far... but line 43 is assigning the 2nd variable... leaving the first unchanged.
Last edited on
Ok, I got it done. Thanks alot guys for your help. I will probably be back soon.
Topic archived. No new replies allowed.