我在搞乱文件输入/输出,我正在尝试制作一个编码/解码程序。我需要帮助将文件中的编码字符读回要解码的程序。(这不是一个作业,因为我明年才上 9 年级,我正在尝试这样做,因为它看起来很有挑战性。)这是我的代码:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <cstdlib>
/*
* OUT: categorize words into char's, assign a symbol/number to each char, output number
* combonation to file.
IN: load file, decode file, read decoded version. */
using namespace std;
void Encode(){
char message[100];
char ENCODED[100];
cout<<"input new content:\n>";
cin.getline(message, 99);
cin.ignore();
cout<<"encoding...\n";
for (int i=0; i<100; ++i){
if (message[i]=='a') ENCODED[i]='1';
else if (message[i]=='b') ENCODED[i]='$';
else if (message[i]=='c') ENCODED[i]='!';
else if (message[i]=='d') ENCODED[i]='*';
else if (message[i]=='e') ENCODED[i]='2';
else if (message[i]=='f') ENCODED[i]='&';
else if (message[i]=='g') ENCODED[i]='^';
else if (message[i]=='h') ENCODED[i]='%';
else if (message[i]=='i') ENCODED[i]='3';
else if (message[i]=='j') ENCODED[i]='=';
else if (message[i]=='k') ENCODED[i]='_';
else if (message[i]=='l') ENCODED[i]='-';
else if (message[i]=='m') ENCODED[i]='2';
else if (message[i]=='n') ENCODED[i]='9';
else if (message[i]=='o') ENCODED[i]='4';
else if (message[i]=='p') ENCODED[i]='|';
else if (message[i]=='q') ENCODED[i]='/';
else if (message[i]=='r') ENCODED[i]='>';
else if (message[i]=='s') ENCODED[i]='?';
else if (message[i]=='t') ENCODED[i]='}';
else if (message[i]=='u') ENCODED[i]='5';
else if (message[i]=='v') ENCODED[i]=',';
else if (message[i]=='w') ENCODED[i]='.';
else if (message[i]=='x') ENCODED[i]=';';
else if (message[i]=='y') ENCODED[i]=')';
else if (message[i]=='z') ENCODED[i]='@';
else if (message[i]==' ') ENCODED[i]='#';
else if (message[i] =='\0') {ENCODED[i] = '\}'; break;}
else ENCODED[i]=' ';
}
cout<<"done encoding.\n";
cout<<"exporting file...\n";
ofstream OUTfile ("encoded.txt");
OUTfile<<ENCODED;
cout<<"file exported to parent directory.\n";
cin.get();
}
void Decode(){ //this is where I run into problems!!
string encoded[100]
char DECODED[100]
ifstream INfile ("encoded.txt");
cout<<"Decoding...\n";
INfile>>encoded;
for (i=0; i<100; ++i){
if (encoded[i]=='1') DECODED[i]='a';
else if (encoded[i]=='$') DECODED[i]='b';
else if (encoded[i]=='!') DECODED[i]='c';
else if (encoded[i]=='*') DECODED[i]='d';
else if (encoded[i]=='2') DECODED[i]='e';
else if (encoded[i]=='&') DECODED[i]='f';
else if (encoded[i]=='^') DECODED[i]='g';
else if (encoded[i]=='%') DECODED[i]='h';
else if (encoded[i]=='3') DECODED[i]='i';
else if (encoded[i]=='=') DECODED[i]='j';
else if (encoded[i]=='_') DECODED[i]='k';
else if (encoded[i]=='-') DECODED[i]='l';
else if (encoded[i]=='2') DECODED[i]='m';
else if (encoded[i]=='9') DECODED[i]='n';
else if (encoded[i]=='4') DECODED[i]='o';
else if (encoded[i]=='|') DECODED[i]='p';
else if (encoded[i]=='/') DECODED[i]='q';
else if (encoded[i]=='>') DECODED[i]='r';
else if (encoded[i]=='?') DECODED[i]='s';
else if (encoded[i]=='}') DECODED[i]='t';
else if (encoded[i]=='5') DECODED[i]='u';
else if (encoded[i]==',') DECODED[i]='v';
else if (encoded[i]=='.') DECODED[i]='w';
else if (encoded[i]==';') DECODED[i]='x';
else if (encoded[i]==')') DECODED[i]='y';
else if (encoded[i]=='@') DECODED[i]='z';
else if (encoded[i]=='#') DECODED[i]=' ';
else if (encoded[i] =='\0') {DECODED[i] = '\}'; break;}
else DECODED[i]==' ';
}
cout<<"Decoded file content: "<<DECODED;
cin.get();
}
int main(){
string choice;
cout<<"Encode new message or decode previous file?\n> ";
cin>>choice;
cin.ignore();
if (choice=="encode") Encode();
if (choice=="decode") Decode();
return 0;
}
如您所见,当涉及到解码功能时,我不知道自己在做什么。任何帮助,将不胜感激!谢谢!
编辑:我用提供的建议更新了代码,但是当编译器到达“INfile>>encoded;”行时 它说“INfile>>encoded”中的运算符“>>”不匹配......