I am wondering if someone can help me trouble shoot my code. It seems to be working all properly but when I go to run it that halfway through it starts to delete my data even though I never call anything to delete it.
So for example my file goes from:
=======================
Rotated by 11 positions
=======================
Lnwypeyw lala sqz xa na lnwypeyw eb pdau sqz pwa w eppa
na pea bn znawec.
-- F. L. IyAru**
to
=======================
Rotated by 12 positions
=======================
Moxzqzx mbmb tra yb ob moxzqzx c qebv tra qxb x qqb
ob qb co aobxd.
-- G. M. JzBsv
Here is my code, I have tried going through it a few times and it all makes sense logically and have no idea while it is losing data.
#include <iostream>
#include <cctype>
#include <fstream>
using namespace std;
bool isUpper(char character){
//this will test and see if it's upper or lowercase
bool upper;
if(character>= 'A' && character <= 'Z'){
upper=true;
}
else {
upper= false;
}
return upper;
}
bool isLower(char character){
//this will test and see if it's upper or lowercase
bool lower;
if(character >= 'a' && character <= 'z'){
lower=true;
}
else {
lower= false;
}
return lower;
}
//Actual function that will rotate the character
char rotate(char character, int offset) {
char next_letter;
//Changes it if it's a lower case
if (isLower(character)) {
next_letter = character + offset;
if (next_letter > 'z'){
next_letter = (next_letter - 26);
return next_letter;
}
return next_letter;
}
else if(isUpper(character)) {
next_letter = character + offset;
if (next_letter > 'Z'){
next_letter = (next_letter - 26);
return next_letter;
}
return next_letter;
}
else {
return character;
}
}
int main() {
//variables for program
char character = 'a';
int offset = 0;
while(offset < 26){
//opens the file
ifstream fin;
fin.open("secretMessage.txt");
if(!fin.good()) {
cout << "Please check your file name!!!";
return 0;
}
//report for reading
cout << "=======================" << endl
<< "Rotated by " << offset << " positions" << endl
<< "=======================" << endl;
//Reads until it's at the end of the file
while ((! fin.eof())){
character = fin.get();
cout << rotate(character,offset);
}
//makes it run 26 times
++offset;
fin.close();
cout << endl << endl;
}
//Closes the file output.
return 0;
}