Need help using references

For my program, I am currently stuck, I know what I want to do, but don't know how to translate it into code. I need help on how I would go about to separating the nice character string into three letter blocks.

For example "ABCDEFGHI"
codeA = ABC
codeB = DEF
codeC = GHI

I know that the at function should be used to get the individual characters. Then a for loop to add the character values together. I know characters ABC should yield the sum 198 and need to be referenced in codeA and equal X in main.

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
/*
Description: Using References
*/

#include <iostream>
#include <string>

using namespace std;

bool decode(string code, int &codeA, int &codeB, int &codeC);

int main()
{
	int x, y, z;
	string sequence;

	cout << "Enter a sequence to decode " << endl;
	cin >> sequence;
	decode(sequence, x, y, z);

	if (bool decode = 1)
	{
		cout << "Sequence was invalid" << endl;
	}
	else
	{
		cout << "Your sequence decoded is " << endl;
		cout << "Num 1 = " << x << endl;
		cout << "Num 2 = " << y << endl;
		cout << "Num 3 = " << z << endl;
	}
	return 0;
}

bool decode(string code, int &codeA, int &codeB, int &codeC)
{
	 codeA = 0;
	 codeB = 0;
	 codeC = 0;
	 

	 if (code.size() != 9)
	 {
		 return 1;
	 }
	 else
	 {

		 //this is where the decoding would take place

		 return 0;
	 }
}
Last edited on
Hmmm... Maybe this can help You, but it`s a bit different.
Question: how You will print string variables using int`s? X, y and z can`t take a string values. And one serious but sometimes hard to find error:
if (bool decode = 1) - this will NEWER give You a good result, should be:
if (bool decode ==1).
To the point:
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
#include <string>
#include <iostream>


using namespace std;

bool decode(string&, string arr[3]);

int main()
{
	
	string sequence;
	string arr[3]; //small array

	string x = "";
	string y = "";
	string z = "";

	cout << "Enter a sequence to decode " << endl;
	cin >> sequence;
	bool result = decode(sequence, arr);
	if ( result == 1)
	{
		cout << "Sequence was invalid" << endl;
	}
	else
	{
		for (string a : arr)
		{
			x = arr[0];
			y = arr[1];
			z = arr[2];
		}

		cout << "Your sequence decoded is " << endl;
		cout << "Num 1 = " << x << endl;
		cout << "Num 2 = " << y << endl;
		cout << "Num 3 = " << z << endl;
	}
		

	cin.ignore();
	cin.get();
}

bool decode(string &sequence, string arr[3])
{
	
	if (sequence.length() != 9)
	{
		return 1;
	}
	else
	{
		int i = 0;
		int tmp = 0;
		for (i = 0; i < 3; i++)
		{
			string decode = sequence.substr(tmp, 3);//divide et impera
			arr[i] = decode;
			tmp += 3;
		}

		return 0;
	}
}


I use Visual Studio 2013, everything works fine.
Topic archived. No new replies allowed.