0

ok so I haven't found a topic that is completely like my situation and haven't found anything that works for my situation. Currently I am working on a project for school and in the snippet of code below, I need to input more than one word for the salesItem[i]. I have tried many variations of this and haven't gotten it to work. Also see the screen shot below. Thank you if you can help and would appreciate if you could explain an answer for a beginner to understand! In the output you will see that nothing has actually been input into the program. It skips right over it to asking for price of that item.

case 1:
            i = 1;
            cout<<"Intial setup: Enter sales items"<<endl;
                do
                {  
                    cout<<"Add sales item "<<i<<": ";
                    getline (cin,name);
                    salesItem[i] = name;
                    cout<<salesItem[i]<<" was entered."<<endl;
                    cout<<"Add a price for "<<salesItem[i]<<": ";
                    cin>> salesItemPrice[i];
                    i = i + 1;
                    cout<<"Are you done? Enter 'yes' or 'no'."<<endl;
                    cin>>exitChoice;
                }
                while((exitChoice != "yes") && (exitChoice == "no"));

Here is the full code for my project, the above is the snippet. I am having issues with.

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;




int main()
{
    
    int i = 30;
    int choice;
    string exitChoice = "";
    string salesItem[i];
    float salesItemPrice[i];
    int numOfSalesItems[i];
    string itemSearch = "";
    string newItemName = "";
    string itemNameUpdate ="";
    float newSalesPrice;
   string name = "";
   
    
    
    
   do
   {
    
    cout<<"          Main Menu         "<<endl;
    cout<<"----------------------------"<<endl;
    cout<<"Please select a menu number:"<<endl;
    cout<<"1. Initial sales item setup"<<endl;
    cout<<"2. Modify sales item"<<endl;
    cout<<"3. Display all sales items"<<endl;
    cout<<"4. Add new sales item(s)"<<endl;
    cout<<"10. Exit Program"<<endl;
    cin>>choice;
    
    
        switch(choice)
        {
            case 1:
            i = 1;
            cout<<"Intial setup: Enter sales items"<<endl;
                do
                {  
                    cout<<"Add sales item "<<i<<": ";
                    getline (cin,name);
                    salesItem[i] = name;
                    cout<<salesItem[i]<<" was entered."<<endl;
                    cout<<"Add a price for "<<salesItem[i]<<": ";
                    cin>> salesItemPrice[i];
                    i = i + 1;
                    cout<<"Are you done? Enter 'yes' or 'no'."<<endl;
                    cin>>exitChoice;
                }
                while((exitChoice != "yes") && (exitChoice == "no"));
                
            

                break;
            case 2:
                cout<<"Modify sales item"<<endl;
                cout<<"Enter the name of the item to be modified"<<endl;
                cin>>itemSearch;
                for(int x = 1; x<i; x++)
                {
                    if(salesItem[x] == itemSearch)
                        {
                        int menuChoice;
                            
                        cout<<"Found Item: "<< salesItem[x]<<endl;
                        cout<<"Please select a menu number."<<endl;
                        cout<<"1. Modify name"<<endl;
                        cout<<"2. Modify item price"<<endl;
                        cout<<"3. Exit Menu"<<endl;
                        cin>>menuChoice;
                        
                        
                        switch(menuChoice)
                        {
                        case 1:
                            cout<<"         Modify Name        "<<endl;
                            cout<<"----------------------------"<<endl;
                            cout<<"Enter the new name for "<<salesItem[x]<<endl;
                            cin>>newItemName;
                            salesItem[x] = newItemName;
                            cout<<"The new item name is: "<< salesItem[x]<<endl;
                            cout<<"Is this correct? Enter 'yes' or 'no'."<<endl;
                            cin>>itemNameUpdate;
                            
                            if(itemNameUpdate == "yes")
                            {
                                cout<<"Item name updated."<<endl;
                                break;
                            }
                            else if (itemNameUpdate == "no")
                            {
                                do
                                {
                                cout<<"Try again."<<endl;
                                cout<<"Enter the new name for "<<salesItem[x]<<endl;
                                cin>>newItemName;
                                salesItem[x] = newItemName;
                                cout<<"The new item name is: "<< salesItem[x]<<endl;
                                cout<<"Is this correct? Enter YES or NO."<<endl;
                                cin>>itemNameUpdate;
                                }
                                while(itemNameUpdate != "yes");
                            }
                        break;
                            
                        case 2:
                            cout<<"         Modify Price       "<<endl;
                            cout<<"----------------------------"<<endl;
                            cout<<"Enter the new price for "<<salesItem[x]<<endl;
                            cin>>newSalesPrice;
                            salesItemPrice[x] = newSalesPrice;
                            cout<<"The new price for this item is "<<salesItemPrice[x]<<endl;
                            cout<<"Is this correct? Enter 'yes' or 'no'."<<endl;
                            cin>>itemNameUpdate;
                            
                            if(itemNameUpdate == "yes")
                            {
                                cout<<"Price has been updated."<<endl;
                            }
                            else if (itemNameUpdate == "no")
                            {
                                do
                                {
                                cout<<"Try again."<<endl;
                                cout<<"Enter the new price for "<<salesItem[x]<<endl;
                                cin>>newSalesPrice;
                                salesItemPrice[x] = newSalesPrice;
                                cout<<"The new price for this item is "<<salesItemPrice[x]<<endl;
                                cout<<"Is this correct? Enter 'yes' or 'no'."<<endl;
                                cin>>itemNameUpdate;
                                }
                                while(itemNameUpdate != "yes");
                            }
                            
                        }
                        }
                }
                 break;   
                    
                    
                    
                    
                
            case 3:
            std::cout << std::fixed<<std::left;
                cout<<"      Display all sales items       "<<endl;
                cout<<"------------------------------------"<<endl;
                cout<<"        Sales Item       Item Price "<<endl;
                for(int x = 1; x < i; x++){
                    
                    cout<<"Item "<<x<<": "<<setw(17)<<salesItem[x]<<"$"<<setprecision(2)<<salesItemPrice[x]<<endl;
                        
                }
                
                break;
            
            case 4:
                cout<<"Add new sales item"<<endl;
                do
                {  
                    cout<<"Add sales item "<<i<<": ";
                    cin>>salesItem[i];
                    cout<<salesItem[i]<<" was entered."<<endl;
                    cout<<"Add a price for "<<salesItem[i]<<": ";
                    cin>> salesItemPrice[i];
                    i = i + 1;
                    cout<<"Are you done? Enter 'yes' or 'no'."<<endl;
                    cin>>exitChoice;
                }
                while(exitChoice != "yes");
                
                
                
                
            case 10:
                cout<<"Exiting Program"<<endl;
                break;
        }
    
    }
    while(choice != 10);
    
    
    
    
}

Output.....

      Main Menu         

Please select a menu number:

  1. Initial sales item setup
  2. Modify sales item
  3. Display all sales items
  4. Add new sales item(s)
  5. Exit Program 1 Intial setup: Enter sales items Add sales item 1: was entered. Add a price for :

enter image description here

also as you can see below here, works fine this way

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

int main () 
{
  string name;
  cout << "Enter Name: ";
  getline (cin,name);
  cout << "You entered: " << name;
}

output.....

Enter Name: John Smith You entered: John Smith

enter image description here

  • Please paste all code and output as text. Also make a [mre]. – cigien Nov 29 '20 at 15:48
  • It's almost certainly because of a formatted extraction earlier in your code that you've omitted from this question. See [this](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) question for more about what happens when you mix statements like `cin >> exitChoice` and `getline(cin, name)`. On an unrelated note, `(exitChoice != "yes") && (exitChoice == "no")` is exactly the same thing as `exitChoice == "no"`. – Nathan Pierson Nov 29 '20 at 15:50
  • Yeah, I was using other strings there, but switched them just to make the program work. Was trying to figure out the best way to "check" for wrong inputs there. I'll check out that question. – Thomas Dilley Nov 29 '20 at 15:55
  • Thank you for that link..... So that people know when they come to this post, the cin>>input leaves behind an empty character from hitting enter or return on the keyboard. In order to use getline(cin, input) you must simply use cin.ignore() after cin>>input. There are parameters you can use with cin.ignore(), however I haven't learned about that quite yet. – Thomas Dilley Nov 29 '20 at 16:26

0 Answers0