if / while / loops (beginning beginner)

I'm writing a simple program to tell me the area of figures give their dimensions, but in an attempt to make a tad more useful I got myself into a hole. I'm not quitting yet, but after searching on the internet for a while I just gave up.

The math itself isn't a problem, its getting it to loop right. From the examples I've seen I cannot figure out how to make it return from inside one of the two parts back to the beginning to let it do both a circle and a triangle without having to exit. Going into triangles means a 1 with let it repeat, and anything else will close it, while in circles I can't get it to either close or return.

I know it'll have a rather simple an obvious answer I completely overlooked, but help?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
cout<<"Hello, I will help you with common math formulas.\n";
	cout<<"For circles, press 1 then enter. \n";
	cout<<"For triangles, press 2 and enter."<<endl;
	cin>>x; {
	while (x==1) {
	cout<<"Type in the radius of a circle to calculate its dimensions."<<endl;
	cin>>r; //Get radius
	d=(r*2);
	c=(d*3.14);
	a=(r*r*3.14); //Calculate dimensions of the circle
	cout<<"The radius is "<<r<<". The diameter is "<<d<<". The circumference is "<<c<<".\n The area is "<<a<<".";
	cin>>a; }
	while (x==2) {
		cout<<"Area of a triangle: Type in the base and enter. \n"; //Input Height/Base
		cin>>b;
		cout<<"Type in the height and enter. \n";
		cin>>h;
		a2=.5*b*h; // Calculate area.
		cout<<"The area of the triangle is "<<a2<<".";
The short answer is you need to loop your input with a do...while loop that has an end condition that you can input.
1
2
3
4
5

do {
//stuff

}while (some_input != exit_value)


The long answer is I slapped together a rewritten version to give you some ideas of how it should look and what things you can do differently -- chief among them, giving your variables descriptive names! This will save you so much effort in the long run! Another one is keeping interface and implementation separate, but that won't be a big deal until you start writing longer programs.

Some of the stuff I used is a little beyond where I'm guessing you're at, but you'll be learning it soon so this should give you an additional example for how most of it is used.

I'd be happy to explain anything below that you're curious about -- but if you don't want to see a solution before you take a shot, consider the following SPOILERS:

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
#include <iostream>
using namespace std;

void get_info_circle();
void get_info_triangle();

int main() {
	cout<<"Hello, I will help you with common math formulas.\n";
	

	int choice =0;
	while(true) {
		cout << "Enter 1 for circles.";
		cout << "\nEnter 2 for triangles."<<endl;
		cout <<"Enter 0 to quit." << endl;
		cout << ">> ";
		cin >> choice;

		switch (choice){
			case 1:
				get_info_circle();
				break;
			case 2:
				get_info_triangle();
				break;
			default:
				cout << "\nGuess we're done!" << endl;
				return(0);
		}
	}
}

void get_info_circle(){
	int radius =0;
	const double pi = 3.14;

	cout << "\nType in the radius of a circle to calculate its dimensions: ";
	cin >> radius;
	cout << "\nThe radius is: " << radius << endl;

	int diameter = radius + radius;
	
	cout << "The diameter is: " << diameter << endl;
	cout << "The circumfrence is: " << diameter * pi << endl;
	
	double area = (radius * radius) * pi;
	
	cout << "The area is: " << area << endl;
	cout << "\n          Returning to main menu!\n" << endl;
}

void get_info_triangle(){
	cout << "\nType in the base of the triangle: ";
	int base =0;
	cin >> base;
	
	cout << "Type in the heigh of the triange: ";
	int height =0;
	cin >> height;

	int area = (base * height)/2;
	cout << "\nThe area of the triangle is: " << area << endl;
	cout << "\n           Returning to main menu!\n" << endl;
}
Last edited on
I heard that this is bad to do but could he not just throw in a goto line command? Why is this so bad to use if your careful? It is because you may change the line? I know in fortran we could call any line we wanted to. like I could call line 50, 10 if I wanted to, and could tell the program to goto 10 and it would go to whatever line was called 10.

For example at the end of his code could he put

1
2
3
4
5
6
7
8
9
10
 cout>> "Would you like to begin again? 1(yes) 2(no,quit)";
cin<< x;
if (x==1)
{
goto line; //(Not sure how to use this but I think you type line then the number

}
;
return 0;
You shouldn't use goto until you absolutely must, and if you're not even sure how to implement it in this language you definitely shouldn't use it. In C++, almost every time a goto statement could be used, a function or a loop would have made more sense.

If you use functions and loops you can take advantage of scope to reuse similar variable names and call destructors automatically. That's a huge benefit. Plus, it keeps everything relevant to an operation in one contained space -- the code I wrote up there isn't the most efficient, but it does serve to illustrate this advantage of functions.

All those benefits are magnified when you take advantage of classes to do this kind of stuff.
Thanks, I read about functions (or classes, forgot what that one is) but hadn't thought to nor how to implement it. Guess it does seem like a better choice. And I'll make sure to try descriptive variables, to me they seemed obvious but I guess making the code readable would be good too.

I'll try it out and see what I can come up with.

I get how it works, C++ is pretty readable, but I don't understand just what case and switch do. Along with that, what is the "while (true)" checking for? i
Last edited on
The "while" will execute whatever is inside the "while" until the condition isn't true. It will goback to the beginning of the while loop over and over until the condition isn't true.

for example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main ()
{

int i=1;

         while (i<100)
      {
          cout<< i;
          i=i+1;
      }

return 0;
}


This program will count to 99,
The while will write the value of i and then increase it by 1 and then it will loop back to the beginning and repeat as long as i is less than 100.
Topic archived. No new replies allowed.