Help Me Please!!!!

I am in desperate need of help. I have been struggling how to use functions to pass information into arrays and my menu switch statement is throwing errors. I tried an if else statement, but that doesn't seem to work. My assignment is due tonight at midnight. any help/suggestions will be greatly appreciated!!!!

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
  *
 * 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: katherine weisbrod
 *
 * Created on June 6, 2018, 9:58 PM
 */

#include <cstdlib>
#include <iostream>
#include <istream>
#include <string>
#include <iomanip>

using namespace std;

void displayMenu();
void printContact(string[], string[], long int, int);
void sortAscending(string[], string[], long int, int);
void sortDescending(string[], string[], long int, int);
void searchContact(string[], string[], long int, int);
void getFirstName (string[], int);
void getLastName (string[], int);
void getPhoneNumber (long int[], int);


   






/*
 * 
 */
int main(int argc, char** argv) 
{//Declare variables
    int menuChoice = 0;

    string firstName[3];
    
    firstName[0] = "Maria";
    firstName[1] = "Thomas";
    firstName[2] = "Harold";
    
    string lastName[3];
    
    lastName[0] = "Neptune";
    lastName[1] = "Quinn";
    lastName[2] = "Apple";
    
    long int number[3];
    
    number[0] = 4352869;
    number[1] = 7658974;
    number[2] = 9568231;
    
    int size = -1;
    
    printContact(firstName, lastName, number, 3);
    
    //get menu choice
	displayMenu();
	cout << "Enter 1, 2, 3, 4, 5, or 6: ";
	cin >> menuChoice;
        
        while (menuChoice > 0 && menuChoice < 6)
	{
		//assign menu functions
	    switch (menuChoice)
      case 1: 
            {
                getFirstName(firstName, 3);
                getLastName(lastName, 3);
                getPhoneNumber(number, 3);
                break;
            }
      case 2:
            {
          sortAscending(firstName, lastName, number, 3);
          break;
            }
      case 3:
      {
       sortDescending(firstName, lastName, number, 3);
       break;
      }
      case 4:
      {
          printContact(firstName, lastName, number, 3);
          break;
      }
      case 5:
      {
          searchContact(firstName, lastName, number, 3);
          break;
      }
      case 6:
      {
          break;
      }
      default:
      {
          cout << "Enter a valid Menu Option!!" << endl;
          break;
      }
           
            

		
		//get menu choice
		displayMenu();
		cout << "Enter 1, 2, 3, 4, 5, or 6: ";
		cin >> menuChoice;
	}	//end while

    
	//system("pause");
	return 0;
	//end of main function
}
//*****function definitions*****
void displayMenu()
{
	cout << "1 Input data" << endl;
	cout << "2 Sort data Ascending" << endl;
	cout << "3 Sort data Descending" << endl;
        cout << "4 Print all data" << endl;
        cout << "5 Search for an individual in the data" << endl;
	cout << "6 End program" << endl;
}	//end of displayMenu function

void getFirstName (string arrayFirstName[], int size, int index)
{
    for (index=0; index< size; index++)
    {
cout << "Please enter the first name and press enter: ";
cin >> arrayFirstName[index];
cout << endl;
    }
}

void getLastName (string arrayLastName[], int size, int index)
{
for (index=0; index< size; index++)
    {
cout << "Please enter the last name and press enter: ";
cin >> arrayLastName[index];
cout << endl;
    }
}

void getPhoneNumber (long int arrayNumber[], int size, int index)
{
for (index=0; index< size; index++)
    {
cout << "Please enter the phone number without hyphens and press enter: ";
cin >> arrayNumber[index];
cout << endl;
    }
}


void printContact(string arrayFirstName[], string arrayLastName[], long int arrayNumber[],int size)
{
   
    for (index  = 0; index < size; index++)
    {
        cout << arrayFirstName[index] << endl;
        cout << arrayLastName[index] << endl;
        cout << arrayNumber[index] << endl;
    }
}

void sortAscending(string arrayFirstName[], string arrayLastName[], long int arrayNumber[], int size)
 {
                       
                        
                        
                  for (int i=0; i < size -1; i++ )
                   {
                        for (int j=0; j<size-i-1;j++)
                        {
                             if (arrayFirstName[j]>arrayFirstName[j+1])
                            {
                               
                                arrayFirstName[j]=arrayFirstName[j+1];
                                arrayLastName [j]=arrayLastName[j+1];
                                arrayNumber[j] = arrayNumber[j+1];                                
                            } //End If statement
                        } //End nested for loop
                    } //End for loop
 }

void sortDescending(string arrayFirstName[], string arrayLastName[], long int arrayNumber[], int size)
 {
                       
                        
                        
                  for (int i=0; i < size -1; i++ )
                   {
                        for (int j=0; j<size-i-1;j++)
                        {
                             if (arrayFirstName[j]<arrayFirstName[j+1])
                            {
                               
                                arrayFirstName[j]=arrayFirstName[j+1];
                                arrayLastName [j]=arrayLastName[j+1];
                                arrayNumber[j] = arrayNumber[j+1];                                
                            } //End If statement
                        } //End nested for loop
                    } //End for loop
 }

void searchContact(string arrayFirstName[], string arrayLastName[], long int arrayNumber[], int size)
{
    bool found = false;
    string userInput;
    index = 0;
    cout << "Enter Name to Search" << endl;
    cin >> userInput;
    
    
    
        if (arrayFirstName[index] ==userInput || arrayLastName[index] == userInput)
        {
             cout << arrayFirstName[index] << endl;
             cout << arrayLastName[index] << endl;
             cout << arrayNumber[index] << endl;
             cout << endl;
             found = true;
            
        } //End if statement
    
    
    if (found==false) //If searched target not found
    {
        cout << "Contact not found." << endl;
    }
}
this code wont compile, which maybe you know but didn't say..

printContact(firstName, lastName, number, 3);

number is an array, but your function takes a straight int value. Either the function needs to change to take an array here, or number is not the correct parameter that you wanted here (maybe number[something?]).

your switch statement is not formatted correctly.
try
switch (thing)
{
case 1:
..

} //the switch itself needs {} pair around ALL the cases. Cases don't require {} but it does not hurt and looks nice to have them on larger blocks.


for (int index = 0; index < size; index++) //fix this, index is undeclared in a couple of places due to this.


Compiling busted code … protip: locate and fix the first issue, then recompile and repeat until it compiles. If you don't understand an error message, show it here along with the current code that is not working.


you certainly can use if/else if you do it right. I prefer this to switch usually because switch only works on integer types (no classes, no doubles, its annoying) and they have no real advantage unless you are making use of no-break (fall through) logic.

just looks like
if(thing == 1)
{
do case 1 stuff here
}
else
if(thing == 2)
{
do case 2 stuff
}


else
{
default
}


Last edited on
Thank you so much!

I addressed the syntax errors you mentioned. Wow, that was so silly of me!!
But now I don't know why it wont compile. any suggestions? Is it my search contacts function because I took out the array int size for a argument? I took it out because it wasn't being used in the search algorithm.

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
/*
 * 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: katherine weisbrod
 *
 * Created on June 6, 2018, 9:58 PM
 */

#include <cstdlib>
#include <iostream>
#include <istream>
#include <string>
#include <iomanip>

using namespace std;

void displayMenu();
void printContact(string[], string[], long int[], int);
void sortAscending(string[], string[], long int[], int);
void sortDescending(string[], string[], long int[], int);
void searchContact(string[], string[], long int[]);
void getFirstName (string[], int);
void getLastName (string[], int);
void getPhoneNumber (long int[], int);


   






/*
 * 
 */
int main(int argc, char** argv) 
{//Declare variables
    int menuChoice = 0;

    string firstName[3];
    
    firstName[0] = "Maria";
    firstName[1] = "Thomas";
    firstName[2] = "Harold";
    
    string lastName[3];
    
    lastName[0] = "Neptune";
    lastName[1] = "Quinn";
    lastName[2] = "Apple";
    
    long int number[3];
    
    number[0] = 4352869;
    number[1] = 7658974;
    number[2] = 9568231;
    
    int size = -1;
    
    printContact(firstName, lastName, number, 3);
    
    //get menu choice
	displayMenu();
	cout << "Enter 1, 2, 3, 4, 5, or 6: ";
	cin >> menuChoice;
        
        while (menuChoice > 0 && menuChoice < 6)
	{
		//assign menu functions
	    switch (menuChoice)
            {
      case 1: 
            {
                getFirstName(firstName, 3);
                getLastName(lastName, 3);
                getPhoneNumber(number, 3);
                break;
            }
      case 2:
            {
          sortAscending(firstName, lastName, number, 3);
          break;
            }
      case 3:
      {
       sortDescending(firstName, lastName, number, 3);
       break;
      }
      case 4:
      {
          printContact(firstName, lastName, number, 3);
          break;
      }
      case 5:
      {
          searchContact(firstName, lastName, number);
          break;
      }
      case 0:
      {
          break;
      }
      default:
      {
          cout << "Enter a valid Menu Option!!" << endl;
          break;
      }
           
            }

		
		//get menu choice
		displayMenu();
		cout << "Enter 1, 2, 3, 4, 5, or 6: ";
		cin >> menuChoice;
	}	//end while

    
	//system("pause");
	return 0;
	//end of main function
}
//*****function definitions*****
void displayMenu()
{
	cout << "1 Input data" << endl;
	cout << "2 Sort data Ascending" << endl;
	cout << "3 Sort data Descending" << endl;
        cout << "4 Print all data" << endl;
        cout << "5 Search for an individual in the data" << endl;
	cout << "6 End program" << endl;
}	//end of displayMenu function

void getFirstName (string arrayFirstName[], int size)
{
    for (int index=0; index< size; index++)
    {
cout << "Please enter the first name and press enter: ";
cin >> arrayFirstName[index];
cout << endl;
    }
}

void getLastName (string arrayLastName[], int size)
{
for (int index=0; index< size; index++)
    {
cout << "Please enter the last name and press enter: ";
cin >> arrayLastName[index];
cout << endl;
    }
}

void getPhoneNumber (long int arrayNumber[], int size)
{
for (int index=0; index< size; index++)
    {
cout << "Please enter the phone number without hyphens and press enter: ";
cin >> arrayNumber[index];
cout << endl;
    }
}


void printContact(string arrayFirstName[], string arrayLastName[], long int arrayNumber[],int size)
{
   
    for (int index  = 0; index < size; index++)
    {
        cout << arrayFirstName[index] << endl;
        cout << arrayLastName[index] << endl;
        cout << arrayNumber[index] << endl;
    }
}

void sortAscending(string arrayFirstName[], string arrayLastName[], long int arrayNumber[], int size)
 {
                       
                        
                        
                  for (int i=0; i < size -1; i++ )
                   {
                        for (int j=0; j<size-i-1;j++)
                        {
                             if (arrayFirstName[j]>arrayFirstName[j+1])
                            {
                               
                                arrayFirstName[j]=arrayFirstName[j+1];
                                arrayLastName [j]=arrayLastName[j+1];
                                arrayNumber[j] = arrayNumber[j+1];                                
                            } //End If statement
                        } //End nested for loop
                    } //End for loop
 }

void sortDescending(string arrayFirstName[], string arrayLastName[], long int arrayNumber[], int size)
 {
                       
                        
                        
                  for (int i=0; i < size -1; i++ )
                   {
                        for (int j=0; j<size-i-1;j++)
                        {
                             if (arrayFirstName[j]<arrayFirstName[j+1])
                            {
                               
                                arrayFirstName[j]=arrayFirstName[j+1];
                                arrayLastName [j]=arrayLastName[j+1];
                                arrayNumber[j] = arrayNumber[j+1];                                
                            } //End If statement
                        } //End nested for loop
                    } //End for loop
 }

void searchContact(string arrayFirstName[], string arrayLastName[], long int arrayNumber[])
{
    bool found = false;
    string userInput;
    int index = 0;
    cout << "Enter Name to Search" << endl;
    cin >> userInput;
    
    
    
        if (arrayFirstName[index] ==userInput || arrayLastName[index] == userInput)
        {
             cout << arrayFirstName[index] << endl;
             cout << arrayLastName[index] << endl;
             cout << arrayNumber[index] << endl;
             cout << endl;
             found = true;
            
        } //End if statement
    
    
    if (found==false) //If searched target not found
    {
        cout << "Contact not found." << endl;
    }
}
Last edited on
what you posted compiles and runs here in the forum compiler tool. So it should be working now, as far as compilation goes.

some compilers need you to explicitly rebuild everything or they may keep a previous version of some (intermediate/compiled) files and get all confused.
Last edited on
Thank you so much! You were right, my ide got all confused. I know its no the greatest, but ok for a noob :)

I made the following adjustments so the sorting features worked better

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
/*
 * 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: katherine weisbrod
 *
 * Created on June 6, 2018, 9:58 PM
 */

#include <cstdlib>
#include <iostream>
#include <istream>
#include <string>
#include <iomanip>

using namespace std;

void displayMenu();
void printContact(string[], string[], long int[], int);
void sortAscending(string[], string[], long int[], int);
void sortDescending(string[], string[], long int[], int);
void searchContact(string[], string[], long int[]);
void getContactInfo (string arrayFirstName[], string arrayLastName[], long int arrayNumber[],int size);
/*
 * 
 */
int main(int argc, char** argv) 
{//Declare variables
    int menuChoice = 0;

    string firstName[3];
    
    firstName[0] = "Maria";
    firstName[1] = "Thomas";
    firstName[2] = "Harold";
    
    string lastName[3];
    
    lastName[0] = "Neptune";
    lastName[1] = "Quinn";
    lastName[2] = "Apple";
    
    long int number[3];
    
    number[0] = 4352869;
    number[1] = 7658974;
    number[2] = 9568231;
    
    
    //get menu choice
	displayMenu();
	cout << "Enter 1, 2, 3, 4, 5, or 6: ";
	cin >> menuChoice;
        
        while (menuChoice > 0 && menuChoice < 6)
	{
		//assign menu functions
	    switch (menuChoice)
            {
      case 1: 
            {
                getContactInfo(firstName, lastName, number, 3);
                break;
            }
      case 2:
            {
          sortAscending(firstName, lastName, number, 3);
          printContact(firstName, lastName, number, 3);
          break;
            }
      case 3:
      {
       sortDescending(firstName, lastName, number, 3);
       printContact(firstName, lastName, number, 3);
       break;
      }
      case 4:
      {
          printContact(firstName, lastName, number, 3);
          break;
      }
      case 5:
      {
          searchContact(firstName, lastName, number);
          break;
      }
      case 0:
      {
          break;
      }
      default:
      {
          cout << "Enter a valid Menu Option!!" << endl;
          break;
      }
           
            }

		
		//get menu choice
		displayMenu();
		cout << "Enter 1, 2, 3, 4, 5, or 6: ";
		cin >> menuChoice;
	}	//end while

    
	//system("pause");
	return 0;
	//end of main function
}
//*****function definitions*****
void displayMenu()
{
	cout << "1 Input data" << endl;
	cout << "2 Sort data Ascending" << endl;
	cout << "3 Sort data Descending" << endl;
        cout << "4 Print all data" << endl;
        cout << "5 Search for an individual in the data" << endl;
	cout << "6 End program" << endl;
}	//end of displayMenu function

void getContactInfo (string arrayFirstName[], string arrayLastName[], long int arrayNumber[],int size)
{
    for (int index=0; index< size; index++)
    {
cout << "Please enter the first name and press enter: ";
cin >> arrayFirstName[index];
cout << "Please enter the last name and press enter: ";
cin >> arrayLastName[index];
cout << "Please enter the phone number without hyphens and press enter: ";
cin >> arrayNumber[index];
cout << endl;
    }
}

void printContact(string arrayFirstName[], string arrayLastName[], long int arrayNumber[],int size)
{
   
    for (int index  = 0; index < size; index++)
    {
        cout << arrayFirstName[index] << endl;
        cout << arrayLastName[index] << endl;
        cout << arrayNumber[index] << endl;
    }
}

void sortAscending(string arrayFirstName[], string arrayLastName[], long int arrayNumber[], int size)
 {
                       
                        
                        
                  for (int i=0; i < size -1; i++ )
                   {
                        for (int j=0; j<size-i-1;j++)
                        {
                             if ( arrayNumber[j]> arrayNumber[j+1])
                            {
                               
                                arrayFirstName[j]=arrayFirstName[j+1];
                                arrayLastName [j]=arrayLastName[j+1];
                                arrayNumber[j] = arrayNumber[j+1];                                
                            } //End If statement
                        } //End nested for loop
                    } //End for loop
 }

void sortDescending(string arrayFirstName[], string arrayLastName[], long int arrayNumber[], int size)
 {
                       
                        
                        
                  for (int i=0; i < size -1; i++ )
                   {
                        for (int j=0; j<size-i-1;j++)
                        {
                             if ( arrayNumber[j]< arrayNumber[j+1])
                            {
                               
                                arrayFirstName[j]=arrayFirstName[j+1];
                                arrayLastName [j]=arrayLastName[j+1];
                                arrayNumber[j] = arrayNumber[j+1];                                
                            } //End If statement
                        } //End nested for loop
                    } //End for loop
 }

void searchContact(string arrayFirstName[], string arrayLastName[], long int arrayNumber[])
{
    bool found = false;
    string userInput;
    int index = 0;
    cout << "Enter Name to Search" << endl;
    cin >> userInput;
    
    
    
        if (arrayFirstName[index] ==userInput || arrayLastName[index] == userInput)
        {
             cout << arrayFirstName[index] << endl;
             cout << arrayLastName[index] << endl;
             cout << arrayNumber[index] << endl;
             cout << endl;
             found = true;
            
        } //End if statement
    
    
    if (found==false) //If searched target not found
    {
        cout << "Contact not found." << endl;
    }
}
Topic archived. No new replies allowed.