0

so there's something in my program that is not going the way I feel like it should go. If I could get some help I would appreciate it. I'll explain how it's supposed to work first and follow up with my question.

So I wrote an encryption program that asks the user to input a string and then it encrypts it and creates a file called "secret.dat" and puts the encrypted phrase in there.

If the user were to put in the phrase:

hello world 123

it would send this into the file:

11spwwzshzcwoh234&6#12

The "11" indicates how many letters the letter was shifted to the right by. This is followed by the phrase he entered in encrypted. The '&' character shows where the encryption ends. Each space in his phrase uses the previous letter and shifts it over by 4 and finally after the '&' character it tells the number location of where the spaces are seperated by a '#' character.

The current program I am writing decrypts the "secret.dat" file and shows his phrase on the screen.

This is what I have so far:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    //Declare Variables
    int shift;
    ifstream inData;
    string input;
    string output;
    int length;

    //Open file
    inData.open("secret.dat");

    //Begin program
    inData >> shift;

    getline(inData, input, '&'); 

    length = input.length();

    for (int count = 0; count < length; count++)
    {
        if (input[count] >= 'a' && input[count] <= 'z')
        {
            output += ((input[count] - 'a' - shift + 26) % 26) + 'a';
        }
        else if (input[count] >= '0' && input[count] <= '9')
        {
            output += ((input[count] - '0' - shift + 10) % 10) + '0';
        }       
    }

    //Declare variables for location of spaces
    int i = 0;
    char ignore;
    int spaces[20];
    int location;

    //Begin finding the spaces
    while (!EOF)
    {
        inData >> location;
        spaces[i] = location;
        inData >> ignore;
    }

    //Preview each result to compare and make sure they are working right
    cout << shift << endl;
    cout << input << endl;
    cout << output << endl;
    cout << spaces[0] << endl;
    cout << spaces[1] << endl;

    return 0;
}

This is what I get as a result

11
spwwzshzswoh234
hellohworldw123
4704512
0

Obviously the last 2 lines are not working correctly (NOTE: this is not how I am going to display it, I just printed these to the screen so that I could see what the result was and make sure it was correct, which it isn't).

So, my question is why my while loop isn't working correctly. It gives me a bunch of random numbers in the first array slot when it should put a 6 in the first spot, then it should skip the next character and then put a 12 in the second spot in the array which it just puts a 0 there. If I just call an integer from the file outside of the while loop it gives me a 6 no problem so I'm unsure on why it is doing this. I figured it would put the first integer in the first slot in the array, then skip the next character, then put the next integer in the array and skip the next character and so on until the end of the file which is why I did it in a while loop and called an integer first, then put that integer into the array, and then called a character which I won't use and have it repeat until the end of the file. The reason I'm doing this is so that I have an array with the location of the spaces in it so that I can use the array to replace the letters that should be spaces with a space character ' '.

Thanks for anyone willing to help!

Cush
  • 67
  • 7
  • 1
    `EOF` is a constant. But it's not enough to change that to using an `eof` function. You should check the stream for failure state *after each read operation*. Note that EOF is not set until a read operation fails. You don't know you're there until you've tried to pass it. – Cheers and hth. - Alf Oct 22 '14 at 04:46
  • @Cheersandhth.-Alf I'm not sure how to check for a failure state. When I run the program, it doesn't give me any errors. I figured that it would keep going until the end of the file is reached by putting that there especially since when I asked my teacher about how to find the spaces, he said to check for the end of file and then call an integer and then put it into an array and then call a characer to ignore. – Cush Oct 22 '14 at 04:57
  • Using a stream as a *condition* in an `if` or `while` is enough to convert to a boolean that is true if the stream is OK. It's equivalent to writing `!stream.fail()`. The usual idiom is `while( stream >> a_variable ) { ... }`, since the result of `>>` is a reference to the stream, triggering the implicit conversion to boolean. – Cheers and hth. - Alf Oct 22 '14 at 05:05
  • Well I changed it to "while(!inData.eof())" and then added i++ under where I made the array equal to the integer and it seems to work. Was it just because I was using eof in the wrong format? – Cush Oct 22 '14 at 05:07
  • Note that `inData.eof()` reflects the internal state which is not updated until a read operation has *encountered* end-of-file. So if the file is empty initially, then `while( !inData.eof() )` will, in blissful ignorance of that fact, try at least one iteration of the loop body. – Cheers and hth. - Alf Oct 22 '14 at 05:09

0 Answers0