Appending pieces of a string results in a blank one

Feb 5, 2017 at 8:03pm
Okay, so I've been making a little code to teach myself about compression. Essentially, it compresses a strand of DNA (series of A, T, C, and G) and then assigns a value of 00, 01, 10, or 11 respectively. However, it seems like the final output, aka the cmpr string is blank. In my code, I have it cout both the i variable and the current status of the cmpr string just so i can see where I am going wrong, and the i variable seems fine, but the cmpr never changes from a blank line. I know the code is super messy and probably quite inefficient, but I just typed it up pretty quick. Hope its readable. Thanks in advance!

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
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;
using std::string;

void codeDown(int i, char* dnaStrand)
{

	if (i = 1)
	{
		if (dnaStrand[i] == 'A')
		{
			string cmpr = "";
		}

		if (dnaStrand[i] == 'T')
		{
			string cmpr = "01";
		}

		if (dnaStrand[i] == 'C')
		{
			string cmpr = "10";
		}

		if (dnaStrand[i] == 'G')
		{
			string cmpr = "11";
		}
	}

	else
	{
		if (dnaStrand[i] == 'A')
		{
			string cmpr = cmpr + "00";
		}

		if (dnaStrand[i] == 'T')
		{
			string cmpr = cmpr + "01";
		}

		if (dnaStrand[i] == 'C')
		{
			string cmpr = cmpr + "10";
		}

		if (dnaStrand[i] == 'G')
		{
			string cmpr = cmpr + "11";
		}
	}
}

int main()
{
	char dnaStrand[5];

	std::string cmpr;

	int i;

	cout << "Gimme the dna (testing with 4 first)" << endl;
	cin.getline(dnaStrand, 5);
	
	cout << dnaStrand << endl;

	for (i = 1;i <= 4;++i)
	{
		codeDown(i, dnaStrand);
		cout << i << endl;
		cout << cmpr << endl;
	}
	
	cout << cmpr << endl;

	system("pause");

	return 0;
}
Feb 5, 2017 at 8:37pm
The cmpr that exists in main is never modified by your code. A different variable with the same name in another function is... different.

Also, line 11 is incorrect. = is assignment. == is comparison.

What you are doing is not compression. You're using twice as much memory to encode the same sequence which is the opposite of compression.
Feb 5, 2017 at 8:50pm
I see, so cmpr is specific to main. And to fix that, would i specify cmpr in the codeDown prototype? And yes, I know the program itself takes up much more data, I'm just trying to understand the idea of compression, like in the Huffman algorithm.
Feb 5, 2017 at 11:47pm
And to fix that, would i specify cmpr in the codeDown prototype?

You could take a string by reference in codeDown, but you wouldn't be specifying cmpr in the prototype - you would feed it to the function at the time that you call it. Even then, there are other problems with your code. You are encoding the "index" i which I hesitate to call in index, because it doesn't index the first character of the string and does index the nul-character ending it.

Making a design change might be more appropriate.

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
#include <iostream>
#include <string>

std::string encode(char ch) {
    switch (ch) {
    case 'A': return "00";
    case 'T': return "01";
    case 'C': return "10";
    case 'G': return "11";
    default: return "";
    }
}

int main()
{
    const std::size_t strand_size = 5;

    char dnaStrand[strand_size];

    std::cout << "Gimme the dna (testing with 4 first)\n";
    std::cin.getline(dnaStrand, strand_size);

    std::string compressed;
    for (std::size_t i=0; i< strand_size; ++i)
        compressed += encode(dnaStrand[i]);

    std::cout << compressed << '\n';
}
Topic archived. No new replies allowed.