Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <iostream> #include <fstream> #include <ctime> #include <string> using

ID: 3620020 • Letter: #

Question

#include <iostream>
#include <fstream>
#include <ctime>
#include <string>
using namespace std;

void main ()
{
int i = 0, a, length;
srand((unsigned)time(NULL));
string messageOriginal[100];
string messageShuffled[100];
string string;
ifstream textfile;
textfile.open("sample.txt");


length = textfile.length();
for (int n = 0; n < length; n++)
{
getline(textfile,messageOriginal[n]);
messageOriginal[n] += ' ';
}
for (int n = 0; n < length; n++)
getline(textfile, messageShuffled[n]);
while(i < 6)
{
a = rand()%6;
string = messageShuffled[i];
messageShuffled[i] = messageShuffled[a];
messageShuffled[a] = string;
i++;
}

cout << "Original Message: ";
for (int n = 0; n < length; n++)
{
cout << messageOriginal[n];
}
cout << "Shuffled Message: ";
cout << messageShuffled[a];

cout << endl << endl;

}

Above is my program code. Everything works except the encrypted text file won't print to the screen. I haven't been able to find the error in my code so hopefully some fresh eyes can provide some feedback. The program opens a text file copies it to two arrays; one prints the original message to the screen and the other is supposed to encrypt the message and print it to the screen.

Explanation / Answer

please rate - thanks this assumes 1 line of code in the text file #include <iostream>
#include <fstream>
#include <ctime>
#include <string>
using namespace std;

int main ()
{
int i = 0;
srand((unsigned)time(NULL));
string messageOriginal;
string messageShuffled;
string string;
string::size_type n;
int j,done=0,donecount;
ifstream textfile;
textfile.open("sample.txt");
if(textfile.fail())             //is it ok?
       { cout<<"file did not open please check it ";
        system("pause");
        return 1;
        }

getline(textfile,messageOriginal);
n=messageOriginal.length();
int used[n];
for(i=0;i<n;i++)      //clear the used array
    used[i]=0;
i=0;
while(done==0)        
{j=rand()%n;         //get a random number between 0 and # of numbers in string
   if(used[j]==0)      //can't use letter if already used
     {used[j]=1;      //mark that letter used
      messageShuffled[i]=messageOriginal[j];
      i++;
      }
    donecount=0;
    for(j=0;j<n;j++)    //check if all letters used
       if(used[j]==1)    //count how many letters have been used
           donecount++;
    if(donecount==n)    //all letters used?
       { done=1;         //yes-set done flag
        cout<<" ";
        }
     }
messageShuffled[n]='';
cout<<i<<" "<<messageShuffled[i]<<endl;
cout << "Original Message: ";
cout << messageOriginal<<endl<<endl;
cout << "Shuffled Message: ";
for(i=0;i<n;i++)
cout << messageShuffled[i];
cout << endl << endl;
system("pause");
return 0;
}