Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无符号/有符号不匹配_C++_Visual C++ - Fatal编程技术网

C++ 无符号/有符号不匹配

C++ 无符号/有符号不匹配,c++,visual-c++,C++,Visual C++,我无法修复此警告消息 警告C4018:“嗯,i作为int,而bin.length()是无符号的,因此不匹配。要删除警告,请将i设为无符号,i设为int,bin.length()设为无符号,因此不匹配。要删除警告,请让我unsigned查找std::string::length的定义。选择适合的i类型。查找std::string::length的定义。选择适合的i类型。我想说bin.length()的类型是size\u t。如何使我不签名?我需要I作为一个int,这样我就可以继续为它添加整数,以检

我无法修复此警告消息


警告C4018:“嗯,
i
作为
int
,而
bin.length()
无符号的,因此不匹配。要删除警告,请将i
设为无符号
i
设为
int
bin.length()
设为
无符号
,因此不匹配。要删除警告,请让我
unsigned

查找
std::string::length
的定义。选择适合的
i
类型。查找
std::string::length
的定义。选择适合的
i
类型。我想说
bin.length()
的类型是
size\u t
。如何使我不签名?我需要I作为一个int,这样我就可以继续为它添加整数,以检查字符串中的每个内存空间,看看它们是1还是0。我想说
bin.length()的类型是
size\t
。如何使I无符号?我需要I作为一个int,这样我就可以继续为它添加整数,以检查字符串中的每个内存空间,看看它们是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;
                    cin.clear();

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