C++ C++;使用结构数组的消息编码器

C++ C++;使用结构数组的消息编码器,c++,arrays,structure,C++,Arrays,Structure,我编写了一个程序,从文本文件中读取两列字母,并使用这些字母对从键盘输入的信息进行编码和解码。我有程序编译,但它只是给我奇怪的白色条,而不是一个编码的消息。我认为我已经正确地编写了void函数,但是对于如何一次读取用户输入消息中的每个字符,我有点困惑。我将向您提供以下代码和说明: 使用结构数组对消息进行编码和解码。该结构将有两个元素:输入字符和输出字符。使用此数组,您的程序将对消息进行编码和解码 您的程序应具有以下功能: loadArray - this function will read a

我编写了一个程序,从文本文件中读取两列字母,并使用这些字母对从键盘输入的信息进行编码和解码。我有程序编译,但它只是给我奇怪的白色条,而不是一个编码的消息。我认为我已经正确地编写了void函数,但是对于如何一次读取用户输入消息中的每个字符,我有点困惑。我将向您提供以下代码和说明:

使用结构数组对消息进行编码和解码。该结构将有两个元素:输入字符和输出字符。使用此数组,您的程序将对消息进行编码和解码

您的程序应具有以下功能:

loadArray - this function will read a file containing 26 character pairs.  These pairs will be loaded into an array of 26 rows.  The first value of the pair is the input character and the second is the output character.
encode - this function will take 2 parameters, the array and a single character.  It will return the output character associated with the parameter character.  If no match is found the original character is returned.
decode - this function will be the inverse of the encode function and will match the character parameter with the output character in the array and return the input character, or the original character if no match is found
在main函数中,加载数组后,程序将提示用户输入消息字符串。然后,它将通过为消息中的每个字符调用encode函数并将其替换为编码值来对消息进行编码

要验证结果,请使用已编码的消息并对其进行解码,以确保返回原始消息

#include<iostream>
#include<string>
#include<fstream>
#include<array>
#include<iomanip>
#include<stdio.h>
#include<sstream>
using namespace std;
struct code
{
    char input;
    char output;
};



void loadArray(code[]);
void encode(code[], char);
void decode(code[], char);

int main()
{
    code input[26];
    string message;
    char output;


    loadArray;

    // prompt the user for the message
    cout << "Enter your message" << endl;
    cin >> message;


    for (char& c : message) {
        encode(input, c);
    }



    return 0;
}


void loadArray(code c[])
{
    ifstream in("codeFile.txt");

    int i;

    for (int i = 0; i <= 26; i++){
        in >> c[i].input >> c[i].output;

    }


    }

void encode(code c[], char in)
{
    int encMessage;
    int i;
    for (i = 0; i <= 26; i++){
        if (c[i].input == in){
            cout << c[i].output;
            }
    }
}

void decode(code c[], char out)
{
    int decMessage;
    int i;
    for (i = 0; i <= 26; i++){
        if (c[i].output == out){
            cout << c[i].input;
        }
    }
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
结构代码
{
字符输入;
字符输出;
};
void loadArray(代码[]);
无效编码(代码[],字符);
无效解码(代码[],字符);
int main()
{
代码输入[26];
字符串消息;
字符输出;
加载阵列;
//提示用户输入消息
cout消息;
for(字符和字符:消息){
编码(输入,c);
}
返回0;
}
void loadArray(代码c[])
{
ifstream in(“codeFile.txt”);
int i;
对于(int i=0;i>c[i]。输入>>c[i]。输出;
}
}
无效编码(代码c[],字符输入)
{
int-encMessage;
int i;

对于(i=0;i一些进一步的提示:

    <> LI>尝试调用 LooStult为<代码> LooStad;——在C++中需要括号调用函数:应该是代码>加载数组(输入);< /代码>
  • 您的函数encode和decode是
    void
    。这是错误的。它们应该首先根据需要返回编码(resp decoded)字符(它将返回…),然后构建编码字符串

if(c[i].input=in)
最有可能的意思是
if(c[i].input==in)参阅你喜欢的C++教科书,关于分配和相等比较操作符的区别。完全忽略了键入哈哈。谢谢,但是它仍然不会输出编码的消息。我曾经想过如果我做了CUT,我就看不到代码>输出< /代码>,这是一个值(也不是代码>输入<代码>)。。你正在打印随机垃圾。你是想在某个时候调用
loadArray
吗?我现在没有打印任何东西,我添加了load array,但我正在研究如何调用输入函数。我丢失了以前的所有项目和文件,因此没有任何参考资料。我的书一文不值。我已经通读了一遍h plus它不包括结构数组。我还没有给输出函数赋值,因为我需要先让输入函数工作。“我现在没有打印任何东西”我可以发誓我看到
cout
loadArray()
不会编译-函数被声明为接受一个参数。@IgorTandetnik:谢谢修复它。