2013-11-25 4 views
1

Я пытаюсь заставить программу получить вход в одном экземпляре для шифрования для пользователя, находясь в другом экземпляре (когда захочет пользователь), из того же файла, созданного при шифровании запускать и отменять шифрование, но вместо этого он просто дает мне то, что выглядит как коды ошибок. Около 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);, который не имеет никакого эффекта.

+0

Ты потрясающий. Я не знаю, почему так оно и было. Возможно, я ошибся. У меня было ощущение, что это будет синтаксис, но это было так долго, что эти детали скользят из моего внимания. Спасибо. –