Need help with converting !!

Hi

I'm writing a small pice of code that takes numbers and convert it to string.

so that's what I got

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

int main() {
  
    
    
   char letter;
   char letter1;
   char letter2;

cin >> letter;
cin >>letter1;
int sum = letter * 128 + letter1;
cout << sum << endl;
int eq;
eq = sum;
cin >> letter2;
eq = (sum * 128 + letter2);
cout << eq;


    
    

    
    return 0;
}



It's just a scratch of the idea that I want to do.


So let me explain what's going on here.

The it asks the user to enter a letter, say 'w' , which it's value is 119
then enter another letter, say 'o' which value is 111

then it do the equation, which is take the value of the first letter multiple it by 128 then add it to the value of the second letter.

So, one question here. How can I make it does this process in a For loop, or any kind of loop??

--

Then I have the second part which is the other way around. That is when the user enter a numbers then the code convert these numbers to a string.

251394404 - "d"
1964018 - "rd"
15343 - "ord"
119 - "word"

so what's happening here is the it divide each time by 128. But I can't do that in a code, I would really need help with that part.


and Thanks :)
For the first part, I would use a do..while loop.
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
bool stop = false;

char letter;

int sum = 0;

do {
	std::cout << "Enter a letter, or enter 0 to stop: ";
	std::cin >> letter;

	if ( !std::cin )
	{
		std::cout << "\nThat was not a letter, please try again.\n\n";

		std::cin.clear ( );
	}

	else if ( letter == '0' ) stop = true;

	else
	{
		sum *= 128;

		sum += letter;

		std::cout << "\nYou entered letter number " << int ( letter )
			<< ".\nThe current sum is " << sum << ".\n\n";
	}
} while ( !stop );
Last edited on
Thanks

So if I want the other way around ?

I have this for now

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main() {
  
int number;
int ans;
int ans1;
string letter;
string letter2;


cout << "Enter the code: " << endl;
cin >> number;
ans = number/ 128;
ans1 = ans/128;
letter = ans1 /128;




cout << letter << endl;


    
    return 0;
}



So what that does is the user enter number and it convert it to a string.

for example is I enter: 251394404
that will print: d
which came from " word"

and that's how it goes :

251394404 - "d"
1964018 - "rd"
15343 - "ord"
119 - "word"


How can I print the whole word ?

and the user is only allowed to enter a string that contain 4 characters only
I'm actually surprised at how well this works...
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <string>

char get ( int & num );

int main ( )
{
	bool stop = false;

	int sum = 0;

	char letter;
	

	do {
		std::cout << "Enter a letter, or enter 0 to stop: ";
		std::cin >> letter;

		if ( !std::cin )
		{
			std::cout << "\nThat was not a letter, please try again.\n\n";

			std::cin.clear ( );
		}

		else if ( letter == '0' ) stop = true;

		else
		{
			sum *= 128;

			sum += letter;

			std::cout << "\nYou entered letter number " << int ( letter )
				<< ".\nThe current sum is " << sum << ".\n\n";
		}
	} while ( !stop );


	std::string word = "";

	while ( sum ) word = get ( sum ) + word;

	word = "\n" + word + "\n\n";

	std::cout << word;
}

char get ( int & num )
{
	for ( int i = 33; i < 127; ++i )
	{
		if ( ( num - i ) % 128 == 0 )
		{
			num -= i;

			num /= 128;

			return char ( i );
		}
	}

	num = 0;

	return '0';
}

edit: It does seem to have issues with words longer than 4 chars.
edit2: int overflows with more than 4 chars.
edit3: It can go up to 9 chars if you use an unsigned long long int.
Last edited on
Niccce!

this convert chars to int

so, how would you convert int to chars, is that gonna required lot of changes in the code?

because if that can be done, that would be awesome!
It does that. The last word it prints out was converted from an int, it's not just the previous letters put together.
Sweet!

thanks man
Topic archived. No new replies allowed.