【问题标题】:How to clear the previous input in c++?如何清除c++中的先前输入?
【发布时间】:2015-09-27 05:18:26
【问题描述】:

'因为我的程序将先前的输入连接到我的当前输入。我想清除以前的输入。

这是代码:

#include <cstdlib>
#include <iostream>
#include <math.h>
#include <conio.h>

using namespace std;

int main()
{

   int ini=1,an=1,ans=1, f=1;
   int bb=2;  
   int hypin;


   string a="a.) Hyperfactorial";
   string b="b.) Superfactorial";
   string c="c.) Primorials";
   string d="Exit";
   string e="a. Yes";
   string ff="b. No";
   string letter;
   string ysno;
   start:
   cout<<"\n"<<"Factorial"<<"\n"<<a<<"\n"<<b<<"\n"<<c<<endl;
   cout<<"\n"<<"Enter letter:"<<endl;
   cin>>letter;


   if (letter=="a"){
     cout<<"\n"<<"Enter number (maximum input:7) : "<<endl;
     cin>>hypin;   

     if(hypin>=8){
       cout<<"\n"<<"Invalid input!"<<endl;
     }else{
       while (hypin>1){
         ini=ini*(pow(hypin,hypin));
         hypin--;
       }
       cout<<"\n"<<"The hyperfactorial is: "<<ini<<endl;}

       cout<<"\n"<<"Do you want to test another factorial?"<<"\n"<<e<<"\n"<<ff<<endl;
       cout<<"\n"<<"Answer: ";
       cin>>ysno;                
       if(ysno=="a"){
         goto start;
       }
       if(ysno=="b"){
         cout<<"\n"<<"Press any key to exit"<<"\n"<<endl;
         getch();
         return 0;
       }
     }

     system("PAUSE");
     return EXIT_SUCCESS;
}

【问题讨论】:

  • 请清除之前输入的命令
  • 哪种语言? C C++? C C++?仅限 C++?
  • c++ 仅 juanchopanza
  • 那么为什么是 C 标签呢?我会删除它。
  • goto,无意义的变量名称,零输入错误检查。你真的想让自己很难受,不是吗?

标签: c++ input


【解决方案1】:

尝试使用

清除 cin 缓冲区
std::cin.ignore(INT_MAX);

编辑:您必须 #include &lt;limits.h&gt; 才能使用 INT_MAX

【讨论】:

  • 仍然没有任何反应
  • @A.Swift 不起作用,因为忽略了忽略的结束条件之一。执行 Pantss 建议的规范方法是 cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n'); 如果错误输入足够糟糕,您可能还需要 cin.clear() 删除 cin 上的错误标志。
【解决方案2】:

你不需要清除任何东西。事实上,您的代码中有一些小错误,并不是每次都设置答案。
我试着为你写一个简单的代码:

bool validInput = false ,continueFlag = true;
string opr, ysno;
int a , b, ans = 0;


while(continueFlag)
{
    cout << "Choose operation: a. Multiply b. Add \n Enter letter: " << endl;
    cin >> opr;
    if (opr != "a" && opr != "b")
    {
        cout << "Invalid input!"<<endl;
        continue; // it returns to "while(continueFlag)" line
    }
    cout << "Enter First Number: ";
    cin >> a;
    cout << "Enter Second Number: ";
    cin >> b;
    if (opr == "a")
    {
        ans = a * b;
    }
    else
    {
        ans = a + b;
    }

    cout << "Answer is : "<< ans << endl;
    do
    {
        cout << "Do you want to test another factorial? a. Yes b. No" << endl;
        cin >> ysno;
        if (opr != "a" && opr != "b")
        {
            cout << "Invalid input!"<<endl;             
            continue; // it returns to "do" line
        }
        validInput = true;
        if (ysno == "b")
        {
            continueFlag = false;
        }
    }while(!validInput);
}

【讨论】:

    【解决方案3】:

    试试这个可能会有帮助

    cout

    我使用了这段代码并用 g++ 编译了它的工作和 linux platfrom(ubuntu)

    #include <cstdlib>
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    int main()
    {
    
       int ini=1,an=1,ans=1, f=1;
       int bb=2;
       int hypin;
    
    
        string a="a.) Hyperfactorial";
        string b="b.) Superfactorial";
        string c="c.) Primorials";
        string d="Exit";
        string e="a. Yes";
        string ff="b. No";
        string letter;
        string ysno;
        start:
        cout<<"\n"<<"Factorial"<<"\n"<<a<<"\n"<<b<<"\n"<<c<<endl;
        cout<<"\n"<<"Enter letter:"<<endl;
        cin>>letter;
    
        if (letter=="a"){
        cout<<"\n"<<"Enter number (maximum input:7) : "<<endl;
        cin>>hypin;
    cout << "\033[2J\033[1;1H";
        if(hypin>=8){
         cout<<"\n"<<"Invalid input!"<<endl;
                     }else{
           while (hypin>1){
           ini=ini*(pow(hypin,hypin));
           hypin--;
        }
           cout<<"\n"<<"The hyperfactorial is: "<<ini<<endl;}
      cout<<"\n"<<"Do you want to test another factorial?"<<"\n"<<e<<"\n"<<ff<<endl;
           cout<<"\n"<<"Answer: ";
           cin>>ysno;
           if(ysno=="a"){
                         goto start;
                         }
                         if(ysno=="b"){
            cout<<"\n"<<"Press any key to exit"<<"\n"<<endl;
               return 0;
                      } }
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-14
    • 2017-02-08
    • 1970-01-01
    • 2010-11-06
    • 2011-12-15
    相关资源
    最近更新 更多