0

我在检查来自用户的无效二进制输入时遇到问题。这是程序。谁能告诉我如何解决这个问题?

这些是我得到的错误

  1. 错误 C2660:“binToDec”:函数不接受 1 个参数
  2. 警告 C4018:“<”:有符号/无符号不匹配
  3. 错误 C2082:重新定义形式参数“bin”
  4. 错误 C2275:“std::string”:非法将此类型用作表达式。见'std::string'的声明
  5. 错误 C2082:重新定义形式参数“bin”

    #include<iostream>
    #include<string>
    #include<math.h>
    
    using namespace std;
    
    void intro();
    bool isBinary(string);
    void decToBin();
    string getBin();
    void binToDec();
    char getChoice();
    char getContinue();
    
    int main()
    {
        char choice, cont;
        string bin;
    
        intro();
    
        do{
            choice = getChoice();
                if(choice == 'b' || choice == 'B')
                {
                    bin = getBin();
                    bool binIsBinary = isBinary(bin);
                    if(binIsBinary)
                        binToDec(bin);
                    else
                    {
                        cout<<"Error!!! Your Number is Not Binary"<<endl;
                        cout<<endl;
                    }
                }
    
                if(choice == 'd' || choice == 'B')
                    decToBin();
            cont = getContinue();
          }
        while(cont == 'y' || cont == 'Y');
    }
    
    
    
    void intro()
    { 
        cout << "This program coverts decimal numbers to binary and vice versa.
                 << endl;
    }
    
    bool isBinary( string bin )
    {
       int i=0;
       bool binIsBinary = true;
    
       while (i < bin.length())
       {
          if( bin.at(i) != '1' && bin.at(i) != '0' )
              {
              binIsBinary = false;
              }
              i++;
       }
    
       return binIsBinary;
    }
    
    void decToBin()
    {
       int dec;
       string bin;
    
       cout << endl << "Please enter a decimal number:"; 
       cin  >>  dec;
       bin = "";
    
       while (dec != 0)
       {
          if (dec % 2 == 0)
             bin.insert(0, "0");
          else
             bin.insert(0, "1");
          dec = dec / 2;
       }
       cout << "The equivalent binary number is: " << bin << endl << endl;
    
    }
    
    string getBin(string bin)
    {   
        string bin;
    
        cout << endl << "Enter a binary number: ";
        cin  >>  bin;
    
        return string;
    }
    
    void binToDec(string bin)
    {
        double deci;
        string bin;
        double len;
    
       len = bin.length();
    
       deci = 0;
       for (int i=0; i<len; i++)
           if (bin.at(i) == '1')
               deci = deci + pow(2, len-i-1);
    
       cout << "The equivalent decimal number is: " << deci << endl    
            << endl;
    }
    
    char getChoice()
    {
        char choice;
    
        cout << endl << "If you would like to convert a binary to a decimal then enter b."<<endl; 
        cout << "If you would like to convert a decimal to a binary then enter d. ";
        cin >> choice;
    
        return choice;
    }
    
    char getContinue()
    {
        char cont;
    
        cout << "Would you like to convert another number(Y/N)? ";
    
        cin >> cont;
    
        return cont;
    }
    
4

1 回答 1

0

我已经纠正了您代码中的问题,但没有检查十进制到二进制和二进制到十进制的转换是否正确。问题主要是因为你错误的函数定义和函数中错误传递的参数。这是修改后的代码:

#include<iostream>
#include<string>
#include<math.h>

using namespace std;

void intro();
bool isBinary(string);
void decToBin();
string getBin();
void binToDec(string);
char getChoice();
char getContinue();

int main()
{
    char choice, cont;
    string bin;

    intro();

    do{
        choice = getChoice();
            if(choice == 'b' || choice == 'B')
            {
                bin = getBin();
                bool binIsBinary = isBinary(bin);
                if(binIsBinary)
                    binToDec(bin);
                else
                {
                    cout<<"Error!!! Your Number is Not Binary"<<endl;
                    cout<<endl;
                }
            }

            if(choice == 'd' || choice == 'B')
                decToBin();
        cont = getContinue();
      }
    while(cont == 'y' || cont == 'Y');
}



void intro()
{ 
    cout << "This program coverts decimal numbers to binary and vice versa." << endl;
}

bool isBinary( string bin )
{
   int i=0;
   bool binIsBinary = true;

   while (i < bin.length())
   {
      if( bin.at(i) != '1' && bin.at(i) != '0' )
          {
          binIsBinary = false;
          }
          i++;
   }

   return binIsBinary;
}

void decToBin()
{
   int dec;
   string bin;

   cout << endl << "Please enter a decimal number:"; 
   cin  >>  dec;
   bin = "";

   while (dec != 0)
   {
      if (dec % 2 == 0)
         bin.insert(0, "0");
      else
         bin.insert(0, "1");
      dec = dec / 2;
   }
   cout << "The equivalent binary number is: " << bin << endl << endl;

}

string getBin()
{   
    string bin;

    cout << endl << "Enter a binary number: ";
    cin  >>  bin;

    return bin;
}

void binToDec(string bin)
{
    double deci;
    double len;

   len = bin.length();

   deci = 0;
   for (int i=0; i<len; i++)
       if (bin.at(i) == '1')
           deci = deci + pow(2, len-i-1);

   cout << "The equivalent decimal number is: " << deci << endl    
        << endl;
}

char getChoice()
{
    char choice;

    cout << endl << "If you would like to convert a binary to a decimal then enter b."<<endl; 
    cout << "If you would like to convert a decimal to a binary then enter d. ";
    cin >> choice;

    return choice;
}

char getContinue()
{
    char cont;

    cout << "Would you like to convert another number(Y/N)? ";

    cin >> cont;

    return cont;
}
于 2012-11-16T10:38:07.413 回答