do
{
system("cls");
cout<<"Enter Item Name:"<<endl;
cin.ignore();
cin.getline(item_name,size);
cout<<item_name<<endl;
cout<<"Enter Item Price:"<<endl;
cin>>item_price;
cout<<" Do You Want To Add More Item..?\nPress Y/N."<<endl;
char c;
c=getche();
}while(c=='y'||c=='Y');
this is the pace of code , actually i am using this in my project, i cannot remove cin.ignore because i have to get input from user unless user press any other character rather then y
You know the answer to your question in the title. The call
cin.ignore();
reads the next character from cin
and discards it.
The real question that you are grappling with, I think, is: how do you terminate the loop?
That's simple enough. Don't compare just one character. Compare the entire line.
do
{
system("cls");
cout << "Enter Item Name:"<<endl;
cin.getline(item_name, size);
cout << item_name << endl;
cout << "Enter Item Price:" << endl;
cin >> item_price;
cout << "Do You Want To Add More Item..?\nPress Y/N." << endl;
std::string ans;
getline(cin, ans);
} while (ans == "y" || ans == "Y");
You can use following. Use cin.ignore();
after the line c=getche();
:
char c;
do
{
system("cls");
cout<<"Enter Item Name:"<<endl;
cin.getline(item_name,size);
cout<<item_name<<endl;
cout<<"Enter Item Price:"<<endl;
cin>>item_price;
cout<<" Do You Want To Add More Item..?\nPress Y/N."<<endl;
c=getche();
cin.ignore();
}while(c=='y'||c=='Y');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.