C++: Can someone help me with this code?
-
I am writing a code to allow a user to enter 5 names. Then they can enter an additional name to add to the list, and delete names from the list. The deleting works fine, but when I run the program the output says: enter a new name and then it just continues with the output, reprinting the list and asking the user to enter a name to delete. At this point, the user can enter names to delete. Here is my code: #include <iostream> #include <string> using namespace std; void read5names(string* names, int size); void displayNames(string* names, int size); string* addEntry(string* dynamicArray, int &size, string newEntry); string* deleteEntry(string* dynamicArray, int &size, string entryToDelete); int findName(string* dynamicArray, int size, string entryToDelete); int main() { // declarations string* names; string newname; int size = 5; int i; names = new string[5]; // read 5 names cout << "Enter 5 names, one per line\n"; read5names(names, size); cout << "\n===== ORIGINAL LIST =====\n"; displayNames(names, size); // add a name and resize cout << "\nEnter a new name to add\n"; getline(cin, newname); names = addEntry(names, size, newname); cout << "\n===== LONGER LIST =====\n"; displayNames(names, size); // remove a name and resize - attempt 1 cout << "\nEnter a name to delete\n"; getline(cin, newname); names = deleteEntry(names, size, newname); cout << "\n===== SHORTER LIST =====\n"; displayNames(names, size); // remove a name and resize - attempt 2 cout << "\nEnter a name to delete that is NOT in the list\n"; getline(cin, newname); names = deleteEntry(names, size, newname); cout << "\n===== FINAL LIST =====\n"; displayNames(names, size); } void displayNames(string* names, int size) { int i; for (i = 0; i < size; i++) cout << names[i] << endl; } void read5names(string* names, int size) { int i; for (i = 0; i < size; i++) cin >> names[i]; } string* addEntry(string* dynamicArray, int &size, string newEntry) { // declare new array one element larger string *newArr; newArr = new string[size + 1]; // copy names over into new array int i; for (i = 0; i < size; i++) newArr[i] = dynamicArray[i]; // add the new name newArr[size] = newEntry; // increase the size size++ // delete the old array delete[] dynamicArray; // return the pointer to the new array return newArr; } string* deleteEntry(string* dynamicArray, int &size, string entryToDelete) { // determine if entryToDelete is in the dynamicArray int location; // location of the match location = findName(dynamicArray, size, entryToDelete); if (location >= 0 ) { // name found. Now delete it. // declare new array one size smaller string *newArr; newArr = new string[size -1]; // copy names over into new array int i; for (i = 0; i < location; i++) newArr[i] = dynamicArray[i]; if (i = location) i++; if ((i > location) &&(i < (size - 1))) { newArr[i - 1] = dynamicArray[i]; i++; } // decrement size size--; //delete the old array delete[] dynamicArray; // return the pointer to the new array return newArr; } else { // name not found cout << entryToDelete << " not found!\n"; // return the original array pointer return dynamicArray; } } int findName(string* dynamicArray, int size, string entryToDelete) { int i; for (i = 0; i < size; i++) { if (dynamicArray[i] == entryToDelete) { return i; break; } } return -1; } } **sorry the indentation doesn't work
-
Answer:
I just replaced getline(cin, newname) with cin>>newname; It did work. However, something is wrong with your function deleteEntry
bethann_... at Yahoo! Answers Visit the source
Related Q & A:
- Can someone help me find a study abroad program?Best solution by Yahoo! Answers
- Can someone help me with my psp warranty?Best solution by Yahoo! Answers
- Can someone help me locate an orphanage in Israel?Best solution by Yahoo! Answers
- Can anyone help me get the code for my stereo?Best solution by Yahoo! Answers
- Can you help me with this code for myspace?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.