2

This is what I have so far. I am trying to edit a dynamically allocated array in C++, however, when the for loop runs, it is skipping over the first item. I need it to get all of the items. Any help is appreciated. Thanks in advance.

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


int main()
{
    // Declare Variables
    int userChoice = 0;
    int numItems = 0;

    cout << "How many items will be on your list? ";
    cin >> numItems;

    string *list = new string[numItems];

    // Give the user some options
    cout << "1. Add Item" << endl;
    cout << "2. Remove Item" << endl;
    cout << "3. Sort Items" << endl;
    cout << "4. Exit" << endl;
    cout << "Enter the number of the operation you wish to perform: ";
    cin >> userChoice;
    cout << endl;

    // Perform the operation
    switch(userChoice)
    {
    case 1:
        {
            cin.clear();    // Remove new line from cin

            for(int i = 0; i < numItems; i++)
            {
                cout << "Item #" << i + 1 << " --> ";
                getline(cin, list[i]);
            }
        }
        break;
    case 2:
        {

        }
        break;
    case 3:
        {

        }
        break;
    case 4:
        {
            return 0;
        }
    default:
        {
            cout << "Error! Invalid Selection" << endl;
        }

    }

    // Output the list
    cout << "-------Items-------" << endl
         << *list << endl << endl;

    // free memory
    delete [] list;

    cout << "Enter the number of the operation you wish to perform: ";
    cin >> userChoice;


    return 0;
}

2 Answers 2

3

It seems odd to me to be using the STL for some things (like string) and then use a standard array to hold the strings. Also, list is a standard type of object in STL, so that is not a great name for a variable. This revised code fixes your issues with ignoring the first line, and also uses a vector instead of doing new and delete.

#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <vector>
#include <iterator>
using namespace std;

int main()
  {
  // Declare Variables
  int userChoice = 0;
  int numItems = 0;

  cout << "How many items will be on your myList? ";
  cin >> numItems;
  cin.ignore ();

  vector<string> myList;

  while (true)
    {
    // Give the user some options
    cout << "1. Add Item" << endl;
    cout << "2. Remove Item" << endl;
    cout << "3. Sort Items" << endl;
    cout << "4. Exit" << endl;
    cout << "Enter the number of the operation you wish to perform: ";
    cin >> userChoice;
    cin.ignore ();
    cout << endl;

    // Perform the operation
    switch(userChoice)
    {
    case 1:
        {
        for(int i = 0; i < numItems; i++)
            {
            cout << "Item #" << i + 1 << " --> ";
            string s;
            getline(cin, s);
            myList.push_back (s);
            }
        }
        break;
    case 2:
        {

        }
        break;
    case 3:
        {
        sort (myList.begin (), myList.end ());
        }
        break;
    case 4:
        {
            return 0;
        }
    default:
        {
            cout << "Error! Invalid Selection" << endl;
        }

    }

    // Output the myList
    cout << "-------Items-------" << endl;
    copy(myList.begin(), myList.end(), ostream_iterator<string>(cout, "\n"));

  } // end of while
}

The project description explicitly says I can't use standard containers, sort functions, or smart pointers

Redone below to not use those things. :)

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

int myCompare (const void * a, const void * b)
  {

  if ((*(string *) a) < *(string *) b) return -1;
  if ((*(string *) a) > *(string *) b) return 1;
  return 0;
  }

int main()
  {
  // Declare Variables
  int userChoice = 0;
  int numItems = 0;

  cout << "How many items will be on your myList? ";
  cin >> numItems;
  cin.ignore ();

  string *myList = new string[numItems];

  while (true)
    {
    // Give the user some options
    cout << "1. Add Item" << endl;
    cout << "2. Remove Item" << endl;
    cout << "3. Sort Items" << endl;
    cout << "4. Exit" << endl;
    cout << "Enter the number of the operation you wish to perform: ";
    cin >> userChoice;
    cin.ignore ();
    cout << endl;

    // Perform the operation
    switch(userChoice)
    {
    case 1:
        {
        for(int i = 0; i < numItems; i++)
            {
            cout << "Item #" << i + 1 << " --> ";
            getline(cin, myList [i]);
            }
        }
        break;
    case 2:
        {

        }
        break;
    case 3:
        {
        qsort (myList, numItems, sizeof (string *), myCompare);
        }
        break;
    case 4:
        {
            delete [] myList;
            return 0;
        }
    default:
        {
            cout << "Error! Invalid Selection" << endl;
        }

    }

    // Output the myList
    cout << "-------Items-------" << endl;
    for(int i = 0; i < numItems; i++)
      cout << myList [i] << endl;

  } // end of while
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is great, and would be what I would've done but I probably should have been more specific in my initial description. The project description explicitly says I can't use standard containers, sort functions, or smart pointers
Or does qsort count as a sort function? Pfft. How can you sort without using a sort function? Write your own? Then it becomes a sort function.
2

The for loop isn't skipping the first element. The first element is just a an empty line.

Because the following clears the error flags.

cin.clear();    // Remove new line from cin --> No!!!!

If you want to skip until the new line you have to use ignore() instead.

cin.ignore(numeric_limits<streamsize>::max(), '\n'); // this removes until newline from cin !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.