LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-30-2012, 09:30 AM   #1
DemiSheep
LQ Newbie
 
Registered: Dec 2010
Posts: 17

Rep: Reputation: 0
Question Two problems with my school project


I am having two problems with my school project. I am writing it using VIM in UNIX on the school server.

First problem:

my menu is doing this:

Code:
MENU
(I)nsert new record
(L)ast name search
(S)ave database to a file
(R)ead database from a file
(Q)uit

Enter choice: i

Insert new record selected

Please enter employee's Last Name: 
Please enter employee's First Name:
It is skipping ahead of Last Name!

Do I need to add cout.flush() somewhere? When I add cout.flush() right under the last name section I get this error:

Code:
unixapps1:Lab1> make
g++ -o LSL lab1.cpp Employee.cpp
lab1.cpp: In function âEmployee createRecord()â:
lab1.cpp:41: error: âstruct std::istreamâ has no member named âflushâ
My menu code in my is this:

Code:
160 int main() {
161 
162         cout << "\n"
163                 << "************************************************************\n"
164                 << "Remember: Search for TODO's in VI before submitting project!\n"
165                 << "************************************************************\n\n";
166 
167                 char menu_choice;
168                 string fileName;
169 
170                 // Create a database
171                 LinkedSortedList<Employee> database;
172 
173 while (true) {
174                 // Display menu
175                 cout << "MENU\n"
176                          << "(I)nsert new record\n"
177                          << "(L)ast name search\n"
178                          << "(S)ave database to a file\n"
179                          << "(R)ead database from a file\n"
180                          << "(Q)uit\n\n";
181 
182                          cout << "Enter choice: ";
183                          cin >> menu_choice;
184                          cout << endl;
185 
186                                 switch (toupper(menu_choice)) {
187 
188                                 case 'I':
189                                         cout << "Insert new record selected\n\n";
190                                         {database.insert(createRecord());}
191                                         break;
192 
193                                 case 'L':
194                                         cout << "Last name search selected\n\n";
195                                         // TODO call function to search by last name
196                                         // TODO call search for Last Name
197                                         // TODO Print all matches found
198                                         {string searchVal = "";
199                                         database.find(searchVal);}
200                                         break;
201 
202                                 case 'S':
203                                         cout << "Save database to a file selected\n\n";
204                                         // TODO call function to save database to file
205                                         // File I/O Save to file
206                                         cout << "Please enter a file name: " << endl;
207                                         cin >> fileName;
208                                         {char* file = (char*) fileName.c_str();
209                                         writeFile(file, database);}
210                                         break;
211 
212                                 case 'R':
213                                         cout << "Read database from a file selected\n\n";
214                                         // TODO call function to read database from file
215                                         // File I/O Read file
216                                         cout << "Please enter a file name: " << endl;
217                                         cin >> fileName;
218                                         {char* file = (char*) fileName.c_str();
219                                         readFile(file, database);}
220                                         break;
221 
222                                 case 'Q':
223                                         exit(EXIT_SUCCESS);
224 
225                                 // default case:
226                                 default:
227                                         cout << "Invalid choice!\n\n"
228                                         << "##### Please try again #####\n\n";
229                                         break;
230 
231                 cin >> menu_choice;
232                                 }
233         }
234   return 0;
235 }
236
My createRecord() function is this:

Code:
 29 // Create the record to be inserted into the employee database
 30 Employee createRecord() {
 31         // Temporary variables for getting input from user
 32         string stringInput = "";
 33         int intInput = 0;
 34 
 35         Employee newEmployee;
 36 
 37         cout << "Please enter employee's Last Name: " << endl;
 38         getline(cin, stringInput);
 39         newEmployee.setLastName(stringInput);
 40 
 41         cout << "Please enter employee's First Name: " << endl;
 42         getline(cin, stringInput);
 43         newEmployee.setFirstName(stringInput);
 44 
 45         cout << "Please enter employee's ID: ";
 46         getline(cin, stringInput);
 47         stringstream myStream(stringInput);
 48         myStream >> intInput;
 49         newEmployee.setId(intInput);
 50 
 51         cout << "Please enter employee's Salary: ";
 52         getline(cin, stringInput);
 53         myStream >> intInput;
 54         newEmployee.setSalary(intInput);
 55 
 56         cout << "Please enter employee's Department: ";
 57         getline(cin, stringInput);
 58         newEmployee.setDepartment(stringInput);
 59 
 60         cout << "Please enter employee's Phone Number: ";
 61         getline(cin, stringInput);
 62         newEmployee.setPhoneNumber(stringInput);
 63 
 64         cout << "Please enter employee's Address: ";
 65         getline(cin, stringInput);
 66         newEmployee.setAddress(stringInput);
 67 
 68         cout << "Please enter employee's Hire Date: ";
 69         getline(cin, stringInput);
 70         newEmployee.setHireDate(stringInput);
Second problem:

I am getting the following build error:

Code:
unixserver:Lab1> make
g++ -o LSL lab1.cpp Employee.cpp
lab1.cpp: In function âint main()â:
lab1.cpp:199: error: no matching function for call to âLinkedSortedList<Employee>::find(std::string&)â
LinkedSortedList.cpp:137: note: candidates are: bool LinkedSortedList<Elem>::find(Elem) const [with Elem = Employee]
make: *** [main] Error 1
Here is my find function:

Code:
132 // Check to see if "value" is in the list.  If it is found in the list,
133 // return true, otherwise return false.  Like print(), this function is
134 // declared with the "const" keyword, and so cannot change the contents
135 // of the list.
136 template<class Elem>
137 bool LinkedSortedList<Elem>::find(Elem searchvalue) const {
138         if (head == NULL) {
139                 return false;
140         } else {
141                 while (head != NULL) {
142                         LinkedNode<Elem>* pointer;
143                         for (pointer = head; pointer != NULL; pointer = pointer->next) {
144                                 if (pointer->value == searchvalue) {
145                                         return true;
146                                 }
147                         }
148                 }
149                 return false;
150         }
151 }
And this is in my LinkedSortedList.h file under the "public:" section:

Code:
 33         bool find(Elem searchvalue) const;
Also, if anyone could give me hints on using gdb in Unix that would be helpful...

Thanks in advance!
 
Old 04-30-2012, 10:42 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Code:
182                          cout << "Enter choice: ";
183                          cin >> menu_choice;
the second statement reads a character from cin, leaving a newline in the stream, which is then read by subsequent getline() call in your createRecord() function. You should clear the input stream, actually. I believe it's istream::sync() or something like that. cout.flush() will have no effect on this.

As for your second problem: Your find() function wants an instance of Employee as its argument. You're giving it a string. Either make an overloaded find() (or change the one you have) that takes string as its argument (or, even beter, a const string&), or call it with an Employee instead (I hope you have the corresponding overloaded == operator implemented), or overload a cast from string to Employee.

Last edited by millgates; 04-30-2012 at 10:45 AM.
 
Old 04-30-2012, 10:54 AM   #3
DemiSheep
LQ Newbie
 
Registered: Dec 2010
Posts: 17

Original Poster
Rep: Reputation: 0
Thanks for your input. I'll look into what you said about my second problem.

I fixed the first problem by adding this after Last name section from a post in another forum:

Code:
cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to manage system alerts and detect performance problems in CentOS (School project)? Moisemark Linux - Newbie 5 02-16-2012 04:09 AM
School Project - Please Help Moisemark Linux - Hardware 1 02-16-2012 02:31 AM
School Project Bovine64 Linux - Networking 2 04-17-2006 06:15 PM
I need a little help for a school project... Tsuroerusu General 25 12-12-2004 07:34 AM
My school project Growly Linux User Groups (LUG) 1 04-30-2004 04:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration