2015-03-02 112 views
-1

我有點兒是C++的新手......有一個月左右的時間......任何人,我們正在做這個最後的項目,我們必須做一個加密/解密程序使用希爾密碼方法。我遇到了一個小問題,當我試圖從函數中返回數字時,它會顯示符號而不是實際的數字。但是,該函數本身正常工作。儘管這是作業/課堂作業,但老師允許我們使用在線資源和外部幫助,因爲正如我之前所說的,我們只編程了一個月左右。任何和所有的幫助將不勝感激!如何將字符串中的字母轉換爲數字 - C++

#define NOMINMAX 
#include <iostream> 
#include <stdlib.h> 
#include <time.h> 
#include <stdio.h> 
#include <windows.h> 
#include <Windows.h> 
#include <chrono> 
#include <string> 
#include <fstream> 
#include <iomanip> 

using namespace std; 

int random1() 
{ 
    unsigned long long int xRan; 
    srand(time(NULL)); 
again: 
    xRan = rand() * 319/43 * 16 * 333 % 9999 + 1; 
    if (xRan <= 0) 
     goto again; 
    return xRan; 
} 
int random2() 
{ 
    unsigned long long int xRan; 
    srand(time(NULL)); 
again: 
    xRan = rand() * 319 * 43/16 * 321 % 9999 + 1; 
    if (xRan <= 0) 
     goto again; 
    return xRan; 
} 
int random3() 
{ 
    unsigned long long int xRan; 
    srand(time(NULL)); 
again: 
    xRan = rand() * 319/43/16 * 2 % 9999 + 1; 
    if (xRan <= 0) 
     goto again; 
    return xRan; 
} 

int convert(char conv) 
{ 
    return (int)conv-87; 
} 

int matrixMult(double q, double w, double e) 
{ 
    int i, j, k, count = 0, total = 0; 

    double a, s, z, x, c, v, b, n, m; 

    a = random1(); 
    s = random2(); 
    z = random3(); 
    x = random1(); 
    c = random2(); 
    v = random3(); 
    b = random1(); 
    n = random2(); 
    m = random3(); 

    int A[1][3] = { q, w, e }; 

    int B[3][3] = { 
     { a, s, z }, 
     { x, c, v }, 
     { b, n, m } 
    }; 

    for (i = 0; i < 3; ++i) 
    { 
     for (j = 0; j < 3; ++j) 
     { 
      for (k = 0; k < 3; ++k) 
       total += A[i][k] * B[k][j]; 
      cout << setw(5) << total << ' '; 
      ++count; 
      if (count % 3 == 0) 
       cout << endl; 
      total = 0; 
     } 
    } 


    return 0; 
} 

int main() 
{ 
    string again = "y"; 

    cout << "Would you like to encrypt or decrypt?(e/d) " << endl; 
    string ende; 
    cin >> ende; 



    if (ende == "e") 
    { 
     cout << "Please enter a name for the message: " << endl; 
     string file; 
     cin >> file; 

     file += ".txt"; 
     ofstream encrypt; 
     encrypt.open(file); 


     string message; 
     cout << "Please enter the message you would like to encrypt: " << endl << endl; 
     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
     getline(cin, message); 

     //int length = message.size(); 

     char converted[9999]; 

     for (int i = 0; i < message.length(); ++i) 
     { 
      converted[i] = convert(message[i]); 
      cout << converted[i] << endl; 
     } 





     encrypt.close(); 

     cout << "Your message has been encrypted.\nPlease check " << file << " for the encrypted message and key" << endl; 
    } 
    if (ende == "d") 
    { 
     cout << "0"; 

    } 






    return 0; 
} 
+2

只是'int轉換[9999];'。你把它聲明爲char,所以它會打印字符 – ForceBru 2015-03-02 14:22:29

+0

那些gotos可以被do-while循環取代 – ryanpattison 2015-03-02 14:28:56

+0

OMG gotos !!! 1! – chmike 2015-03-02 14:30:41

回答

1
int converted[9999]; 
for (int i = 0; i < message.length(); ++i){ 
    converted[i] = convert(message[i]); 
    cout << converted[i] << endl; 
} 

您已經聲明convertedchar當你需要打印出來的數字。你最好宣佈它爲int或使用cout << int(converted[i]) << endl;

順便說一下,使用endl可以使程序有點慢。您可以改用"\n"

+0

你需要'endl'的唯一時間就在你要求輸入之前。 – 2015-03-02 14:47:36

+0

@MarkRansom,通常甚至不會......輸入和輸出流是_tied_,這意味着輸入操作將首先刷新輸出緩衝區。見例如http://en.cppreference.com/w/cpp/io/basic_ios/tie – Andrew 2015-03-02 14:59:27

相關問題