How can I avoid using 'GoTo'???

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
//Author: Brandon Jackson
//Program Operation: To find Area and Perimeter

        #include <iostream>
        using namespace std;

        int main()
        {
                int Shape = 0;
                int Length = 0;
                int Width = 0;


                cout << "This program will find Area and Perimeter" << endl;
                cout << "First, You need to pick the number of the shape" << endl;
                cout << "You want the Area/Perimeter calculated of" << endl;
                cout << "Then you input the Length followed by the Width" << endl;
                cout << "Here are the shapes available, and the number" << endl;
                cout << "you need to type to use that shape" << endl;
                cout << "Square-1, Circle-2" << endl;
                cout << "Pick the number of the shape you wish to use to continue" << endl;

                cin >> Shape;

                if(Shape == 1)
                {
                        cout << "Input Length" << endl;
                        cin >> Length;
                        cout << "Input Width" << endl;
                        cin >> Width;
                        cout << "The Area of the Square is " << Length * Width << endl;
                        cout << "The Perimeter of the Square is " << 2 * Length + 2 * Width << endl;

                }
                else if(Shape == 2)
                {
                        cout << "Input Radius" << endl;
                        cin >> Length;
                        cout << "The Area of the Circle is " << 3.14 * Length * Length << endl;
                        cout << "The Perimeter of the Circle is " << 2 * 3.14 * Length << endl;
                }


                return 0;
        }

~
~
~
~


Can someone put in the right direction on how I can
1. Loop the code so I can continue using it after I have already found the Area/Perimeter of a shape.(As In, I want to look at a Circle after I look at a square, and vise versa)
2. Keep the code from entering a fail state if someone enters a number that is not listed.(Like if I enter a 3, even though a 3 is not an option)

Thanks in advance
1. Wrap lines 14-41 in a do-while loop - You way want to ask the user whether to restart or end the program after line 41
2. The same for line 23 - If you want really checked input see http://www.cplusplus.com/forum/articles/6046/
oooo who used to program on commodore64 (me2) :D

you may want to check out the tutorial on this website, it is very basic and well written, and includes just about everything you need to know.

http://cplusplus.com/doc/tutorial/

in particular the chapter on control statements.
Last edited on
Topic archived. No new replies allowed.