Trouble spotting an infinite loop

I cannot seem to find what is causing my program to fall into an infinite loop. When the user enters "2" and goes into the lookupTaxes function, they then pick an address. After that, the program "should" return to the displayMenu function and start from the top. Repeating this, so long as the user does not enter "5". But, every time I enter the street address in the lookupTaxes function, the program falls into an infinite loop. I'm not following the logic here. 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
  #include <iostream>


using namespace std;

//Function Prototypes----------------
void displayMenu();
void lookupTaxes();


//Global Arrays----------------------
const int VAL = 7;
string address[VAL] = {"151 Acorn", "161 Acorn", "200 Main", "500 Arcade", "161 Acorn", "120 Xenia", "200 Acorn" };
int amountDUE[VAL] = {500, 1500, 15000, 25000, 6000, 1000, 20000};


int main() {
    
    displayMenu();
    
    return 0;
}

void displayMenu()
{
    int selection;
    
    do
    {
    cout << "\t County Auditors Property Tax Search and Lookup" << endl;
    cout << "Enter 1 to display the data" << endl;
    cout << "Enter 2 to lookup taxes" << endl;
    cout << "Enter 3 to sort taxed from ascending order" << endl;
    cout << "Enter 4 to show the property with the largest tax due" << endl;
    cout << "Enter 5 to exit\n\n";
    cout << "Please enter your selection: ";
    
    cin >> selection;
        
    if (selection == 1)
        displayMenu();
    if (selection == 2)
        lookupTaxes();
    
    } while (selection != 5);
    
}

void lookupTaxes()
{
    string selection;
    
    cout << "Enter the address of the property: ";
    cin >> selection;
    
    if (selection == "151 Acorn" )
    {
        cout << amountDUE[0];
    }
    else if (selection == "161 Acorn")
    {
        cout << amountDUE[1] << " &" << amountDUE[4];
    }
    else if (selection == "200 Main")
    {
        cout << amountDUE[2];
    }
    else if (selection == "500 Arcade")
    {
        cout << amountDUE[3];
    }
    else if (selection == "120 Xenia")
    {
        cout << amountDUE[5];
    }
    else cout << amountDUE[6];
    
    
}
Hi,

Think about the condition you have for your do loop.

Use the search function at the top of this page to look for bool controlled while loop which has a switch inside.

Hope all is well :+)
Last edited on
@TheIdeasMan - I tried to follow the example you asked me to look at but I'm still running into issues. Now, when prompted to enter the selection from the menu, it keeps prompting without entering the switch....I cannot figure out what's wrong with the logic.

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
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

//Function Prototypes----------------
void displayMenu();
void lookupTaxes();


//Global Arrays----------------------
string address[] = {"151 Acorn", "161 Acorn", "200 Main", "500 Arcade", "161 Acorn", "120 Xenia", "200 Acorn" };
int amountDUE[] = {500, 1500, 15000, 25000, 6000, 1000, 20000};


int main() {
    
    int selection;
    bool quit = false;
    
    displayMenu();
    
    while(!quit)
    {
        cout << "Please enter your selection: \n";
        cin >> selection;
        
        switch (selection)
        {
            case '1': displayMenu();
                break;
                
            case '2': lookupTaxes();
                break;
                
            case '5': quit = true;
                return 0;
  
            default: cout << "Not valid input." << endl;


        }
    }
    
    return 0;
}

void displayMenu()
{
    cout << "\t County Auditors Property Tax Search and Lookup\n"
         << "Enter 1 to display the data\n"
         << "Enter 2 to lookup taxes\n"
         << "Enter 3 to sort taxed from ascending order\n"
         << "Enter 4 to show the property with the largest tax due\n"
         << "Enter 5 to exit\n\n";
}

void lookupTaxes()
{
    string selection;
    
    cout << "Enter the address of the property: " << endl;;
    getline(cin, selection);
    
    if (selection == "151 Acorn" )
    {
        cout << amountDUE[0] << endl;
    }
    else if (selection == "161 Acorn")
    {
        cout << amountDUE[1] << " & " << amountDUE[4] << endl;
    }
    else if (selection == "200 Main")
    {
        cout << amountDUE[2] << endl;
    }
    else if (selection == "500 Arcade")
    {
        cout << amountDUE[3] << endl;
    }
    else if (selection == "120 Xenia")
    {
        cout << amountDUE[5] << endl;
    }
    else if (selection == "200 Acorn")
    {
        cout << amountDUE[6] << endl;
    }
                    

    
}
Hi,

You have declared selection as an int , but in the cases they are quoted which means they are a char - which will have a different value.

Also, would it make sense to call displayMenu() at the beginning of the while loop, and not have it as a menu option?

Some other things:

Don't have line 5, Google as to why that is so. Instead put std:: before each std thing. It might seem a pain, but you will notice that all the expert people on this site always do it - such as JLBorges as just one example. It will save you from some hard to see bugs in the future.

Try not to have global variables of any sort. Pass them as parameters to any function which needs them. Again, this will save problems in the future.

With your addresses, having the user type them in is error prone, if they type in 120 xenia say, then the code will fail. Much better to print them all out with an associated number, and have the user type that in. When you learn how to use a struct, you can group all the like things together- address, tax etc.

Hope all is well :+)
Topic archived. No new replies allowed.