Modify the following using VS C++ , Note to modify \" Encoding\" part of the ass
ID: 3865743 • Letter: M
Question
Modify the following using VS C++, Note to modify "Encoding" part of the assignment. **Decoding is already finished.**
Instructions are:
The code involves writing a number in place of each letter of the alphabet. The coding equivalences are: A=1, B=2, C=3, D=4, E=5, … Z=26 (case sensitive).
They are using zero for each space between words.
The program needs to first ask if ‘coding or decoding.’ If coding, it then asks for the messages (letters and spaces only) and in turn prints out a string of numbers that ‘code’ the messages according to the above. When encoding, the input message should be converted to uppercase before starting the encoding process; then if any non-legal characters are detected in the input stream during the process, they should be turned into the number 99 when output. To convert the string to uppercase you will use the toupper() function available in the cctype library.
If decoding, accept a series of integer values, each separated by a space; convert the numeric values to their letter and space equivalents. Any numbers that are out of bounds (i.e., not between 0 and 26) should be turned into a ? (question mark) in the output message.
A sample run might be:
Welcome to the Letter Code program!
Enter choice (<E>ncoding, <D>ecoding, or <Q>uit)? E
Enter your message: MEET AT SALLYS TODAY
Your coded message is:
13 5 5 20 0 1 20 0 19 1 12 12 25 19 0 20 15 4 1 25
Enter choice (<E>ncoding, <D>ecoding, or <Q>uit)? D
Enter your numbers, separated by a space. Use zero for a space in your message; enter the number 99 at the end of the list:
1 2 3 0 4 5 99
Your decoded message is:
ABC DE
Enter choice (<E>ncoding, <D>ecoding, or <Q>uit)? Q
Thanks for using the Letter Code program! =============================================
The Encode logic in this program go beyond the simple ‘cin’ function. Use string class and the global function getline() that can be used to pull a full string of characters from the cin object. You can then manipulate the string using functions of the string class, such as substr().
Code a separate class file for the conversion processes. The class will have two static, public members: Encode and Decode. The Encode function will receive the input string to be encoded from the user and return a string with the numeric conversions (separated by spaces, as shown above). The Decode function should receive a vector of int values which are then processed back into a string result (the original message)!
**Thank you my current program is below using VS C++ , please help by modifing my currnet code below:***
LttrCodLgic.h
#pragma once
#include <string>
#include <vector>
using namespace std;
using namespace System;
class LttrCodLgic
{
public:
static string Encode(string m);
static string Decode(vector<int> letters);
private:
};
LttrCodLgic.cpp
#include "stdafx.h"
#include "LttrCodLgic.h"
#include <string>
#include <vector>
#include<sstream>
using namespace std;
using namespace System;
string LttrCodLgic::Encode(string m)
{
char ip, op;
unsigned int i;
stringstream result;
for (i = 0; i < m.length(); i++)
{
ip = m[i];
ip = toupper(ip);
if ((int)ip >= 65 && (int)ip <= 99)
{
op = (int)ip - 65 + 1;
}
else
{
if (m[i] == 0)
{
op = ' ';
}
else
{
op = 99;
}
}
result << (int)op << "";
}// end of for
return result.str();
}
string LttrCodLgic::Decode(vector<int> letters)
{
char c;
unsigned int i;
stringstream result;
for (i = 0; i < letters.size(); i++)
{
if (letters[i] == 0)
{
c = ' ';
}
else
{
if (letters[i] < 1 || letters[i] > 26)
{
//out of bounds value...
c = '?';
}
else
{
//convert to char based on ASCII (i.e., A=65, B=66, etc.)
c = (char)(letters[i] + 64);
}
}
result << c;
}// end of for
return result.str();
}
LttrCod.cpp
#include "stdafx.h"
#include "LttrCodLgic.h"
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
using namespace std;
using namespace System;
void getEncodeString();
void getDecodeString();
int main()
{
char choice;
cout << "Welcome!" << endl;
do {
cout << "Encode, Decode, or Quit (E/D/Q): ";
cin >> choice;
if (!cin.good()) {
cin.clear();
cin.ignore(1000, ' ');
choice = '?';
}
switch (choice)
{
case'e':
case'E':
getEncodeString();
break;
case'd':
case'D':
getDecodeString();
break;
case'q':
case'Q':
break;
default:
cout << "Please enter E, D, or Q" << endl;
break;
}//end of switch
} while (toupper(choice) != 'Q');
cout << "Thanks! " << endl;
system("Pause");
return 0;
}
void getEncodeString()
{
//obtain input as String..
//call LttrCodLgic for Encode
string input, empty;
getline(cin, empty);
cout << "Please enter your message:" << endl;
getline(cin, input);
cout << "Your coded message is: " << LttrCodLgic::Encode(input) << endl;
}
void getDecodeString()
{
vector<int> letters;
unsigned int x;
cout << "Enter numbers to be decoded;" " use space between numbers and 99 to terminate input:" << endl;
do
{
cin >> x;
if (!cin.good())
{
cin.clear();
cin.ignore(1000, ' ');
x = 99;
}
if (x != 99)
{
letters.push_back(x);
}
} while (x != 99);
if (letters.size() > 1) {
cout << " Your decoded message is: " << endl;
//decode process here...(call to LttrCodLgic)
cout << LttrCodLgic::Decode(letters) << endl << endl;
}
return;
}
Explanation / Answer
Given below is the modified LttrCodLgic.cpp file and the output. Please don't forget to rate the answer if it helped. Thank you.
LttrCodLgic.cpp
#include "stdafx.h"
#include "LttrCodLgic.h"
#include <string>
#include <vector>
#include<sstream>
using namespace std;
using namespace System;
string LttrCodLgic::Encode(string m)
{
char ip;
int op;
unsigned int i;
stringstream result;
for (i = 0; i < m.length(); i++)
{
ip = toupper(m[i]);
if (ip >= 'A' && ip <= 'Z')
{
op = ip - 'A' + 1;
}
else
{
if (m[i] == ' ')
{
op = 0;
}
else
{
op = 99;
}
}
if(i != 0)
result << " ";
result << op;
}// end of for
return result.str();
}
string LttrCodLgic::Decode(vector<int> letters)
{
char c;
unsigned int i;
stringstream result;
for (i = 0; i < letters.size(); i++)
{
if (letters[i] == 0)
{
c = ' ';
}
else
{
if (letters[i] < 0 || letters[i] > 26)
{
//out of bounds value...
c = '?';
}
else
{
//convert to char based on ASCII (i.e., A=65, B=66, etc.)
c = (char)(letters[i] + 64);
}
}
result << c;
}// end of for
return result.str();
}
output
Welcome!
Encode, Decode, or Quit (E/D/Q): e
Please enter your message:
MEET AT SALLYS TODAY
Your coded message is: 13 5 5 20 0 1 20 0 19 1 12 12 25 19 0 20 15 4 1 25
Encode, Decode, or Quit (E/D/Q): d
Enter numbers to be decoded;
use space between numbers and 99 to terminate input:
13 5 5 20 0 1 20 0 19 1 12 12 25 19 0 20 15 4 1 25 99
Your decoded message is:
MEET AT SALLYS TODAY
Encode, Decode, or Quit (E/D/Q): e
Please enter your message:
ABC DE
Your coded message is: 1 2 3 0 4 5
Encode, Decode, or Quit (E/D/Q): d
Enter numbers to be decoded;
use space between numbers and 99 to terminate input:
1 2 3 0 4 5 99
Your decoded message is:
ABC DE
Encode, Decode, or Quit (E/D/Q): q
Thanks!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.