Trouble with if-else versus switch-case

Greetings,

I am quite new to the programming world and am currently enrolled in a college course on C++ programming. That being said, we are only starting on the for and while loops so any help more advanced than that I won't understand you. At least not until I look it up and learn what it is. In any case, I am working on a simple program to calculate the circumference of an ellipse based on two numbers requested from the user. The idea with our lab isn't so much simply being able to ask for the two numbers and spitting out an answer, but to catch if the user inputs something other than a positive integer. i.e. negative numbers, same number twice, etc. Below is a copy of my code as well as a script I ran that shows some of the output. I am running into the problem of the user putting in not just the same number (which results in a circle) but if they put in two zeros or one zero and one positive number. Obviously a zero and a positive number is simply a line of said length. Any help would be greatly appreciated. I used italics to show the code versus me asking questions. Thanks in advance.

P.S. Josh is my lab instructor so the references to him in the code are simply me being silly.

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
#include <iostream>
#include <cmath>
#include <cstring>

using namespace std;

int main()
{

	double min, maj, c;

	const double PI=3.1415926535898;

	int x = 0;

	cout << "This program will give you the " << endl;
	cout << "circumference of an ellipse given " << endl;
	cout << "the major and minor axis." << endl << endl;

	cout << "Please enter the minor axis: " << flush;

	cin >> min;

	cout << "Please enter the major axis: " << flush;

	cin >> maj;

	if (cin.fail())
		{
		cout << endl << endl << "Error: Not an integer. " << endl << endl;
		return 1;
		}

	else if(min == maj)
		{
		cout << "Josh, based on the numbers you entered you " << endl;
		cout << "have a circle, not an ellipse." << endl << endl;

		c = 2*PI*sqrt((pow(min,2)+pow(maj,2))/2);  // Calculates the circumference of the circle.

		cout << "The circumference of the circle based " << endl;
		cout << "upon your inputs is: " << c << endl << endl;
		}

	else if ( (min == 0) || (maj > 0) )
		{
		cout << endl << "Josh, you have entered a zero " << endl;
		cout << "for the minor axis.  Zeros will " << endl;
		cout << "not give you a circle nor an ellipse. " << endl;
		cout << "Based on your entry you have a line of " << endl;
		cout << "length " << maj << endl;
		}

	else if ( (min > 0) || (maj == 0) )
		{
		cout << endl << "Josh, you have entered a zero " << endl;
		cout << "for the major axis.  Zeros will " << endl;
		cout << "not give you a circle nor an ellipse. " << endl;
		cout << "Based on your entry you have a line of " << endl;
		cout << "length " << min << endl;
		}

	else if ( (min == 0) || (maj == 0) )
		{
		cout << endl << "Josh, you have entered a zero " << endl;
		cout << "for both of the axis.  Zeros will " << endl;
		cout << "not give you a circle nor an ellipse. " << endl;
		cout << "Stop being silly! " << endl;
		}

	else if ( (min < 0) || (maj > 0) )
		{
		cout << endl << "Josh, you have entered a negative " << endl;
		cout << "number for the major axis.  Negative numbers " << endl;
		cout << "will not give you a circle nor an ellipse. " << endl;
		cout << "Stop being silly! " << endl;
		}
	
	else if ( (min > 0) || (maj < 0) )
		{
		cout << endl << "Josh, you have entered a negative " << endl;
		cout << "number for the minor axis.  Negative numbers " << endl;
		cout << "will not give you a circle nor an ellipse. " << endl;
		cout << "Stop being silly! " << endl;
		}

	else
	{
		c = 2*PI*sqrt((pow(min,2)+pow(maj,2))/2);  // Calculates the area of an ellipse.
	
		cout << "The circumference of the ellipse based " << endl;
		cout << "upon your inputs is: " << c << endl << endl;
	}

	return 0;
}



The output shows like this:

Script started

This program will give you the 
circumference of an ellipse given 
the major and minor axis.

Please enter the minor axis: 0
Please enter the major axis: 0

Josh, you have entered a zero 
for both of the axis.  Zeros will 
not give you a circle nor an ellipse. 
Stop being silly! 

bash-3.2$ ./Lab3-test

This program will give you the 
circumference of an ellipse given 
the major and minor axis.

Please enter the minor axis: 0
Please enter the major axis: 5

Josh, you have entered a zero 
for both of the axis.  Zeros will 
not give you a circle nor an ellipse. 
Stop being silly! 

bash-3.2$ ./Lab3-test

This program will give you the 
circumference of an ellipse given 
the major and minor axis.

Please enter the minor axis: 5
Please enter the major axis: 0

Josh, you have entered a zero 
for both of the axis.  Zeros will 
not give you a circle nor an ellipse. 
Stop being silly! 

bash-3.2$ exit
exit

Script done


I thought about trying to use a switch-case statement in order to get around this problem but I can't seem to get it right. I may end up taking the sections I am having problems with out completely as it is only extra credit and won't hurt my overall score. Again, any help would be appreciated.
Last edited on
You should put your code [code]inside code tags[/code] and your output [output]inside output tags[/output].

Just edit your post above with the little icon that looks like a pencil on paper ;)
Last edited on
Thanks for that. Sometimes I get ahead of myself and miss the little things that will make it easier for others to read my code. Sigh. *smacks self in head*
Funny. I don't have this problem.

-Albatross
Nice. Heh heh.

By the way, let me be clear, this IS for a lab assignment that is due this week. I am however NOT looking for any kind of answer just maybe a shove in the right direction. It doesn't do me any good to get the answer from someone if I am trying to learn the material. Also, The program/code is completed for the assignment so I will get full credit if I take out the statements listed below. I am just messing around and tweaking it a little to see if I can catch other things that weren't asked for in the assignment.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	else if ( (min == 0) || (maj > 0) )
		{
		cout << endl << "Josh, you have entered a zero " << endl;
		cout << "for the minor axis.  Zeros will " << endl;
		cout << "not give you a circle nor an ellipse. " << endl;
		cout << "Based on your entry you have a line of " << endl;
		cout << "length " << maj << endl;
		}

	else if ( (min > 0) || (maj == 0) )
		{
		cout << endl << "Josh, you have entered a zero " << endl;
		cout << "for the major axis.  Zeros will " << endl;
		cout << "not give you a circle nor an ellipse. " << endl;
		cout << "Based on your entry you have a line of " << endl;
		cout << "length " << min << endl;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	else if ( (min < 0) || (maj > 0) )
		{
		cout << endl << "Josh, you have entered a negative " << endl;
		cout << "number for the major axis.  Negative numbers " << endl;
		cout << "will not give you a circle nor an ellipse. " << endl;
		cout << "Stop being silly! " << endl;
		}
	
	else if ( (min > 0) || (maj < 0) )
		{
		cout << endl << "Josh, you have entered a negative " << endl;
		cout << "number for the minor axis.  Negative numbers " << endl;
		cout << "will not give you a circle nor an ellipse. " << endl;
		cout << "Stop being silly! " << endl;
                }
Last edited on
You probably don't want logical ORs in your if statements; try logical ANDs instead (that's &&).
I don't think you can use switch in this case because you don't have definite outputs.

Both min and max have three basic states: negative, zero and positive.

You could make a case for 0 and since positive is correct, you could make it the default case, but you have no way of knowing a priori the negative values each of them can have.

Or, I mean, you could by using an invented index saying something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int _min;
if(0>min)
   _min = -1;
else if(0==min)
   _min = 0;
switch min_neg
{
   case -1:
   //...
   break;
   case 0:
   //...
   break;
   default:
   //...
   break;
}

Or something (then repeat for maj), but I don't think it's worth it, quite honestly.

I'd suggest simply removing the else-if statements and substituting them for simple if-statements. It makes it go through each of them, but it's safer, since you've got so many combinations to watch for.

You can instead simply use
1
2
3
4
5
6
7
8
if(0==min)
   //no minor axis
else if(0>min)
   //negative minor axis
if(0==max)
  //no major axis
else if(0>max)
  //negative major axis 


This is also better because it allows the user to input the minor and major axes in any order (you have no check as is). Actually, you should call them x-axis and y-axis, since perhaps your user wants to make a vertical ellipse, not a horizontal one.

As well, what's wrong with negative axes? Aren't their values simply squared?
Last edited on
Topic archived. No new replies allowed.