2014-01-30 27 views
-1

我正在編寫一個程序,它將根據兩個公式計算密碼強度。它要求用戶輸入2個密碼,一個是8個字符或更少,另一個是20個字符或更多。第一部分執行出問題。但是,當我執行第二部分時,它會起作用,但提取接下來的十二個字符的子字符串函數會一直提取18個字符。我多次查看過我的代碼,不明白爲什麼會這樣做。任何幫助將不勝感激!C++中的子串錯誤

#include <iostream> 
#include <cmath> 
#include <string> 
#include <iomanip> 
using namespace std; 

int main() 
{ 
//All variables and constants are declared 
string eight_password, first_char, next_seven, twenty_password, first_char_twenty, next_seven_twenty, next_twelve, remaining; 
int ep_length, character_set, first_char_length, next_seven_length, character_set_20, twenty_length, first_char_twenty_length, next_seven_twenty_length, next_twelve_length, remaining_length; 
double eight_ent_strength, eight_nist_strength, twenty_ent_strength, twenty_nist_strength; 
const int NIST_FIRST = 4, NIST_SEVEN = 2, NIST_REM = 1, NIST_CHARACTER=94, NIST_BONUS=6; 
const double NIST_TWELVE = 1.5; 
//Console prompts for user to input password and character set 
cout << "Hello! Please enter a password of 8 characters or less (including spaces!):" << endl; 
getline(cin, eight_password); 
cout << "What character set is being used?"; 
cin >> character_set; 
cout << endl; 
//Password length and information entropy strength are calculated and saved to the appropriate variables 
ep_length = eight_password.length(); 
eight_ent_strength = (ep_length*((log(character_set))/(log(2)))); 
//First character and next seven characters are extracted and saved to the appropriate variables 
first_char = eight_password.substr(0, 1); 
next_seven = eight_password.substr(1, 7); 
//First character and next seven characters lengths are calculated and saved to the appropriate variables 
first_char_length = first_char.length(); 
next_seven_length = next_seven.length(); 
//NIST strength is calculated and saved to the appropriate variable 
eight_nist_strength = (first_char_length*NIST_FIRST) + (next_seven_length*NIST_SEVEN)+((character_set/NIST_CHARACTER)*NIST_BONUS); 

//The information that was calculated is now printed back out on the console to be viewed by the user 
cout << "Your password " << eight_password << " is " << ep_length << " characters long. According to the information " << endl; 
cout<<"entropy formula, it has a strength of " << eight_ent_strength << "." << endl; 
cout << "The first character is \"" << first_char << "\" and the next seven characters are \"" << next_seven << "\". " << endl; 
cout << "According to the NIST formula, it has a strength of " << eight_nist_strength << "." << endl << endl; 

cin.ignore(); 

cout << "Now, please enter a password of 20 characters (including spaces!):" << endl; 
getline(cin, twenty_password); 
cout << "What character set is being used?"; 
cin >> character_set_20; 
cout << endl; 

twenty_length = twenty_password.length(); 

twenty_ent_strength = (twenty_length*((log(character_set_20))/(log(2)))); 

first_char_twenty = twenty_password.substr(0, 1); 
next_seven_twenty = twenty_password.substr(1, 7); 
next_twelve = twenty_password.substr(8, 19); 
remaining = twenty_password.substr(19); 

first_char_twenty_length = first_char_twenty.length(); 
next_seven_twenty_length = next_seven_twenty.length(); 
next_twelve_length = next_twelve.length(); 
remaining_length = remaining.length(); 

twenty_nist_strength = (first_char_twenty_length*NIST_FIRST) + (next_seven_twenty_length*NIST_SEVEN) + (next_twelve_length*NIST_TWELVE) + (remaining_length*NIST_REM) + ((character_set_20/NIST_CHARACTER)*NIST_BONUS); 

cout << "Your password " << twenty_password << " has a length of " << twenty_length << "." << endl; 
cout << "According to the information entropy formula, it has a strength of " << twenty_ent_strength << endl; 
cout << "The first character is \"" << first_char_twenty << "\"." << endl; 
cout<< "The next seven characters are \"" << next_seven_twenty <<"\"."<< endl; 
cout << "The next twelve characters are \"" << next_twelve << "\"." << endl; 
cout<< "The remaining characters are \"" << remaining << "\"." << endl; 
cout << "According to the NIST formula, it has a strength of " << twenty_nist_strength <<"." << endl; 
return 0; 
} 
+0

也許從編寫實際易讀的代碼開始。然後,通過構建[testcase](http://sscce.org)來解決問題。 –

回答

0

next_twelve = twenty_password.substr(8, 19);將提取19個字符

next_twelve = twenty_password.substr(8, 12);將提取12個字符

第一參數是開始位置。第二個參數是長度。

string substr (size_t pos = 0, size_t len = npos) const; 
+0

但是爲什麼呢?我只想要它提取12個字符。 – user3064203

+0

coz'next_seven = eight_password.substr(1,7);'提取7個字符 –

+1

解釋爲什麼這會通過編輯答案。 –