简体   繁体   中英

How to program different outputs for a .txt file string

I seem to be having a problem with my current program.

What I am trying to do is create a program that outputs the possible decryptions of a stream of text found in a .txt file.

I have created my program up to the following point, but I have run into a problem.

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

using namespace std;

int main(){

    ifstream inputFile;

    string message = " ";
    int lengthOfMessage, counter = 0;
    int ord = 0;
    char newChar;
    inputFile.open("Jumbled Message.txt");

    if(!inputFile){
        cout<<"Input File Cannot Be Found"<<endl;
    }

    while(inputFile)
        getline(inputFile, message);                   
        lengthOfMessage = message.length();

        for(int i=0; i < lengthOfMessage; i++){

            ord = int(message[i]);
            ord += 1;
            newChar = char(ord);

        cout<<"Run"<<counter<<" unjumbled: "<<newChar<<endl;
        message= " ";
        counter ++; 
    }
}

Basically, when the program runs, each individual ASCII value of the text shift once is displayed beside each run amount.

For Example:

Run 0 Unjumbled: \
Run 1 Unjumbled: 2
Run 2 Unjumbled: x

As I have mentioned earlier, I am trying to create different possible message outputs for each run within the program.

You may want to convert the full string before displaying it. Then got onto the next line in the file.

Here I have made a sample that convert a line in the file to all possible values and display them, then repeat for next line until end of file.

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

int main()
{
  std::ifstream         inputFile;
  std::string           jumbled_message;
  std::string           unjumbled_message;
  int                   line = 0;

  inputFile.open("Jumbled Messages.txt");
  if (!inputFile)
  {
      std::cerr << "Input File Cannot Be Found" << std::endl;
      return 1;
  }

  while (inputFile)
  {
      getline(inputFile, jumbled_message);
      unjumbled_message.resize(jumbled_message.size());
      for (int shift = 0; shift < 255; shift++)
      {
          for(size_t i = 0; i < jumbled_message.size(); i++)
          {
              unjumbled_message[i] = jumbled_message[i] + shift;
          }
          std::cout<< "Line" << line << " - shift: " << shift
                   << " - unjumbled: "<< unjumbled_message
                   << std::endl;
      }
      unjumbled_message.clear();
      line++;
  }
  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM