2016-11-05 151 views
1

我一直在爲我的大學課程設計一個密碼生成器,其中一個部分涉及到創建'複雜'密碼,這些密碼只不過是隨機字符串的密碼,用戶應該能夠指定使用什麼類型的字符。但是,控制函數是否使用的if語句集不會基於uppertrue numbertrue和lowertrue中的值進行激活,它們的作用就像語句返回true一樣,因此函數始終運行。 的#include 的#include 的#include 的#include代碼忽略if語句 - C++

int upper(), lower(), number(), symbol(); //initializing functions to be used to generate the ascii code 
int clength = 15; 
int pass[30]; 
int uppertrue = 0, numbertrue = 1, symboltrue = 0; 
int main() 
{ 
    srand (time(NULL));              //seed random generator 
    int i = 0;                //counter 
    int which = 0; 
    do 
    { 
     which = rand() % 4 + 1;  //randomly decides which type of character will be shown - probablity is unweighted for complex module 
     if (which == 1) 
     { 
      pass[i] = lower();  //inserts the code returned by the function into the array 
      i++; 
     } 
     else if ((uppertrue == 1) && (which == 2)) 
     { 
      pass[i] = upper(); 
      i++; 
     } 
     else if (numbertrue == 1 && which == 3) 
     { 
      pass[i] = number(); 
      i++; 
     } 
     else if (symboltrue == 1 && which == 4) 
     { 
      pass[i] = symbol(); 
      i++; 
     } 
    }while (i!=(clength+1));  //terminates loop when the array is complete 
    std::string strpass; 
    int x=0; 
    do 
    { 
     char tempchar; 
     tempchar = pass[x]; 
     std::cout << tempchar; 
     x++; 
    }while (x!=15); 
    return 0; 
} 

int upper()  //creates random number between the range of ascii characters that results in caps 
{ 
    return rand() % 65 + 26; 

} 

int number() //same as upper but for numbers 
{ 
    return rand() % 48 + 9; 
} 

int lower()  //same as upper but for lower case 
{ 
    return rand() % 122 + 26; 
} 

int symbol() //same as upper but for symbols (currently only supporting a few characters 
{ 
    return rand() % 63 + 6; 
} 

如果有人能在正確的方向,將不勝感激指向我,好像它是一個邏輯上的錯誤,但我看不出什麼錯邏輯用它。這可能與C++的某種怪癖有關嗎? (記住我被教過C,這是我在C++中做的第一件事) 非常感謝 (評論說刪除部分,我通常會輸入uppertrue等的值,所以我硬編碼值顯示,而不是問題)

+0

請稍微減少問題。對於初學者來說,擺脫'std :: cin'調用並將其替換爲硬編碼輸入以儘可能簡潔地展示問題。見http://stackoverflow.com/help/mcve –

+0

這不是你問的問題,但你需要做'rand()%26 + 65'和'rand()%26 + 97'(不是122)。你可以用'rand()%('Z' - 'A'+ 1)+'A''和'rand()%('z' - 'a'+ 1)+'a' '' –

+0

除了(不正確的)幻數之外,'lower()','upper()','number()'和'symbol()'的代碼是不可移植的。 'number()'很容易實現:return rand()%10 +'0';'。其他的應該用一個'char'實現:'char lc [] =「abcdefghijklmnopqrstuvwxyz」;'然後'return lc [rand()%26];'。 –

回答

2

你的問題是在這裏:

int lower()  // same as upper but for lower case 
{ 
    return rand() % 122 + 26; 
} 

它會產生隨機數的範圍26 147.這是東西比範圍完全不同的小寫字符。您需要:

return rand() % ('z' - 'a' + 1) + 'a'; 

您應該以類似的方式修復其他功能。

請注意那些擔心他們的代碼能夠運行的人,例如使用EBCDIC字符編碼的大型機:這裏假設a..z有連續的字符代碼。

+0

非常感謝,我正在尋找完全錯誤的地方,試圖找到修復 –

+0

道歉,最初downvoting這一點。只要OP意識到可移植性限制,您確實放棄了*編碼*,這是一個很好的答案。 – Bathsheba

1

特定問題是,您在隨機返回各種字符的函數中存在錯誤。

對於與字符關聯的數值,C++標準故意含糊不清。精確的映射低至實現,並且該方案被稱爲編碼

儘管ASCII編碼很常見,但它絕不是通用的,所以爲了實現的可移植性除非您確實需要,否則最好不要對您的平臺進行假設。

所以,你真的應該對線路重鑄lower

char lower 
{ 
    const char* s = "abcdefghijklmnopqrstuvwxyz"; 
    return s[rand() % 26]; 
} 

這是真正的便攜。我也冒着改變你的函數返回類型的自由。

你應該做類似的upper。你的symbols函數將會同樣退出。

我會忍不住採取了number同樣的方法太多,但這裏的C++標準說一下數字:在編碼必須安排字符09是在一個連續的塊並按照這個順序,這樣的聲明

return rand() % ('9' - '0' + 1) + '0';

便攜。作爲最後一句話,你可以使用代替硬編碼26使用static char[] s = "abc...z";(sizeof(s) - 1)。這是一種相當先進的技術,對於初學者來說不是很明顯,但是隨着編程技能的發展對它進行研究。

+0

這種方法是'symbol()'函數唯一明智的選擇,特別是。 – hyde