Программа базового шифрования неправильно читает из файла

Я пытаюсь сделать так, чтобы программа получала входные данные в одном экземпляре для шифрования для пользователя, а в другом (когда пользователь этого хочет) извлекает из того же файла, созданного в ходе шифрования, и переворачивает шифрование, но вместо этого просто дает мне то, что выглядит как коды ошибок. Около 6 цифр / букв каждый раз, но это совсем не связано с тем, что он должен делать.

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <conio.h>
#include <cstdio>
#include <string>

using namespace std;
//ENCRYPTION AND DECRYPTION
string Decode(string text)
{
    int a;
    for(a=0; a<text.length(); a++){
        text[a]--;
    }
    return text;
}


string Encode (string text)
{
    int a;
    for(a=0; a<text.length(); a++){
        text[a]++;
    }
    return text;
}

//PROMPTS AND MAIN PROGRAM
int main(){
char input;
cout<<"+-+-+-+-+-+-+-+\n";
cout<<"|C|r|y|p|t|e|r|\n";
cout<<"+-+-+-+-+-+-+-+\n\n";
cout<<"Version 1.01 - Revision 2013\n";
cout<<"Created by Dylan Moore\n";
cout<<"____________________\n\n";
cout<<"Hello, would you like to decode or encode an encryption key?\nType 'd' for decode or 'e' for encode.\n\n";
cin>>input;
cin.get();
    switch(input){
        //DECODE
        case 'd':
        {
            string Message;
            ifstream myfile;
            myfile.open ("Key.txt");
                if (myfile.fail())
            {            
                    cout<<"Key.txt not found! Please make sure it is in the same directory as Crypter!\n\n";            
                    cin.get();            
                    return 0;            
                }    
            getline(myfile, Message);
            myfile.close();
            cout<<"Decoded Data:\n"<<Decode;(Message); 
            break;
        }
        //ENCODE
        case 'e':
        {
            string Message;
            ofstream myfile;
                    cout<<"Type the key you wish to be encrypted. When finished, press 'enter' 2 times to confirm.\n\n";
            getline (cin, Message);
            cin.get();
            myfile.open ("Key.txt");
            myfile<<Encode(Message);
            myfile.close();
                    cout<<"Thank you, your message has been saved to 'Key.txt' in this directory.\n";
            break;
        }

    }
    return _getch();
}

1 ответ

cout<<"Decoded Data:\n"<<Decode;(Message);
//                             ^ wrong

должно быть

cout<<"Decoded Data:\n"<<Decode(Message); 

Ваш текущий код печатает адрес Decode Затем функция выполняет второе утверждение (Message); который не имеет никакого эффекта.

Другие вопросы по тегам