2016-10-07 1 views
-2

이미 답변 한 사용자 덕분에! 이제 출력 파일에 스트림을 출력하는 데 여전히 문제가 있습니다. 나는 out_stream.put (ch)을 사용해야한다고 생각하지 않는다. 또는 out_stream < < ch; . 스트림을 출력하기 위해 여기에 뭔가 빠져 있습니다.이 기능을 사용하여 key_shift 변경 결과를 출력 파일로 인쇄하는 위치를 어떻게 수정할 수 있습니까?

#include <iostream> 
#include <cstdlib> 
#include <fstream> 


using namespace std; 


void display_menu();  //Display the menu 
void get_files(ifstream& in_stream, ofstream& out_stream); 
void encrypt(ifstream &in_stream, ofstream &out_stream, int key_shift);  //Encrypts a file 
int get_num_1_through_10(int number); //Gets an integer numbered 1-10 and has the user re-enter integer if incorrrect 


int main() 
{ 
ifstream in_stream; //declaring the input file path 
ofstream out_stream; //delcaring the output file path 
string in_filename, out_filename; 

int option;   //Declaration of user "option" choice 
int key_shift;   //Declaration of user "shift key value" choice 
do     //Runs menu at least once 
{ 
    display_menu();   //function call 
    cin >> option;   //User inout for the option menu 
    switch(option) { 

     case 1:  cout << "Enter a value between 1 and 10 for the shift key value: ";   //Prompts the user to choose a "shift key value" between integers 1-10 
        cin >> key_shift;                 //User input for the "shift key value" 

        get_num_1_through_10(key_shift);             //Function call to get a number 1-10 

        cout << "You have chosen a shift key value of " << key_shift << endl;   //Prints to screen users "shift key value" choice 
        break; 

     case 2:  cout << "Time to get the files set up!\n"; 

        cout << "Beginning the encryption process...\n"; 

        encrypt(in_stream, out_stream, key_shift); 

        in_stream.close(); 
        out_stream.close(); 


        break; 

     case 3:  break; 

     case 4:  cout << "Goodbye" << endl;       //User chooses to quit the program 
        return 0; 

     default: cout << "Enter a choice 1-4" << endl;  //Alerts the user of incorrect input 
    } 

} while (option != 4); //While option is anything other than 1-4, quits 
return 0;     //Quits 
} 


void display_menu() { 
cout << "1) Set the shift key value" << endl; 
cout << "2) Encrypt a message" << endl; 
cout << "3) Decrypt a message" << endl; 
cout << "4) Quit" << endl; 
cout << "Type in an option and hit enter: "; 
} 

int get_num_1_through_10(int number) { 

if ((number > 0) && (number < 11)) {  //Excluding integers except integers 1-10 

    return number;       //If true, returns the users integer choice 
} 

else {              //If false, returns user back to menu function 
    cout << "Please input a number between 1 and 10\n";  //Reminds the user of incorrect integer input 
    return main();           //Returns to main function 
} 
} 


void encrypt(ifstream &in_stream, ofstream &out_stream, int key_shift) { 

ifstream in_filename; 
ofstream out_filename; 
get_files(in_filename, out_filename); 

char ch; 

do { 

(in_stream.get(ch)); 

    while (ch != '\n') { 
     ch = ch + key_shift; 
    out_stream.put(ch); 
    } 
} 
return; 
} 

void get_files(ifstream& in_stream, ofstream& out_stream) { 

string in_filename, out_filename; 
cout << "Enter the source file name: "; 
cin >> in_filename;       //User types in the file name to receive data 
cout << "Enter the destination file name: "; 
cin >> out_filename;      //User types in the file name to output the data to 

in_stream.open(in_filename.c_str());  //Opens the stream for input file 
out_stream.open(out_filename.c_str()); //Opens the stream for out file 

if (in_stream.fail() || out_stream.fail()) {  //If the input or output fail 
    cout << "Error opening input/output files\n"; //Alerts the user of failure 
    exit(1);          //Terminates 
} 

cout << "Files opening!" << endl; 
} 
+0

key_shift 키는 사용자가 먼저 입력합니다. – Logan

+0

그런 다음 모든 코드를 제공하지 않았다는 것을 이미 알고 있습니다. [이 while (! in_stream.eof())는 방법에 의해 문제가 될 것입니다] (http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered- 잘못), 문제가 아니라면. 모든 코드가 없어도 확신 할 수있는 방법은 없습니다. – user4581301

+0

잠깐. 'ch '는 한 번만 읽으면되지만 마술처럼 바뀌는 것처럼 작동합니다. 일하지 않을거야. – user4581301

답변

0

문제 1 :

while (!in_stream.eof()) 

는 파일의 끝을지나 읽고이 파일의 끝을 명중 있다는 테스트하기 전에 하나 개 추가 읽기를 제공 할 것입니다. 여기에 더 많은 : Why is iostream::eof inside a loop condition considered wrong?

솔루션 :

읽기, 다음 (뿐 아니라 EOF 많은 일이 잘못 할 수 있기 때문에) 유효한 읽기 테스트와 읽기가 유효한 경우 다음 읽기 값을 사용하십시오.

문제 2 :

in_stream.get(ch); 
while (!in_stream.eof()) { 
    while (ch != '\n') { 
     ch = ch + key_shift; 
    out_stream << ch; 
    } 
} 

데도이 루프 ch 판독 없다. 값을 읽지 않으면 변경되지 않으며 EOF가 절대로 읽히지 않을 것이라고 확신 할 수 있습니다.

솔루션 :

루프에서 ch을 읽어보십시오.

while (in_stream.get(ch)) { 

    while (ch != '\n') { 
     ch = ch + key_shift; 
     out_stream << ch; 
    } 
} 

같은

뭔가 하나 개의 조류로 두 돌을 죽일 것이다. while (in_stream.get(ch))은 읽기가 성공한 경우에만 루프의 본문을 입력하고 읽은 다음 테스트합니다.

+0

정보 주셔서 감사합니다! 나는 그것을 바꿨고 출력 파일의 이름을 입력 한 후 프로그램이 멈춘 것처럼 보였다. – Logan

+0

개발 환경에서 거의 확실하게 제공되는 디버거에서 프로그램을 실행할 수있는 좋은시기입니다. 프로그램이 정지 할 때까지 기다렸다가 프로그램을 중단시킨 다음 bactkrace를 검사하여 막힌 곳을 확인합니다. – user4581301

+0

루즈를 발견했습니다. "!" ! – Logan

관련 문제