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

Recommended Answers

All 3 Replies

Ok so I was working on it and I figured out how to get the correct message from the input file, but now I cant get the message saved in the OUTPUT folder. Only the last letter of the string is appearing in the folder. This is my new code.

#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 - 'a';
                        outChar += 26 + shift;
                        outChar %= 26;
                        outChar += 'a';
                        cout << outChar;
                        }
                    if( inChar >= 'A' && inChar <= 'Z' ) {
                        outChar = inChar - 'A';
                        outChar += 26 + shift;
                        outChar %= 26;
                        outChar += 'A';
                        cout << outChar;
                        }
                }


                inputFile >> str;
                outputFile << outChar;
                cout << outChar << endl;
            }

        inputFile.close();// Closing INPUT.txt file
        outputFile.close();// Closing OUTPUT.txt file
        }
    }

Thanks in advance for your help

It will only output the last letter because a char type is only one character. You can use a string and add the letters onto that then output the word with outFile.

Thanks sfuo.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.