Issues regarding Menu

Hello, I have to make a menu, 1. compress Text that will give allow you to input a value that will appear in 2. Extract Text.

The problem is that it outputs the print fine but won't accept any value and will infinitely repeat if done otherwise, I tried making choice a char but then it won't output anything inside the case.

Help would be appreciated

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <string>
#include <iostream>
using namespace std;

int main()
{
	int choice;
	string str;//Declare the string
	do
	{
		std::cout << "1. Compress Text" << std::endl << "2. Extract Text" << std::endl << "3. Exit\n";
		std::cin >> choice;

		switch (choice)
		{
		case 1:
			getline(cin, str);

			cout << "----------------------------------------\n";
			cout << "Please input the Text you want compressed:\n";
			cout << "----------------------------------------\n";

			break;

		case 2:
			cout << "----------------------------------------\n";
			std::cout << "Here is your compressed Text: ";
			cout << str << endl;
			cout << "----------------------------------------\n";
			break;

		case 3:
			cout << "----------------------------------------\n";
			std::cout << "Bye\n";
			cout << "----------------------------------------\n";
			return 3;
		}
	} while (choice != 3);
}
Last edited on
Update!

Started working on it, and got it to sort of work. The issue is that it doesn't output the whole value, could it be the fault of char limit?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <string>
#include <iostream>
using namespace std;

int main()
{
	int choice;
	string str;//Declare the string
	do
	{
		cout << "1. Compress Text" << std::endl << "2. Extract Text" << std::endl << "3. Exit\n";
		cin >> choice;

		switch (choice)
		{
		case 1:

			cout << "----------------------------------------\n";
			cout << "Please input the Text you want compressed:\n";
			cout << "----------------------------------------\n";

			cin >> str;
			getline(cin, str);

			break;

		case 2:
			cout << "----------------------------------------\n";
			cout << "Here is your compressed Text: ";
			cout << str << endl;
			cout << "----------------------------------------\n";
			break;

		case 3:
			cout << "----------------------------------------\n";
			cout << "Bye\n";
			cout << "----------------------------------------\n";
			return 3;
		}
	} while (choice != 3);
}
Last edited on
Yeah, read this and reformat your posts.
https://www.cplusplus.com/articles/jEywvCM9/
Perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <string>
#include <iostream>
using namespace std;

int main() {
	int choice;
	string str;		//Declare the string

	do {
		cout << "1. Compress Text" << std::endl << "2. Extract Text" << std::endl << "3. Exit\n";
		cin >> choice;
		cin.ignore(1000, '\n');

		switch (choice) {
			case 1:
				cout << "----------------------------------------\n";
				cout << "Please input the Text you want compressed:\n";
				cout << "----------------------------------------\n";
				getline(cin, str);
				break;

			case 2:
				cout << "----------------------------------------\n";
				cout << "Here is your compressed Text: ";
				cout << str << endl;
				cout << "----------------------------------------\n";
				break;

			case 3:
				cout << "----------------------------------------\n";
				cout << "Bye\n";
				cout << "----------------------------------------\n";
				break;

			default:
				cout << "Unknown option\n";
				break;
		}
	} while (choice != 3);
}

It's perfect! Thank you.

On another note is it possible to replace the text input with ASCII of range 128-255, i thought about using the following code;

replace(str.begin(), str.end(), 'a', 'c');

but I can't seem to do much with it, as it only allows a single character and it would be tedious to code every single word and phrase.

So I was wondering if there is a method that would replace certain words with ones in the ASCII range.

Thank you for your time
Last edited on
What is an example of a "word" outside the ASCII range, and what would it be replaced with?

e.g. [200, 100, 220] --> [46, 100, 46]?

PS: If you're using std::strings, it's not specified whether <char> (string's underlying character type) is signed or unsigned. So if you are expecting input in range [0, 255] you might want to use std::basic_string<unsigned char> directly.
Last edited on
The example my teacher used is a quote -

"Momma said that the Forrest part was to remind me that sometimes we all do things that, well, just don't make no sense.",

and I have to use ASCII to change some of the more common words in English into a compressed form as follows;

"Momma said € • FÂÞî pót › · Þm¸d Æ € •Žs ¿ – Ä Ú¸gs €, ¿ll, jÌt dÃ't make Ë såse."

I have no idea how to go about this except to input every single word for the code to detect and if it doesn't move on, but then what will it replace it within the range of 128-225.
is it possible to replace the text input with ASCII of range 128-255

Yes, it is possible.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#include <algorithm>
#include <numeric>

int main()
{
   // create a string long enough to contain the standard ASCII table
   std::string str(128, ' ');

   // fill the string with the ASCII codes from 0 to 127
   std::iota(str.begin(), str.end(), 0);

   // display the string, excluding the control characters
   for (size_t itr { 32 }; itr < str.size(); ++itr)
   {
      std::cout << str[itr];
   }
   std::cout << "\n\n";

   // transform the string, adding 128 to each char
   std::transform(str.begin(), str.end(), str.begin(),
                  [ ] (unsigned char c) -> unsigned char { return c + 128; });

   // display the entire transformed string
   for (const auto& itr : str)
   {
      std::cout << itr;
   }
   std::cout << '\n';
}
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂

ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ 

The exact output of the transformed string may be different for you.

Do note the lambda at line 23, that allows using std::transform instead of a for loop to "walk through" the entire string to change each element as an unsigned char.
THANK YOU!

I added the code into my case 2 and it works perfectly!
Just a minor quibble, "compress a string" could mean something different than "transform a string". Your questions indicate you want transform.

Compressing a string (possible adding two or more elements together to create a single element, usually adding it to a new string):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

int main()
{
   std::string orig_str { "Hello World" };
   std::string trans_str;

   for (size_t itr { }; itr < orig_str.size() - 1; itr += 2)
   {
      trans_str += static_cast<unsigned char> (orig_str[itr] + orig_str[itr + 1]);
   }

   std::cout << orig_str.size() << ", " << trans_str.size() << '\n';

   std::cout << trans_str << '\n';
}
11, 5
¡╪Å╞▐

There are other compression schemes as well.

But, of course, if "compress" is what your assignment calls the transforming operation, then that is that.
The assignment is compression, since it's turning sense into såse.

Edit: An example (in all likelihood you could take the easy way out and use std::string here, I'm just being strict)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Example program
#include <iostream>
#include <string>

using String = std::basic_string<unsigned char>;
    
std::ostream& operator<<(std::ostream& os, const String& str)
{
    for (auto ch : str)
    {
        os << ch;   
    }
    return os;
}

int main()
{

    String str = (const unsigned char*)"sense";

    String search_str = (const unsigned char*)"en";
    
    unsigned char replacement_arr[] = { 134, '\0' };  
    String replacement = replacement_arr;
  
    auto it = str.find(search_str);
    if (it != String::npos)
    {
        str.replace(it, search_str.length(), replacement);
    }
    
    std::cout << str << '\n';
}
såse
Last edited on
is it possible to replace the text input with ASCII of range 128-255

Just another "minor quibble".

There are no ASCII characters in the range of 128-255, ASCII is a 7 bit encoding all valid ASCII characters are in the range of 0 - 127. While there are "extended ASCII" codes those "extended" codes are not standard and may differ between individual systems. And be careful, if your string is using a char, 128-255 is out of the range of a signed char type.

what I would do is get a statistical breakdown of english and assign the most common pairs of letters (eg "ee", "oo", "tt", "tr", "ph", ...) to the 128...255 range and play switcheroo with that.
that would get you about a 20-25 % compression over simple text if it were all the same case. Mixed case, far less, as you have to repeat the pairs 4 ways.

Topic archived. No new replies allowed.