我有点像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;
}
答案 0 :(得分:1)
int converted[9999];
for (int i = 0; i < message.length(); ++i){
converted[i] = convert(message[i]);
cout << converted[i] << endl;
}
当您需要打印数字时,您已将converted
声明为char
。您最好将其声明为int
或使用cout << int(converted[i]) << endl;
顺便说一句,使用endl
会使程序有点慢。您可以改用"\n"
。