Eliminate goto Statements

My teacher wants me to get rid of all of the goto statements in my code. I have already restructured the code to eliminate many, but there are still some that I do not know how to get the code to work without. Any suggestions?

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

// Luca Del Signore
// Computer Science II
// Draw a Line Program

#include <iostream>
using namespace std;
void line_specs(int& length, char& material, const int& border_length);
void border(const int& border_length);
int main()
{
    char yesno, line_type, material;
    int length, foo;
    const int border_length = 20;
    
    
    // Program Introduction
    cout << "Welcome to the line drawing program." << endl;
start:
    cout << "Would you like to draw a line?(y/n)" << endl;
    cin >> yesno;
    if ((yesno == 'n') || (yesno =='N'))
        cout << "END PROGRAM";
    else if ((yesno == 'y') || (yesno =='Y'))
    {
        border(border_length);
        goto affirmative;
    }
    else
    {
        cout << "ERROR" << endl;
        goto start;
    }
    
    
    
    // Line Type Decision
affirmative:
    cout << "Enter the line type." << endl;
    cout << "'V' for vertical, 'H' for horizontal, 'F' for diagonal-foreward, or 'B' for diagonal-backward ." << endl;
    cin >> line_type;
    if ((line_type == 'v') || (line_type == 'V'))
    {
        border(border_length);
        line_specs(length, material, border_length);
        for (int i = 0; i < length; i++)
        {
            cout << material << endl;
        }
        goto ask;
    }
    else if ((line_type == 'h') || (line_type == 'H'))
    {
        border(border_length);
        line_specs(length, material, border_length);
        for (int i = 0; i < length; i++)
        {
            cout << material << " ";
        }
        cout << endl;
        goto ask;
    }
    else if ((line_type == 'f') || (line_type == 'F'))
    {
        border(border_length);
        line_specs(length, material, border_length);
        for (int i = 0; i < length; i++)
        {
            for (int x = i; x > 0; x--)
                cout << " ";
            cout << material << endl;
        }
        cout << endl;
        goto ask;
    }
    else if ((line_type == 'b') || (line_type == 'B'))
    {
        border(border_length);
        line_specs(length, material, border_length);
        foo = length;
        for (int i = 0; i < length; i++)
        {
            for (int x = 0; x < foo; x++)
                cout << " ";
            cout << material << endl;
            foo--;
        }
        cout << endl;
        goto ask;
    }
    else
        cout << "ERROR" << endl;
    goto affirmative;
    


    
    
    cout << "Would you like to draw another line?" << endl;
    cin >> yesno;
    while ((yesno != 'y') && (yesno != 'Y') && (yesno != 'n') && (yesno != 'N'))
    {
        cout << "INVALID INPUT" << endl;
        cout << "Would you like to draw another line?" << endl;
        cin >> yesno;
    }
    if ((yesno == 'n') || (yesno == 'N'))
        cout << "END PROGRAM" << endl;
    else if ((yesno == 'y') || (yesno == 'Y'))
        goto affirmative;
    return 0;
}




// Line Specification Function
void line_specs(int& length, char& material, const int& border_length)
{
    cout << "Input the desired length of the line." << endl;
    cin >> length;
    border(border_length);
    cout << "Input the desired character with which" << endl;
    cout << "to build the line." << endl << endl;
    cin >> material;
    border(border_length);
    
}

//Border Formating Function
void border(const int& border_length)
{
    for (int i = 0; i < border_length; i++)
        cout << "- ";
    cout << endl;
}
You can restructure your program to use functions to encapsulate code that you need to run in more than one place.

For situations where you need to execute some code, and then return to an earlier statement and re-execute the same code, you can use for-loops or while-loops.
Loops could be a logical alternative.

However, your program does already have unexpected things. Line 23 says that the program ends, but the next executed line, 39, asks for line type.

Lines 19-33 effectively ask the user a (y/n) question, but lines 99-110 essentially ask the same question again. The question could be a separate function that loops until the user gives a valid input, and then returns a boolean.
I've got it figured out, thanks.
> there are still some that I do not know how to get the code to work without.
> Any suggestions?

Pedantically replacing gotos with loops, while retaining the original poor code structure usually leads to even poorer code.

Restructure the code. May be something like this:

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
bool yes_or_no( const char* prompt ) // returns true for yes
{
    cout << prompt << " (y/n): " ;
    char yesno ;
    cin >> yesno ;
    if( (yesno == 'y') || (yesno =='Y') ) return true ;
    else if( (yesno == 'n') || (yesno =='N') ) return false ;
    else cout << "***error: either 'y' or 'n' is required.\n" ;

    return yes_or_no(prompt) ; // error, try again
}

int main()
{
    // ...

    // Program Introduction
    cout << "Welcome to the line drawing program." << endl;

    // start:
    while( yes_or_no( "Would you like to draw a line?" )  )
    {
        // affirmative:
        cout << "Enter the line type." << endl;
        cout << "'V' for vertical, 'H' for horizontal, ... etc.: " ;
        char line_type ;
        cin >> line_type;

        switch(line_type)
        {
            case 'V' : case 'v' :
            {
               // draw veritcal line; perhaps call a function draw_vert_line(....)
               // ...
            }
            break ;

            case 'H' : case 'h' :
            {
               // draw horizontal line; call a function draw_horiz_line(....)
               // ...
            }
            break ;

            // likewise. for diagonal, diagonal-foreward, diagonal-backward

            default:
                 std::cout << "error in input\n" ;
        }
        
        // unless the house is on fire, do not shout
        cout << /*"END PROGRAM" << endl; */ "end program.\n" ; 
    }
}
You HAVE to learn how to do this, because it is only going to become more complex. You need to use for-loops, while-loops, do-while-loops, functions, and switch-statements.
Topic archived. No new replies allowed.