c++ - 为什么使用 cin.clear() 不清除输入?
问题描述
我将值添加到程序中并将其打印回来以检查存储了哪些值,但在打印值之前,我希望清除 cin 并打印列表为空。问题是它还在打印保存的列表,没有用cin.clear()反映 如何实现呢?如果我没有错,我认为我没有以正确的方式做这件事。
编码:
#include <iostream>
#include <vector>
#include <limits>
using namespace std;
int main()
{
vector <int> number{};
char selection{};
do{
cout << "\nMain Menu" << '\n';
cout << "P - Print numbers" << '\n';
cout << "A - Add numbers" << '\n';
cout << "M - Show the medium number" << '\n';
cout << "S - Show small number" << '\n';
cout << "L - Show large number" << '\n';
cout << "Q - Quite the program" << '\n';
cout << "------------------------" << '\n';
cout << "Enter your Choice: ";
cin >> selection;
if(selection == 'p' || selection == 'P')
{
cin.clear();
{
if (number.size() == 0)
cout << "[] - The list is Empty" << '\n';
else{
cout << " [ ";
for(int num:number)
cout << num << " ";
cout << " ] " << '\n';
}
}
}else if(selection == 'a' || selection == 'A')
{
int num_to_add{};
cout << "Enter the number to add: ";
cin >> num_to_add;
number.push_back(num_to_add);
cout << num_to_add << " Successfully Added" << '\n';
cin.clear();
}else if(selection == 'm' || selection == 'M')
{
if (number.size() == 0)
cout << "[] - The list is Empty, unable to calculate" << '\n';
else{
int total{};
for(int num:number)
total += num;
cout << "The middle number is: " <<static_cast<float>(total)/number.size() << '\n';
}
}else if(selection == 's' || selection == 'S')
{
if (number.size() == 0)
cout << "[] - The list is Empty, unable to retrieve smaller value" << '\n';
else{
int smallest = number.at(0);
for(int num:number)
if(num < smallest)
smallest = num;
cout << "The smallest number is: " << smallest << '\n';
}
}else if(selection == 'l' || selection == 'L')
{
if (number.size() == 0)
cout << "[] - The list is Empty, unable to retrieve larger value" << '\n';
else{
int larger = number.at(0);
for(int num:number)
if(num > larger)
larger = num;
cout << "The larger number is: " << larger << '\n';
}
}
}while (selection != 'q' && selection != 'Q');
return 0;
}
解决方案
你的数据存储在vector < int > number{}中;当您需要清空存储的数据时, 您可以通过简单地说number.clear()来清除数字。
更多解释:只需运行我的代码,添加数字,按 c 清除,然后检查您的列表。
#include <iostream>
#include <limits>
#include <vector>
using namespace std;
void mainMenu();
void dataValidation();
void CalcuLation();
void outPuts();
int main()
{
dataValidation();
return 0;
}
void dataValidation()
{
int bad_index = 0;
string Uinput;
cout << "Enter the number only to check if You are not a bot: ";
cin >> Uinput;
bool isvalid_ = true;
if(Uinput[0] == '0')
{
isvalid_ = false;
cout << "Oops! Entering Zero is not allowed. Try again with correct number." << '\n';
return ;
}
for (int countingPoint = 0; countingPoint < Uinput.length() && isvalid_; countingPoint++)
{
if (!(Uinput [countingPoint] >= 48 && Uinput[countingPoint] <= 57))
{
isvalid_ = false;
cout << "Not Success" << '\n';
bad_index = countingPoint;
break;
}
}
if(isvalid_)
{
int userChoice_;
char restart_;
do{
system("cls");
cout << "Thank you for Entering the correct number to solve bot captcha..." << '\n';
cout << "-----------------------------------------------------------------" << '\n';
mainMenu();
cout<< " Enter the Choice: ";
while (!(cin>>userChoice_))
{
system("cls");
cin.clear();
cin.ignore(numeric_limits<streamsize>:: max(), '\n');
system("cls");
cout<< " Wrong choice detected! Illegal stance stopped! Try again." << '\n';
cout<< " *********************************************************" << '\n';
mainMenu();
cout<< " Enter the Choice: ";
}
do{
cin.clear();
cout << " Do you want to Restart the App? [Y/n] " ;
cin >> restart_;
system("cls");
mainMenu();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}while (restart_!='Y'&&restart_!='y'&&restart_!='N'&&restart_!='n');
}while (restart_ == 'Y' || restart_ == 'y');
return;
}
else
cout << "Value Entered: ["<< Uinput[bad_index] << "] is illegal which is not a number. Try again with a correct number." << '\n';
}
void mainMenu()
{
cout << "Main Menu" << '\n';
cout << "A for Adding Numbers" << '\n';
cout << "P for Printing Saved numbers" << '\n';
cout << "S for Printing Saved numbers" << '\n';
cout << "L for Printing Saved numbers" << '\n';
cout << "C to to clear the saved list" << '\n';
cout << "Q to Quite the Program" << '\n';
cout << "Enter your Choice: ";
CalcuLation();
}
void CalcuLation()
{
vector <int> number{};
vector <int> Emptynumber{0};
char selection{};
do{
cin >> selection;
if(selection == 'p' || selection == 'P')
{
if (number.size() == 0)
cout << "[] - The list is Empty" << '\n';
else{
cout << " [ ";
for(int num:number)
cout << num << " ";
cout << " ] " << '\n';
}
}else if(selection == 'a' || selection == 'A')
{
int num_to_add{};
cout << "Enter the number to add: ";
cin >> num_to_add;
number.push_back(num_to_add);
cout << num_to_add << " Successfully Added" << '\n';
}else if(selection == 'm' || selection == 'M')
{
if (number.size() == 0)
cout << "[] - The list is Empty, unable to calculate" << '\n';
else{
double total{};
for(double num:number)
total += num;
cout << "The middle number is: " <<static_cast<int>(total)/number.size() << '\n';
}
}else if(selection == 's' || selection == 'S')
{
if (number.size() == 0)
cout << "[] - The list is Empty, unable to retrieve smaller value" << '\n';
else{
int smallest = number.at(0);
for(int num:number)
if(num < smallest)
smallest = num;
cout << "The smallest number is: " << smallest << '\n';
}
}else if(selection == 'l' || selection == 'L')
{
if (number.size() == 0)
cout << "[] - The list is Empty, unable to retrieve larger value" << '\n';
else{
int larger = number.at(0);
for(int num:number)
if(num > larger)
larger = num;
cout << "The larger number is: " << larger << '\n';
}
}else if(selection == 'C' || selection == 'c')
{
if (number.size() == 0)
cout << "[] - The list is Already Empty, Can't be Cleared" << '\n';
else{
number.push_back(0);
number.clear();
cout << " Successfully Cleared" << '\n';
cout << " The list is Empty " << '\n';
}
}
}while (selection != 'q' && selection != 'Q');
return;
}
推荐阅读
- ansible - 尝试在 Ansible 剧本中使用“selectattr in”过滤器,但按照下面的剧本失败
- c# - 如何在 Tizen Xamarin 上播放声音
- python - 我的循环卡在无限循环中我觉得没有错误请证明我错了吗?
- angular - 在angular2中按类颜色排序
- json - 折叠谷歌表格中一定深度的所有组
- docx4j - 查找并替换 Content Control-docx4j 中的内容
- java - Java排序算法问题
- java - 如何获取数据列表,特定日期仅从每个日期获取 6 条记录而不是更多
- sql - SQL:根据主键查询完整的层次结构
- javascript - 将像素值转换为抽象值,如中心、底部等