Read in a code -one character for each of the letters of the alphbet. Next, read
ID: 3702894 • Letter: R
Question
Read in a code -one character for each of the letters of the alphbet. Next, read in a message. Encode the message using the code. Repeat. For example: Enter the code: QWERTYUIOPASDFGHJKLZXCVBNM The code is: Incoming message: ABCD EFG: HIJ Coded message: QWER TYU: 10p! Do you want to play again CY/N)?N ALSO! You have to check that there are enough characters in the code. For example: Enter the code: [SOME CODE IS ENTERED WHICH IS LESS THAN 26 CHARACTERS] The code is not big enough! Do you want to play again (Y/N)? Y CCONTINUE]Explanation / Answer
#include <iostream>
using namespace std;
int main() {
// your code goes here
string code;
string ans;
string msg;
bool play;
do{
cout<<"Enter the code: ";
cin>>code;
if(code.length()<26)
{
cout<<"The code is not big enough!"<<endl;
goto end;
}
if(code.length()>26)
{
cout<<"The code is too big!"<<endl;
goto end;
}
cout<<"The code is: ";
for(int i=0;i<code.length();i++)
cout<<"["<<code[i]<<"] ";
cout<<endl;
cin>>msg;
cout<<"Incoming message: "<<msg<<endl;
for(int i=0;i<msg.length();i++)
{
if(msg[i]>='A' && msg[i]<='Z')
ans+=code[msg[i]-'A'];
else
ans+=msg[i];
}
cout<<"Coded message: "<<ans<<endl;
end:
char play_temp;
cout<<"Do you want to play again (Y/N)?";
cin>>play_temp;
if(play_temp=='Y')
play=true;
else
play=false;
}while(play);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.