Infinite loop error

Hello. So i am currently in college doing programming and i'm having an issues and no one can assist me right now so i'm posting it here for assistance.
What am i doing wrong here? I keep getting an infinite loop. Can someone explain to me on how to make the loop work until the user enters 7 or F1? And this code is still missing a good amount of things, but i'm just having issues with the loops for the switch statement mainly. The loops starts at line 42 and ends at line 120.

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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <conio.h>

using namespace std;
const int MAX=10;

void display_menu();
void active_numbers(int active_number, int array_numbers[], int current_used_numbers);
bool is_list_empty(int active_number);
void numbers_in_text(const int array_numbers[], int active_number, int current_used_numbers);
void input_key(int answer, int & num1, int & num2);
void sort_array(int array_numbers[], int current_used_numbers, int active_number);
void ending_of_cases(const int array_numbers[], int active_number, int current_used_numbers);
void write_file(ofstream & output_file, const int array_numbers[], int active_number, int current_used_numbers);


int main()
{
    int array_numbers[MAX], current_used_numbers, active_number;
    ifstream test_file_input("test.txt");

    if(test_file_input.fail())
    {
        cout << "File opening failed. Please make sure the file name is test.txt (input)\n";
        exit(1);
    }

    int next;
    test_file_input >> current_used_numbers >> active_number;
    for(int i=0; i<current_used_numbers; i++)
    {
        test_file_input >> next;
        array_numbers[i]=next;
    }
    test_file_input.close();

    display_menu();
    numbers_in_text(array_numbers, active_number, current_used_numbers);
    int answer=getch(), num1, num2;
    do
    {
        input_key(answer, num1, num2);
        switch(num1)
        {
            case 49://1 Button-Insert

                ending_of_cases(array_numbers, active_number, current_used_numbers);
                break;
            case 224://Arrows/Delete/Insert Keys
                switch(num2)
                {
                    case 83://Insert Key-Insert

                        ending_of_cases(array_numbers, active_number, current_used_numbers);
                        break;
                    case 82://Delete Key-Delete

                        ending_of_cases(array_numbers, active_number, current_used_numbers);
                        break;
                    case 77://Right Arrow-Move Right

                        ending_of_cases(array_numbers, active_number, current_used_numbers);
                        break;
                    case 75://Left Arrow-Move Left

                        ending_of_cases(array_numbers, active_number, current_used_numbers);
                        break;
                    case 80://Down Arrow-Select

                        ending_of_cases(array_numbers, active_number, current_used_numbers);
                        break;
                    default:
                        cout << "This is not a valid choice please re-enter a valid input.\n";
                        ending_of_cases(array_numbers, active_number, current_used_numbers);
                }
            case 50://2 Button-Delete

                ending_of_cases(array_numbers, active_number, current_used_numbers);
                break;
            case 51://#3 Button-Sort

                ending_of_cases(array_numbers, active_number, current_used_numbers);
                break;
            case 52://#4 Button-Select

                ending_of_cases(array_numbers, active_number, current_used_numbers);
                break;
            case 53://#5 Button-Move Right

                ending_of_cases(array_numbers, active_number, current_used_numbers);
                break;
            case 54://#6 Button-Move Left

                ending_of_cases(array_numbers, active_number, current_used_numbers);
                break;
            case 55://#7 Button-Exit

                ending_of_cases(array_numbers, active_number, current_used_numbers);
                break;
            case 0://F1 / F2 Buttons
                switch(num2)
                {
                    case 59://F1 Button-Exit

                        break;
                    case 60://F2 Button-Sort

                        ending_of_cases(array_numbers, active_number, current_used_numbers);
                        break;
                    default:
                        cout << "This is not a valid choice please re-enter a valid input.\n";
                        ending_of_cases(array_numbers, active_number, current_used_numbers);
                }
            default:
                cout << "This is not a valid choice please re-enter a valid input.\n";
                ending_of_cases(array_numbers, active_number, current_used_numbers);
        }
    }while(num1 != 0 && num2 != 59 || num1 != 55)
    ofstream output_file("test.txt");
    write_file(output_file, array_numbers, active_number, current_used_numbers);

    return 0;
}

//select next
//insert item
//delete item
//shift right/left
//swap values get index of lowest
//check selection
//array full
//last item selected

//first item selected
//move right/left valid

void write_file(ofstream & output_file, const int array_numbers[], int active_number, int current_used_numbers)
{
    output_file << current_used_numbers << endl << active_number << endl;

    for(int i =0; i<current_used_numbers; i++)
    {
        output_file << array_numbers[i] << ' ';
    }
    output_file.close();
}

void ending_of_cases(const int array_numbers[], int active_number, int current_used_numbers)
{
    system("CLS");
    display_menu();
    numbers_in_text(array_numbers, active_number, current_used_numbers);
}
void input_key(int answer, int & num1, int & num2)
{
    answer;
    num1=answer;
    if(answer==224 || answer == 0)
    {
        answer=getch();
        num2=answer;
    }
}

void display_menu()
{
    cout << "1.Insert     \"Insert\" key\n"
         << "2.Delete     \"Delete\" key\n"
         << "3.Sort       \"F2\" key\n"
         << "4.Select     \"Down Arrow\" key\n"
         << "5.Move Right \"Right Arrow\" key\n"
         << "6.Move Left  \"Left Arrow\" key\n"
         << "7.Exit       \"F1\" key\n\n";
}

void numbers_in_text(const int array_numbers[], int active_number, int current_used_numbers)
{
    for(int i=0; i<current_used_numbers; i++)
    {
        if(array_numbers[i] == array_numbers[active_number])
            cout << '[' << array_numbers[i] << ']';
        else
            cout << array_numbers[i] << ' ';
    }
}

bool is_list_empty(int active_number)
{
    bool status = true;
    if(active_number=0)
        return status;
    else
        return status=false;
}

void move_left(int array_numbers[], int & active_number, int current_used_numbers)
{


}

void swap_values(int & v1, int & v2)
{
    int temp;
    temp = v1;
    v1 = v2;
    v2 = temp;
}

void sort_array(int array_numbers[], int current_used_numbers)
{
    int minimum=array_numbers[0];
    int index_of_min=0;

    for (int index = 1; index < current_used_numbers; index++)
    {
        if (array_numbers[index] < minimum)
        {
            minimum = array_numbers[index];
            swap_values(array_numbers[index], array_numbers[index_of_min]);
            index_of_min = index;
        }
    }
}


Last edited on
closed account (oy0Gy60M)
Can you show us the contents of the file "test.txt"?
The test.txt is a simple format.
# <---Amount of current numbers.
# <--- Subscript of array(active number)
# # # # <-----(numbers)

So a basic formatting would be

4 <--- Amount of numbers max 10
1 <---- active subscript within the array
12 13 -15 0 <--- and numbers
Last edited on
closed account (oy0Gy60M)
while(num1 != 0 && num2 != 59 || num1 != 55)

It should be :
while(num1 != 0 && num2 != 59 && num1 != 55)
Oh alright. Thank you very much for the assistance. Could you possibly explain why || doesn't work then? It makes sense on the way i wrote it, but apparently not.
closed account (oy0Gy60M)
Logical AND (&&) operator :
https://msdn.microsoft.com/en-us/library/c6s3h5a7.aspx

Logical OR (||) operator :
https://msdn.microsoft.com/en-us/library/f355wky8.aspx
Last edited on
Oh. Sigh I feel like a complete idiot now. Thank you very much for the help. I really appreciate it. Hopefully i won't have many more of these "brain lag" while i write this code out.
You may also want to read this order of precedence for all the operators

http://web.ics.purdue.edu/~cs240/misc/operators.html
Alright i will thank you both for the assistance.
Topic archived. No new replies allowed.