2017-04-20 43 views
0

我有這樣的C代碼C代碼不工作

#include <stdio.h> 
int main() { 
    char Username[100]; 
    char Password[100]; 
    printf("discl0sed 0.1 discl0sed tty1\ndiscl0sed login: "); 
    scanf("%s", Username); 
    printf("Password: "); 
    scanf("%s", Password); 
    if (Username == "root" && Password == "!DISCL0SEDor1=1--") 
    { 
     return 0; 
    } 
    else 
    { 
     while (1 == 1) 
     { 
      printf("Login incorrect\ndiscl0sed login: "); 
      scanf("%s", Username); 
      printf("Password: "); 
      scanf("%s", Password); 
      printf("%s:%s", Username, Password); 
      if (Username == "root" && Password == "!DISCL0SEDor1=1--") 
      { 
       break; 
      } 
      else 
      { 
       continue; 
      } 
     } 
    } 
} 

這反映在Linux中tty外殼。 問題是,當我使用正確的憑據,它說「不正確的登錄」。

爲什麼會發生這種情況?

+4

查找'strcmp' - Google it! –

+0

當查看手冊頁 - 閱讀有關'scanf' –

+1

請打開您的編譯器警告! –

回答

2

我希望這會幫助,

在C中,你必須使用STRCMP或STRNCMP字符串比較。它就像Java中的String.Equal一樣。

如果您正在比較int堆棧內存中的哪一個是可以的,因爲它被寫入(在內存中)本身,但是如果該變量在分配的內存或數組中,則每個元素都具有分配內容的地址記憶。由於內容的兩個地址不同,所以必須使用函數來比較內容本身。

祝你好運!

+4

C和C++是不同的語言。 C++會使用'std :: string' –

+0

在C中你可以使用'str * cmp()',但在'C++'中你可以使用'std :: string :: compare'。 – alvits

+0

@jenesaisquoi OMG ..你真的是對的!那是很久以前的事了。 –