Help with my switch/case

I'm using a switch/case structure to operate a menu. The menu works as intended, with 1 exception. It ignores the first entry. You can input any integerr the first time and the loop simply returns to the beginning, after which it operates as intended.

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
 do  //this loop is set up to have the menu open after each function is completed
        {
            cout << endl;
            cout << "Please enter a choice. " << endl;
            cin >> userChoice;
            if (userChoice == 0)
            {
                outputChoice = "Enter Contacts.";
            }
            if (userChoice == 1)
            {
                outputChoice = "Sort Contacts in Ascending Order by Number.";
            }
            if (userChoice == 2)
            {
                outputChoice = "Sort Contacts in Descending Order by Number.";
            }
            if (userChoice == 3)
            {
                outputChoice = "Print All Contact Information. ";
            }
            if (userChoice == 4)
            {
                outputChoice = "Search for a Contact by Name. ";
            }
            cout << "You chose option: " << userChoice << ", " << outputChoice << endl;
            cout << endl;

            switch(userChoice)  //set up menu with switch case using characters entered by user
            {
                case 0:
                    displayBreak();
                    cout << endl;
                    input(i, contactName, phoneNumber);  //asks for names and numbers to input
                break;

                case 1:
                    displayBreak();
                    cout << endl;
                    sortA(i, j, phoneNumber, index, contactName);  //sorts arrays in ascending order by number
                break;

                case 2:
                    displayBreak();
                    cout << endl;
                    sortD(i, j, phoneNumber, index, contactName);  //sorts arrays in descending order by number
                break;

                case 3:
                    displayBreak();
                    cout << endl;
                    printAll(contactName, phoneNumber, index, i);  //prints arrays in their current state
                break;

                case 4:
                    displayBreak();
                    cout << endl;
                    search(i, searchName, contactName,phoneNumber);  //asks for name to search contactNames for
                break;  
                
                default:
                    displayBreak();
                    cout << endl;
                    cout << "Invalid Input, the program will now exit. " << endl;
                    cout << endl;
                    cin.get();
                    displayBreak();
                    cout << endl;
                    break;
              }//end switch
        }
        while(userChoice < 5);
        {
                    displayBreak();
                    displayBreak();
                    cout << "################  Goodbye  ################" << endl;
                    displayBreak();
                    displayBreak();
                    exit;
        }   //end do/while 
if statement, it is needless.
If you mean the if statements setting the userChoice variable, I know I don't need them, they're there to make the output prettier... ie. "You chose option 0, Input Contacts" rather than "You chose option 0". They don't affect the behavior of the loop, as it has operated the same with and without them.
By the way, the end of your do...while occurs on line 72. The do...while runs once no matter what, not caring about the condition, and only on the second run-through does it check if userChoice<5. So lines 73-80 are needlessly inside curly brackets (you can remove these brackets and left-indent to the same column as the while). These statements will execute no matter what, after the loop.

That being said, the logic still looks okay. userChoice is some integer declared outside the loop; its initial value shouldn't matter. Go through with debugger and try to clarify what you mean by "It ignores the first entry." I understand this as a question about the do...while loop, which has nothing to do w/ the switch statement. Since this example isn't complete (won't compile), only you can debug this. Other people can only guess as to the problem.
Last edited on
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* 
 * File:   WClapham_Final-Program_5Feb17
 * Author: Lloyd Clapham
 *
 * Created on February 5, 2018, 12:35- PM
 */

/*
 Contact Manager Program
 Presents user with Menu consisting of the following options
 Input Contacts (10)
 Sort Contacts in ascending numerical order
 Sort Contacts in descending numerical order
 Print all contacts in their current order
 Search for a contact by name
 quit*/

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

//function prototypes
//displayBreak function
void displayBreak();
//display menu function
void displayMenu();
//input function
void input(int i, string contactName[10], int phoneNumber[10]);
//printAll function
void printAll(string contactName[10], int phoneNumber[10], int index[10], int i);
//Search function
void search(int i, string searchName, string contactName[10], int phoneNumber[10]);
//sortA function
void sortA(int i, int j, int phoneNumber[10], int index[10], string contactName[10]);
//sortD function
void sortD(int i, int j, int phoneNumber[10], int index[10], string contactName[10]);

//main function
int main(int argc, char** argv) {

    //declare variables
    int phoneNumber[10];
    string contactName[10];
    int index[10];
    int i;
    int j;
    string searchName;
    int userChoice;
    string outputChoice;
    //Display the main menu prior to asking user for choice
    displayMenu();
    //menu choice
        //get menu choice from user
        cout << "Please enter a choice " << endl;
        cin >> userChoice;
        cout << "You chose option: " << userChoice << endl;
        cout << endl;
    //Descision structure to call appropriate function from main menu
        do  //this loop is set up to have the menu open after each function is completed, thanks Mr. Abdullah
        {
            cout << endl;
            cout << "Please enter a choice. " << endl;
            cin >> userChoice;
            if (userChoice == 0)
            {
                outputChoice = "Enter Contacts.";
            }
            if (userChoice == 1)
            {
                outputChoice = "Sort Contacts in Ascending Order by Number.";
            }
            if (userChoice == 2)
            {
                outputChoice = "Sort Contacts in Descending Order by Number.";
            }
            if (userChoice == 3)
            {
                outputChoice = "Print All Contact Information. ";
            }
            if (userChoice == 4)
            {
                outputChoice = "Search for a Contact by Name. ";
            }
            cout << "You chose option: " << userChoice << ", " << outputChoice << endl;
            cout << endl;
            switch(userChoice)  //set up menu with switch case using menu options entered by user
            {
                case 0:
                    displayBreak();
                    cout << endl;
                    input(i, contactName, phoneNumber);  //asks for names and numbers to input
                break;

                case 1:
                    displayBreak();
                    cout << endl;
                    sortA(i, j, phoneNumber, index, contactName);  //sorts arrays in ascending order by number
                break;

                case 2:
                    displayBreak();
                    cout << endl;
                    sortD(i, j, phoneNumber, index, contactName);  //sorts arrays in descending order by number
                break;

                case 3:
                    displayBreak();
                    cout << endl;
                    printAll(contactName, phoneNumber, index, i);  //prints arrays in their current state
                break;

                case 4:
                    displayBreak();
                    cout << endl;
                    search(i, searchName, contactName,phoneNumber);  //asks for name to search contactNames for and returns with contact name and number if it exists
                break;  
                        
                default: //default behavior for the loop when none of the cases are satisfied
                    displayBreak();
                    cout << endl;
                    cout << "Invalid Input, the program will now exit. " << endl;
                    cout << endl;
                    cin.get();
                    displayBreak();
                    cout << endl;
                    break;
              }//end switch
        }
        while(userChoice < 5); //completes the do/while loop, perform the loop while userChoice variable is 0-4, if it's 5, exit
        {
                    displayBreak();
                    displayBreak();
                    cout << "################  Goodbye  ################" << endl;
                    displayBreak();
                    displayBreak();
                    exit;
        }   //end do/while
    return 0;
}//end of main function

//User Defined Functions
//displayBreak function
void displayBreak()
{
    cout << "###########################################" << endl;
} //end of displayBreak function
//display menu function
void displayMenu()
{
    cout << endl;
    displayBreak();
    cout << "Welcome to your Contacts Manager. " <<endl;
    cout << "Please enter a selection. " << endl;
    cout << endl;
    cout << "To enter contacts, ENTER:  0 " << endl;
    cout << "To sort numbers in ascending order, ENTER:  1"<< endl;
    cout << "To sort numbers in descending order, ENTER:  2 " << endl;
    cout << "To display all contact information, ENTER:  3 " << endl;
    cout << "To search for a person's number, ENTER:  4 " << endl;
    cout << "To quit the program, ENTER:  5 " << endl; 
    displayBreak();
    cout << endl;
} //end of displayMenu function
//printAll function
void printAll(string contactName[10], int phoneNumber[10], int index[10], int i)
{
    for (i=0; i<=9; i++)
    {
        cout << contactName[index[i]] << "'s number is: " << phoneNumber[index[i]] <<endl;
    }
    cout << endl; 
}//end of printAll function
//searchForName function
void search(int i, string searchName, string contactName[10], int phoneNumber[10])
{
    cout << "Enter a name to search for: ";
    cin >> searchName;
    cout <<endl;
    for (int i = 0; i < 10; i++)
        if (contactName[i] == searchName)
        {
            cout << "User " << contactName[i] << "'s number is: " << phoneNumber[i] <<endl;
        }
        cout << "User " << searchName << " cannot be found.";
        cout <<endl;
}//end of searchForName function
//sortA function
void sortA(int i, int j, int phoneNumber[10], int index[10], string contactName[10])
{
//initialization of index for sort
    for (i=0; i<=9; i++)
    {
        index[i]=i;
    }
    //perform sort
    for (i=0; i<=8; i++)
    {
        for(j=i+1; j<=9; j++)
        {
            int temp;
            
            if(phoneNumber[index[i]] > phoneNumber[index[j]])
            {
                temp = index[i];
                index[i] = index[j];
                index[j] = temp;
            }
        }
    }
}//end of sortA function
//sortD function
void sortD(int i, int j, int phoneNumber[10], int index[10], string contactName[10])
{
//initialization of index for sort
    for (i=0; i<=9; i++)
    {
        index[i]=i;
    }
    //perform sort
    for (i=0; i<=8; i++)
    {
        for(j=i+1; j<=9; j++)
        {
            int temp;
            
            if(phoneNumber[index[i]] < phoneNumber[index[j]])
            {
                temp = index[i];
                index[i] = index[j];
                index[j] = temp;
            }
        }
    }
}//end of sortD function
//input function
void input(int i, string contactName[10], int phoneNumber[10])
{
    for (i=0; i<=9; i++)
    {
        cout << "Please enter a contact's name: ";
        cin >> contactName[i];
        cout << "Please enter " << contactName[i] << "'s Number: ";
        cin >> phoneNumber[i];
        cout << endl;
    }
}//end input function 
debugger doesn't spit out an error...:
"###########################################
Welcome to your Contacts Manager.
Please enter a selection.

To enter contacts, ENTER: 0
To sort numbers in ascending order, ENTER: 1
To sort numbers in descending order, ENTER: 2
To display all contact information, ENTER: 3
To search for a person's number, ENTER: 4
To quit the program, ENTER: 5
###########################################

Please enter a choice
0
You chose option: 0


Please enter a choice.
0
You chose option: 0, Enter Contacts.

###########################################
Thanks very much icy... your suggestion made me think about it slightly different. I was asking the user for a choice twice. The loop is fine, it's the menu option before the loop that was screwing me up. I don't need that part.
1
2
3
4
5
6
//menu choice
        //get menu choice from user
        cout << "Please enter a choice " << endl;
        cin >> userChoice;
        cout << "You chose option: " << userChoice << endl;
        cout << endl;
Topic archived. No new replies allowed.