Kirasiris
Kirasiris

Reputation: 550

How to accept string 'end' to finish program in a Vector using C++?

So I declare an int variable called listOfItems that can accept input from the user. Now that variable can be a big number but if the user decides to stop adding items into the vector(using the 'string value' var) he/she can easily stop by just typing 'end' into the program.

  // DECLARE INT listOfItems and shoppingList vector
  int listOfItems = 0;
  vector<string> shoppingList;
  cout << "Please enter the number of items you wish to purchase: " << endl;
  cout << "type end to finish program " << endl;

  cin >> listOfItems;
  /// SECOND VERSION AFTER READING SOME COMMENTS
  string value;
  do {
    cin >> value;
    shoppingList.push_back(value);  // add new value to vector
  } while(value != "end" && value != "END");


/// MAIN VERSION
for (int i = 0; i < listOfItems; i++) {
    string value;
    cin >> value;
    do {
      shoppingList.push_back(value);  // add new value to vector
    } while(value != "end" && value != "END");
  }

In the block above, I just create a for-loop that will run as many times as specified in the listOfItems submitted by the user. Inside said for-loop, I added a do while, that should add items into the vector as long as the string has not met the 'end' or 'END' keyword.

It is not working. Does anyone have any idea on fixing this?

Upvotes: 0

Views: 68

Answers (4)

Gerard097
Gerard097

Reputation: 825

You should check for the value 'END' before actually inserting otherwise you will have that value inside your h shopping list (you could just pop the last item but nah).

//Check if v is not END before actually inserting the element
for(std::string v; shoppingList.size() < listOfItems 
                   && std::cin >> v && v != "END" && v != "end";) {
  //Move it since you won't use v anymore
  shoppingList.push_back(std::move(v));
}

Upvotes: 1

Nikhil Badyal
Nikhil Badyal

Reputation: 1697

You could do something like

string value;
int i = 0;
while(cin>>value && i++ != 10 && value != "END" && value != "end")
{
   shoppingList.push_back(value);
}

Upvotes: 0

scohe001
scohe001

Reputation: 15446

string value;
cin >> value;
for (int i = 0; i < listOfItems && value != "END" && value != "end"; i++) {
    shoppingList.push_back(value);  // add new value to vector 
    cin >> value;
}

This will do what you want. It...reads a value in from the user. Checks if that value is "end" or "END" and that we still don't have enough items. It adds what they entered to the shopping list (now that we've determined it's good). It reads another item and does it all again.

Upvotes: 0

user10957435
user10957435

Reputation:

You actually need to accept input in your while loop:

do {
    string value;
    cin >> value;
    shoppingList.push_back(value);  // add new value to vector
} while(value != "end" && value != "END");

Otherwise this will run continually. This way, you actually base what you're inserting in the std::vector on user input.

Upvotes: 0

Related Questions