-
Parallel Array Problem
I'm not sure if I did this properly. The program works fine but I don think that is what the problem is asking for.
Any other ways to accomplish this task?
Thanks in advance!
Here's the problem
Code:
Name and load two parallel arrays for the following table. Write a search routine (not a whole program)
that uses the input variable deptName to extract the access code.
Department Access Code
Accounting 1123X5
Receiving 1154V2
Sales 8823Q1
Management 5412R7
and my solution:
Code:
#include<iostream.h>
int main()
{
char department[4][15] = {"Accounting", "Receiving", "Sales", "Management"};
char access_code [4][15] = {"1123X5", "1154V2", "8823Q1", "5412R7",};
cout << "\n Please enter department number to retreive the access code:";
cout << "\n Accounting = 1";
cout << "\n Receiving = 2";
cout << "\n Sales = 3";
cout << "\n Management = 4" << endl;
int deptName;
cin >> deptName;
cout << department[deptName-1] << " ";
cout << "\nAccess code is: " << access_code[deptName-1] << endl;
return 0;
}
-
since this is C++, use 'iostream' instead of 'iostream.h'. instead of using char[]'s use 'string's (youll need to #include<string> first) unless your required to use char[]'s or want to. after your include statements do "using namespace std;".
the program looks like it will do what is asked. but it doesnt really 'search' for it. maybe he wants you to retrieve a string for the department name and use a for loop to loop through each element in department and compare the strings, then return the access code at that index.
i think it would be either this way or the way you have it, but the way i described sounds more like 'searching', but i guess its up to the teacher and what he's been teaching you.
-
Well, the assignment says to load the data, maybe implying input. It also sounds like deptName could be a string that you ask the user for, which might imply string comparison. You should probably just also check whether what the user enters is actually a valid department (in your solution, I could give it a number like 841875 and it would crash).