【问题标题】:How can I fix this to where the function will print out the results of the key_shift change into the output file?我该如何解决这个问题,函数会将 key_shift 更改的结果打印到输出文件中?
【发布时间】:2016-10-07 00:23:14
【问题描述】:

感谢这里已经回复的用户!现在我仍然无法将流输出到输出文件。我认为我不应该使用 out_stream.put(ch);或 out_stream

#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;
}

【问题讨论】:

  • key_shift 键是用户较早输入的。
  • 那么你已经知道你没有提供所有的代码。 This while (!in_stream.eof()) will be a problem by the way,如果不是问题。没有所有代码就无法确定。
  • 等一下。您只阅读了一次ch,但对其进行操作,就好像它会发生神奇的变化一样。不会工作。
  • 哦,我明白了,谢谢!

标签: c++ encryption input output


【解决方案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))读取然后测试,读取成功才进入循环体。

【讨论】:

  • 感谢您的信息!我更改了它,现在输入输出文件的名称后程序似乎挂起,所以我正在检查它。
  • 现在是在调试器中运行程序的好时机,几乎可以肯定,您的开发环境附带了该程序。等待程序挂起并强制程序中断,然后检查 bactkrace 以查看卡在哪里。
  • 找到胭脂“!” !
  • 输出似乎仍有问题。也许有类似 out_stream.put(ch) 的东西?不,不是那样!
猜你喜欢
  • 2021-10-20
  • 2021-12-24
  • 2019-02-08
  • 1970-01-01
  • 2020-03-25
  • 2020-02-14
相关资源
最近更新 更多