954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ Shift Cypher

Okay the text on my input file is
Wxmv_bpm_xwl_jig_lwwza,_PIT._Q'u_awzzg,_Lidm._Q'u_inziql_Q_kiv'b_lw_bpib.

Here's My code but for some reason I can't get my program to convert the message to my output.txt file. Can someone please help thanks

#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)){              // Running If then statements to make the correct code show up
    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;         //Saving to the output file.
    outputFile << outChar;
    cout << outChar << endl;
    }

    inputFile.close();// Closing INPUT.txt file
    outputFile.close();// Closing OUTPUT.txt file
    }
    }
byrosport
Newbie Poster
12 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

what exactly is the cypher you are using? Are you just adding or subtracting a certain amount from ever character?

NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
 
what exactly is the cypher you are using? Are you just adding or subtracting a certain amount from ever character?


I don't know how to tell what cipher I am using?

byrosport
Newbie Poster
12 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

lool, so what was the idea? You wrote a program and you don't know what it does?

What exactly is done to the input to make the output?

pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 111
 

lool, so what was the idea? You wrote a program and you don't know what it does?

What exactly is done to the input to make the output?


Run the program it convert an input code to a readable sentense I just need the end to send it to and OUTPUT text and i cant get it to work

byrosport
Newbie Poster
12 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

The reason why there isn't anything outputting to the outfile.txt is due to the fact that outChar is being manipulated in the for-loop a number of times but only until the end, past the end of the for-loop, is a call made to the outfile. If you check your normal cout output window you will see all your expected characters.

Something to take note on. You may want to change up your indentation slightly as it will make it easier for you to read.

If a user enters a number greater or smaller than you expect, use a while loop to ask the user again instead of automatically exiting the program.

If you already checked for an input/output file to exist, you don't need to check it again.

You also need to add the #include if you are to use getline(infile, str)

Saith
Junior Poster
118 posts since Dec 2010
Reputation Points: 86
Solved Threads: 24
 

The reason why there isn't anything outputting to the outfile.txt is due to the fact that outChar is being manipulated in the for-loop a number of times but only until the end, past the end of the for-loop, is a call made to the outfile. If you check your normal cout output window you will see all your expected characters.

Something to take note on. You may want to change up your indentation slightly as it will make it easier for you to read.

If a user enters a number greater or smaller than you expect, use a while loop to ask the user again instead of automatically exiting the program.

If you already checked for an input/output file to exist, you don't need to check it again.

You also need to add the #include if you are to use getline(infile, str)

Where do i need to get rid of the outChar? I am struggling finding the right way?

byrosport
Newbie Poster
12 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 
Where do i need to get rid of the outChar? I am struggling finding the right way?


Please show me where I need to change things I am so close

byrosport
Newbie Poster
12 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 
outputFile << outChar;


needs to be included in the for-loop, not the while loop.

If it's only included in the while loop, the last character read will be placed in the OUTFILE.TXT. Whereas if the statement is placed in the for loop, every character read will be placed in the OUTFILE.TXT.

Saith
Junior Poster
118 posts since Dec 2010
Reputation Points: 86
Solved Threads: 24
 
outputFile << outChar;

needs to be included in the for-loop, not the while loop.

If it's only included in the while loop, the last character read will be placed in the OUTFILE.TXT. Whereas if the statement is placed in the for loop, every character read will be placed in the OUTFILE.TXT.


Thanks a Bunch That really helped me out

byrosport
Newbie Poster
12 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: