动态分配字符串数组

时间:2016-02-06 01:40:45

标签: c++ arrays string

这是我到目前为止所拥有的。我试图在C ++中编辑一个动态分配的数组,但是,当for循环运行时,它会跳过第一个项目。我需要它来获取所有项目。任何帮助表示赞赏。提前谢谢。

#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 个答案:

答案 0 :(得分:3)

对于某些事情(比如字符串)使用STL然后使用标准数组来保存字符串对我来说似乎很奇怪。此外,list是STL中的标准对象类型,因此它不是变量的重要名称。此修订后的代码通过忽略第一行来解决您的问题,并使用vector代替newdelete

#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
}
  

项目描述明确表示我不能使用标准容器,排序函数或智能指针

重做下面不要使用那些东西。 :)

#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
}

答案 1 :(得分:2)

for循环不会跳过第一个元素。第一个元素只是一个空行。

因为以下clears错误标记。

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

如果您想跳到新行,则必须使用ignore()

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