如何在c ++中清除以前的输入?

时间:2015-09-27 05:18:27

标签: c++ input clear

'因为我的程序将以前的输入加入到我当前的输入中。我想清除以前的输入。

这是代码:

#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;
}

3 个答案:

答案 0 :(得分:1)

尝试使用

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

编辑:您必须#include <limits.h>才能使用INT_MAX

答案 1 :(得分:1)

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

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);
}

答案 2 :(得分:-1)

尝试这可能会有所帮助

cout&lt;&lt; &#34; \ 033 [2J \ 033 [-1; 1 H&#34 ;;

我使用了这段代码并用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;
}