0

My code below, whenever I input the menu items, will automatically end the program if I add more than one word such as "Tequila Sunrise" rather than "Tequila". How do I fix this to make it so I can add items with more than one word in the name?

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    // Restaurant Side Title
    cout << "Welcome to the Menu Ordering System. \n\n\n";

    // Create a txt file to store the menu
    fstream MenuWriteFile("drinksmenu.txt");
    fstream MenuPricesFile ("drinksprices.txt");
    
    // Write the menu to the file
    string myDrinks;
    double drinksPrices;
    int i;
    cout << "Please enter your first drink: ";
    cin >> myDrinks;
    MenuWriteFile << myDrinks << endl;
    cout << "Price: ";
    cin >> drinksPrices;
    MenuPricesFile << drinksPrices << endl;
    cout << "To add another drink type 1, if not type 0: ";
    cin >> i;
    while (i == 1) {
        cout << "Please enter your next drink: ";
        cin >> myDrinks;
        MenuWriteFile << myDrinks << endl;
        cout << "Price: ";
        cin >> drinksPrices;
        MenuPricesFile << drinksPrices << endl;
        cout << "To add another drink type 1, if not type 0: ";
        cin >> i;
    }
    
    MenuWriteFile.close();
    MenuPricesFile.close();
    
    // Create a text string, which is used to output the text file
     string myText;

     // Read from the text file
     ifstream MenuReadFile("drinksmenu.txt");

     while (getline (MenuReadFile, myText)) {
       cout << "\n\n" << myText << "\n";
     }

     // Close the file
     MenuReadFile.close();
    return 0;
    }
  • 1
    Does this answer your question? [Code for getting multiple words in a string from user](https://stackoverflow.com/questions/4825483/code-for-getting-multiple-words-in-a-string-from-user) – 273K May 03 '21 at 15:34

1 Answers1

1

Use std::getline():

while (i == 1) {
  cout << "Please enter your next drink: ";
  std::getline(std::cin >> std::ws, myDrinks);
  ....
David G
  • 94,763
  • 41
  • 167
  • 253
  • Would I still need std:: if I have using namespace std? – Tom Rogers May 03 '21 at 15:34
  • @TomRogers Nope. – David G May 03 '21 at 15:34
  • ***if I have using namespace std?*** Since `using namespace std;` is considered a bad practice you will see many answers with `std::` from experienced users. – drescherjm May 03 '21 at 15:35
  • Related: [https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – drescherjm May 03 '21 at 15:36
  • Usually it would output the contents of the txt file at the end but now it's not, any ideas why? – Tom Rogers May 03 '21 at 15:47
  • Even when we don't mean to add the `std::` muscle memory just seems to add it in after a few years. – user4581301 May 03 '21 at 15:48
  • @TomRogers You might need to change `while (getline(MenuReadFile, myText))` at the bottom to `while (getline(MenuReadFile >> ws, myText))`. – David G May 03 '21 at 16:16