2015-03-02 274 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

相关问题