Class and Getline problems

Pages: 12
Hellow, i am writing a program that will decode and encode messages. (A = 1, B = 2, space = 0, etc.) I am having trouble with my encode, specifically inputting the string and then converting it. I think i have my function right, but do i need a loop and/or convert to array? Also, my decode function is outputting only question marks, i don't know why.

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// LetterCode.cpp : main project file.

#include "stdafx.h"
#include "LetterCodeLogic.h"
#include <iostream>
#include <string>
#include <vector>


using namespace std;
using namespace System;

void getEncodeString();
void getDecodeString();


int main()
{
	char choice;
	
	cout << "Welcome to my LetterCode program" << endl;
	do {
		cout << "Please input a choice, either encode, decode, or quit (E/D/Q): ";
		cin >> choice;
		if (!cin.good())
		{
			cin.clear();
			cin.ignore(1000, '\n');
			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 statement


	} while (toupper(choice) != 'Q');

	cout << "Thank you for using my program" << endl;
	

	system("Pause");

    return 0;
}
void getEncodeString()
{
	//obtain input as string
	//call LetterCodeLogic for encoding
	//cout << "Function not yet available" << endl;

	string m;
	cout << "Please enter your message to be encoded, use spaces between words: " << endl;
	cin.ignore();
		getline(cin, m);
		

		cout << LetterCodeLogic::Encode(m) << endl;


	return;

}
void getDecodeString()
{
	vector<int> letters;
	unsigned int x;

	cout << "Please enter your 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, '\n');
			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 LetterCodeLogic
		cout << LetterCodeLogic::Decode(letters) << endl << endl;
	}
	
	return;
}


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
67
68
69
70
71
72
73
74
// lettercode class
#include "stdafx.h"
#include "LetterCodeLogic.h"
#include <string>
#include <vector>
#include <sstream>

using namespace std;
using namespace System;



string LetterCodeLogic::Encode(string m)
{
	//process character of string
	//use int = char feature to get value (w/offset)
	//space is ASCII 32

	int c;
	unsigned int i;
	stringstream result;

	for (i = 0; i < m.length(); i++)
	{
		     if (m.at(i) == ' ')
		{
			c = 0;
		
		}
			
		else
		{
			if (m.length() > 0)
			{
				m.at(i) = toupper(m.at(i));
				c = (((int)(m.at(i))) - 64);
			}
		}

		result << c<<" ";
	}

	return result.str();
}

string LetterCodeLogic::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
				c = '?';
			}
			else
			{
				//convert to char based on ASCII coding 
				c = (char)(letters[i] + 64);
			}
		}
		result << c;
	} // end of for loop
	return result.str();
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// letter code header
#pragma once
#include <string>
#include <vector>

using namespace std;
using namespace System;

class LetterCodeLogic
{
public:
	
	static string Encode(string m);
	static string Decode(vector <int> letters);

private:

};
Last edited on
Fixed the decode part, easy fix! Still working on the encoding though.
Got the getline to work, now onto the function in encoding. If I take out the if statement, lines 73-76, excluding the cout then I get an output to the console. Not what I want, however, I get 48 repeating many times. It isn't an infinite loop.
Last edited on
closed account (48T7M4Gy)
try m.length() instead of m.length at line 73
Yea, I realized that and fixed it. I now don't get any compile errors and my encode returns a value, but it is always in the form of so many '48' repeating.
closed account (48T7M4Gy)
Hint: ASCII 48 is zero 0 which probably means your if statement at line 25 is wrong.

(Either that or you have just invented the first completely in breakable code)
Last edited on
Line 25 in my LetterCode Class? ...safe to assume I didn't create the first in breakable code...
closed account (48T7M4Gy)
yep that's the one
I took out that if statement for now; I am still getting wrong output (excluding space errors). I am not converting it right obviously, but can anyone point me in the right direction?
closed account (48T7M4Gy)
Ok before you start building camels, use == instead of =

Do that before you start making changes. I can't guarantee it will work but at least the statement will be right.
Ah, embarrassing. That seems to be a common mistake I need to fix while coding mixing up those two. Thank you for pointing that out.
Almost done, i am getting a correct output except for the spaces problem. I get the ASCII char for space, 48, instead of a space. Should I check for spaces after I use the function to convert?
closed account (48T7M4Gy)
yes - isn't that what you have commented out above?
Yes, when i un-comment that is when I get the '48' instead of a 0 for the space.
Got it, easy fix. Thanks again
Last question; how would I insert a space into the output of the string of numbers from the encode function? ex) instead of 51023405897 it would be 5 1 0 23 4 0 5 8 9 7 (zeros still representing spaces)

Fixed that, sorry for the numbers of posts by the way. How should I start validation of the input string in the encode part? I don't want numbers.

Ill update my code in a minute also.
Last edited on
closed account (48T7M4Gy)
I don't quite follow. Are you saying there is a conflict with 0s and spaces because a space always encodes as a zero?

If that's the case then you have a problem because when you decode it there is no way to differentiate between the two. One way is for the encoder to completely ignore spaces?

There are plenty of other alternatives but they would be complicate - when a space is detected encrypt the word 'space' into the cypher text. Or use a more comprehensive alphabet and include symbols.

Keep in mind that a Caesar code is easily cracked anyway so the simplest is the best answer. I,d ignore spaces and live with the fact that the decoded text is slightly, but not impossible, hard to read.
I fixed the zero/space problem i was having. How can i check the input string for numbers, or only having letters and spaces? Actually, to be more specific, I need to change the incorrect characters (not letters or spaces) into a '99'. I am lost on how to start doing this for a string.
Last edited on
closed account (48T7M4Gy)
isalpha() check it out in the reference section here. I think the other one is isdigit()
Last edited on
What headers or includes do I need for isalpha() ? My compiler isn't recognizing it as anything.
Pages: 12