Hi, I have an assignment where i'm supposed to read from a Input File and save it to an output file. The final expression should only contain alphanumeric characters. The code that I have so far gives me both symbols and alphanumeric characters. I was wondering if anyone saw anything in my code that would be causing this. Thanks in advance for your help.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
string str;
int shift;
char inChar;
char outChar;
char c;
ifstream inputFile("INPUT.txt");// Opening INPUT.txt file
ofstream outputFile("OUTPUT.txt");// Opening OUTPUT.txt file
if(!inputFile){// Checking for errors or existence of INPUT.txt file
cout << "Error...file INPUT.txt doesnt exist.\n";
exit(1);
}
if(!outputFile){// Checking for errors or existence of OUTPUT.txt file
cout << "Error...file OUTPUT.txt doesnt exist.\n";
exit(1);
}
cout << "Enter an amount to shift by: ";// Prompting users for shift amount
cin >> shift;
if(shift<-9|| shift>9){// Liminting range of shift numbers between -9 and 9
cout<< "Shift must be a number between -9 and 9.\n";
}else
{if(!inputFile){// Checking for errors in INPUT.txt file
cout << "Error...file INPUT.txt doesnt exist.\n";
exit(1);
}
if(!outputFile){// Checking for errors in OUTPUT.txt file
cout << "Error...file OUTPUT.txt doesnt exist.\n";
exit(1);
}
while(getline(inputFile,str)){
for(int i=0; i<str.length();i++){
inChar =str[i];
if( inChar >= '0' && inChar <= '9' ) {
outChar = inChar - '0';
outChar += 10 + shift;
outChar %= 10;
outChar += '0';
cout<< outChar;
}
if( inChar >= 'a' && inChar <= 'z' ) {
outChar = inChar - '0';
outChar += 26 + shift;
outChar %= 26;
outChar += '0';
cout << outChar;
}
if( inChar >= 'A' && inChar <= 'Z' ) {
outChar = inChar - '0';
outChar += 26 + shift;
outChar %= 26;
outChar += '0';
cout << outChar;
}
}
inputFile >> str;
outputFile << outChar;
cout << outChar << endl;
}
inputFile.close();// Closing INPUT.txt file
outputFile.close();// Closing OUTPUT.txt file
}
}