c++ - Dynamically Allocating String Arrays -


this have far. trying edit dynamically allocated array in c++, however, when loop runs, skipping on first item. need of items. appreciated. 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 on list? ";     cin >> numitems;      string *list = new string[numitems];      // give user options     cout << "1. add item" << endl;     cout << "2. remove item" << endl;     cout << "3. sort items" << endl;     cout << "4. exit" << endl;     cout << "enter number of operation wish perform: ";     cin >> userchoice;     cout << endl;      // perform operation     switch(userchoice)     {     case 1:         {             cin.clear();    // remove new line cin              for(int = 0; < numitems; i++)             {                 cout << "item #" << + 1 << " --> ";                 getline(cin, list[i]);             }         }         break;     case 2:         {          }         break;     case 3:         {          }         break;     case 4:         {             return 0;         }     default:         {             cout << "error! invalid selection" << endl;         }      }      // output list     cout << "-------items-------" << endl          << *list << endl << endl;      // free memory     delete [] list;      cout << "enter number of operation wish perform: ";     cin >> userchoice;       return 0; } 

the loop isn't skipping first element. first element empty line.

because following clears error flags.

cin.clear();    // remove new line cin --> no!!!! 

if want skip until new line have use ignore() instead.

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

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -