-2

Thanks to the users here who already replied! Now I am still having problems outputting the stream to the output file. I don't think I should be using out_stream.put(ch); or out_stream << ch; . I am missing something here for outputting the 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;
}
Logan
  • 11
  • 3
  • The key_shift key is entered earlier by the user. – Logan Oct 07 '16 at 00:24
  • Then you already know you have not supplied all of the code. [This `while (!in_stream.eof())` will be a problem by the way](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong), if not the problem. No way to be sure without all of the code. – user4581301 Oct 07 '16 at 00:26
  • Wait a sec. You only read `ch` once, but operate on it as though it's going to magically change. Not going to work. – user4581301 Oct 07 '16 at 00:28
  • Oh I see, thank you! – Logan Oct 07 '16 at 00:38

1 Answers1

0

Problem 1:

while (!in_stream.eof()) 

Will read past the end of the file and provide one extra reading before it tests that it's hit the end of the file. More here: Why is iostream::eof inside a loop condition considered wrong?

Solution:

Read, then test for a valid read (and not just for EOF because many things can go wrong), and then, if the read is valid, use the value read.

Problem 2:

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

Nowhere in this loop is ch read. If the value is not read, it won't change, and you can be pretty sure the EOF is never going to be read.

Solution:

Read ch in the loop.

Something like

while (in_stream.get(ch)) {

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

Should kill both stones with one bird. while (in_stream.get(ch)) reads and then tests, only entering the body of the loop if the read is successful.

Community
  • 1
  • 1
user4581301
  • 29,019
  • 5
  • 26
  • 45
  • Thanks for the information! I changed it and the program seems to hang after I input the name of the output file now so I am checking into that. – Logan Oct 07 '16 at 00:51
  • Good time to run the program in the debugger that almost certainly came with your development environment. Wait for the program to hang and force the program to break, then inspect the bactkrace to see where it got stuck. – user4581301 Oct 07 '16 at 00:55
  • Found the rouge "!" ! – Logan Oct 07 '16 at 01:07
  • There still seems to be something wrong with the output. Maybe there's something like out_stream.put(ch) ? Nope, not that! – Logan Oct 07 '16 at 01:10
  • There is [`out_stream.write(&ch, 1);`](http://en.cppreference.com/w/cpp/io/basic_ostream/write) – user4581301 Oct 07 '16 at 05:57