2012-11-16 185 views
0

我在修復此警告消息時遇到問題。未簽名/簽名不匹配

警告C4018:「<」:符號/無符號不匹配

誰能幫我找出問題是什麼?它坐落在布爾函數在while (i < bin.length())

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

查找'std :: string :: length'的定義。選擇「我」的類型,以便它適合。 – Zane

回答

3

好,iint,而bin.length()unsigned,因此不匹配。要刪除警告,請使我unsigned

+2

我會說'bin.length()'的類型是'size_t'。 – Zane

+0

我如何讓我無符號?我需要我作爲一個整數,所以我可以繼續添加它的整數來檢查字符串中的每個內存空間,看看它們是1還是0 – user1793565

+1

'unsigned int i = 0;'' – matthew3r