How do I stop a endless loop?

My program will load the main menu screen, but as soon as I input a menu selection it get stuck in an endless loop. Any help would be great!

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
 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   main.cpp
 * Author: Josh
 *
 * Created on February 5, 2018, 5:20 PM
 */

#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>
#include <string>
using namespace std;

// Function Predeclarations

void menu();
void inputInfo(int i, string fname[3], string lname[3], string phoneNumber[3]);
void sort(string fname[3], string lname[3], string phoneNumber[3], string ascending);
void search(int i, string searchUser, string fname[3], string phoneNumber[3]);
void print(int i, string lname[3], string fname[3], string phoneNumber[3]);


int main()


{
   string fname[3]; 
   string lname[3];
   string phoneNumber[3];
   string ascending;
   int reply;
   string searchUser;
   int i;
   
do//this loop is set up to have the menu open after each function is completed
    {
        menu();
        
        cin >> reply;
        
        switch(reply)//set up menu with switch case using characters entered by user
        {
            case 'a':inputInfo(i, fname, lname, phoneNumber);
            break;
            
            case 'b':ascending = "y";sort(fname, lname, phoneNumber, ascending);
            break;
            
            case 'c':ascending = "n";sort(fname, lname, phoneNumber, ascending);//sorts arrays in descending order
            break;
            
            case 'd':print(i, fname, lname, phoneNumber);//prints arrays in their current state
            break;
            
            case 'e':search(i, searchUser, fname, phoneNumber);
            break;
        }
}
    while (reply != 'f');
    cout << "Goodbye" << endl;

    return 0;
}
// Function Implementations 
    
void menu()//prompts user to make a selection
{
    cout << "Welcome to your Contact Manager. Please select an option" << endl;
    cout << "a. Input Data" << endl;
    cout << "b. Sort Data Ascending" << endl;
    cout << "c. Sort Data Descending" << endl;
    cout << "d. Print All Data" << endl;
    cout << "e. Search for an individual in the data" << endl;
    cout << "f. End Program" << endl;
}

void inputInfo(int i, string fname[3], string lname[3], string phoneNumber[3])
{
    {
        for (i = 0; i < 2; i++)
    
    
     cout << "\nEnter Person's Name. Then press enter: ";
     cin >> fname[i];      // store the input directly in the array
     cout << "/nEnter Person's Last Name. Then press enter: ";
     cin >> lname[i];        
     cout << "Enter Phone Number. Then press enter: ";            // instead of using a dummy
     cin >> phoneNumber[i];
    }  
}

void sort(string fname[3], string lname[3], string phoneNumber[3], string ascending)
{
    string ptemp = "";
    string fntemp = "";
    string lntemp = "";
    int pass;
    int index;
    
    if(ascending == "y")
    {
        for (pass = 0; pass < 3; pass++)//create outer loop to count the number of passes
        {
            for (index = 0; index < 3 - pass; index++)//inner loop to iterate 10 times to account for all values
            {
                if (phoneNumber[index + 1] < phoneNumber[index])//if any value is moved the other two will also move inside their arrays
                {
                    ptemp = phoneNumber[index];
                    phoneNumber[index] = phoneNumber[index + 1];
                    phoneNumber[index + 1] = ptemp;
                    lntemp = lname[index];
                    lname[index] = lname[index + 1];
                    lname[index + 1] = lntemp;
                    fntemp = fname[index];
                    fname[index] = fname[index + 1];
                    fname[index + 1] = fntemp;
                }
            }
        }
        int index = 0;//prints out list after sorting
        cout << left << setw(12) << "Phone Number" << setw(5) << " " << setw(20) << "Last Name" << right << setw(20) << "First Name" << endl;  
        for(index = 0; index < 3; index++)   
        {
            cout << left << setw(12) << phoneNumber[index] << setw(5) << " " << setw(20) << fname[index] << right << setw(20) << lname[index] << endl;
        }
    }
  // Write the code descending order below: 
    }

void search(int i, string searchUser, string fname[3], string phoneNumber[3])
{
cout << "\nPlease enter the first name of the person you want to search for"
         << " and then press enter:\n";
    cin >> searchUser;
    
    
    for(i = 0; i < 3; i++)
{
if (fname[i] == searchUser)
{
break;
}
}
    
if(i > 9)
{
cout << "\n" << searchUser << " was not found." << endl;
}
else
{
cout << "\n" << searchUser << " was found in the phone book! " << searchUser << "'s number is " 
<< phoneNumber[i] << "." << endl;
}
} 


void print(int i, string lname[3], string fname[3], string phoneNumber[3])
{
    for (i=0; i<=2; i++)
    {
        cout << lname[i] << "'s number is: " << phoneNumber[i] <<endl;
    }
    cout << endl; 
}
How do I stop a endless loop?

You take it out back and break it's knees.


The cause of your problem is that you are asking the user to input a character, but then you try to read an integer. Input fails → all further input fails → loop never ends.

This exposes two problems:
 • mismatch between instructions and expected input
 • invalid input can break your program

Personally, I have come to always recommend getting all input from the user as a string, then testing the string for validity. This makes it impossible for standard input to fail due to bad input, and heartily encourages you to actively select valid inputs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
std::string user_input;
bool done = false;

while (!done)
{
  menu();

  getline( std::cin, user_input );
  if (user_input.size())
    switch (std::tolower( user_input[ 1 ] ))
    {
      case 'a': ...;
      ...

      case 'f': done = true; break;

      default: ;
    }

}
std::cout << "Goodbye\n";

Hope this helps.
Last edited on
Fixed my loop issue. But now my arrays will not work correctly. I took it from an old project where it worked perfectly but now outputs 3 blank lines and I can only populate one of those line. 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
#include <iostream>
#include <string>
#include <cctype>

char menu() // return the selection made by the user
{
    std::cout << "\nWelcome to your Contact Manager. Please select an option\n"
              << "a. Input Data\n"
              << "b. Sort Data Ascending\n"
              << "c. Sort Data Descending\n"
              << "d. Print All Data\n"
              << "e. Search for an individual in the data\n"
              << "f. End Program\n" ;

    char choice ;
    std::cout << "enter your choice: " ;
    std::cin >> choice ;
    choice = std::tolower(choice) ; // if upper case, convert to lower case

    static std::string valid_choices = "abcdef" ;

    // if it is a valid choice, return it
    if( valid_choices.find(choice) != std::string::npos ) return choice ;

    std::cout << "invalid choice. try again\n" ;
    return menu() ; // try again
}

const std::size_t ARRAY_SZ = 3 ;

void input( std::string fname[ARRAY_SZ], std::string lname[ARRAY_SZ], std::string phone[ARRAY_SZ] )
{
    for( std::size_t i = 0 ; i < ARRAY_SZ ; ++i )
    {
        std::cout << "Enter Person's first name: " ;
        std::cin >> fname[i] ;

        std::cout << "Enter Person's last name: " ;
        std::cin >> lname[i] ;

        std::cout << "Enter Person's phone number: " ;
        std::cin >> phone[i] ;
    }
}

void print( const std::string fname[ARRAY_SZ], const std::string lname[ARRAY_SZ], const std::string phone[ARRAY_SZ] )
{
    for( std::size_t i = 0 ; i < ARRAY_SZ ; ++i )
    {
        std::cout << i+1 << ". " << lname[i] << ", " << fname[i] << ": phone " << phone[i] << '\n' ;
    }
}


int main()
{
    std::string fname[ARRAY_SZ] ;
    std::string lname[ARRAY_SZ] ;
    std::string phone[ARRAY_SZ] ;

    // as long as the choice is not 'f' end program
    for( char selection = menu() ; selection != 'f' ; selection = menu() )
    {
         switch(selection)
         {
             // ...
             case 'a' : input( fname, lname, phone ) ; break ;

             case 'd' : print( fname, lname, phone ) ; break ;

             // etc.
         }
    }
}
Topic archived. No new replies allowed.