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:
- Initial sales item setup
- Modify sales item
- Display all sales items
- Add new sales item(s)
- Exit Program 1 Intial setup: Enter sales items Add sales item 1: was entered. Add a price for :
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