Need help decoding a string

For my program, I am currently trying to figure out how to determine the length of the string the user imputed. The string must be nine characters long and the function needs to return a true of false based on the string length. Here is what I have so far for my project.

A little background on my program.
For this program I am taking in a nine character string and decoding them into three separate sets and summing their ascii values.

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
/*
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 = false)
	{
		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;
	 bool codelength;

	 if (code != '9')
	 {
		 codelength = false;
	 }
	 else
	 {

	 }

	 return codelength;
}
You'll have to modify for your function

1
2
3
4
5
6
7
8
if (sequence.size()!= 9)
{
return 1;
}
else
{
return 0;
}

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

use code.size(); to get the size of the string parameter code. For more string methods see here:

http://www.cplusplus.com/reference/string/string/

You cannot do this:
if (code != '9') code is a string object and '9' is a char literal. This is a type mismatch.
Last edited on
Thank you so much for the help. Another question I have is, how would I change 0 and 1 to (true or false)
closed account (E0p9LyTq)
1
2
3
4
5
6
7
8
if (sequence.size()!= 9)
{
return true;
}
else
{
return false;
}
Topic archived. No new replies allowed.