Cant get this to increment array index number

Im trying to increment the arrays index number so it will loop through and change all the letters specified to numbers but it isnt working

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void ReplacingAlgorithym(string &tempTxtStorage)
{
    size_t pos = 0;
    int incrementAlpha = 0;
    int incrementNum = 0;

    bool ended = false;

    string alphabet[4] = {"a", "b", "c", "d"};
    string number[4] = {"4", "3", "9", "0"};


    //trying to get program to increment the array amount so it will loop through and replace all letters
    //But it will not work.
        while((pos = tempTxtStorage.find(alphabet[incrementAlpha], pos)) != string::npos)
        {
            tempTxtStorage.replace(pos, alphabet[incrementAlpha].length(), number[incrementNum]);
            incrementAlpha++;
            pos += number[incrementNum].length();
            incrementNum++;
        }
}
Do you want to replace every occurrence, of the letters in alphabet with the numbers at the corresponding index in number?

If so, check out below. You need just one index, so you don't have to synchronise 2. Also you need 2 loops, and you need to reset pos after the inner 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
30
31
void ReplacingAlgorithym(string &tempTxtStorage)
{
    size_t pos = 0;
    int incrementAlpha = 0;

    bool ended = false;

    string alphabet[4] = {"a", "b", "c", "d"};
    string number[4] = {"4", "3", "9", "0"};


    //trying to get program to increment the array amount so it will loop through and replace all letters
    //But it will not work.

    
    	while (!ended)
    	{    
	    	if ("d" == alphabet[incrementAlpha]) // you need a more generic check than this
	    	{
		    	ended = true;
	    	}
	    	
	        while((pos = tempTxtStorage.find(alphabet[incrementAlpha], pos)) != string::npos)
	        {
	            tempTxtStorage.replace(pos, alphabet[incrementAlpha].length(), number[incrementAlpha]);
	        }
	        
	        ++incrementAlpha;
	        pos = 0;
	    }
}
ys im trying to replace every occurance of every letter with the corresponding number. however the program will not get to the inner loop for some reason.
Last edited on
ok so i did this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
while(!ended)
    {
        if("d" == alphabet[incrementAlpha])
        {
            ended == true;
            break;
        }
        while((pos = tempTxtStorage.find(alphabet[incrementAlpha], pos)) != string::npos)
        {
            tempTxtStorage.replace(pos, alphabet[incrementAlpha].length(), number[incrementNum]);
            //pos += number[incrementNum].length();
        }
        ++incrementAlpha;
        ++incrementNum;
        pos = 0;
    }


and this works but it outputs the contents of the text file twice, why is that?
I'm not sure how you're running it, I tested the function I posted and it worked as expected.

What you've re-posted won't work.

line 5: "ended == true" has no effect in this context. You've merely compared two things and discarded the value.

line 6: "break" is entirely unnecessary and means you will always skip checking for "d"

since there are no output statements in the function, I don't know how you're accomplishing the output, so I can't comment on why it's happening twice.

And using two indexes that you're manipulating independently, that's going to trip you up eventually, get rid of one of them, unless youwant the same letter replaced by different numbers as time goes by
here is the function as i updated it

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
void ReplacingAlgorithym(string &tempTxtStorage)
{
    size_t pos = 0;
    //string LA = "a";
    //string newstr = "8";
    int incrementAlpha = 0;
    int incrementNum = 0;

    bool ended = false;

    string alphabet[26] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
    string   number[26] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "#", "*", "(", ")", "@", "!", "%", "<", ",", "'", ">", "|", "[", "]", "{", "}"};


    /*
    while((pos = tempTxtStorage.find(LA, pos)) != string::npos)
    {
        tempTxtStorage.replace(pos, LA.length(), newstr);
        pos += newstr.length();
    }
    */

    //trying to get program to increment the array amount so it will loop through and replace all letters
    //But it will not work.

    while(!ended)
    {
        if("z" == alphabet[incrementAlpha])
        {
            ended == true;
            break;
        }
        while((pos = tempTxtStorage.find(alphabet[incrementAlpha], pos)) != string::npos)
        {
            tempTxtStorage.replace(pos, alphabet[incrementAlpha].length(), number[incrementNum]);
            //pos += number[incrementNum].length();
        }
        incrementAlpha++;
        incrementNum++;
        pos = 0;
    }

    cout << tempTxtStorage << endl;
}


"line 5: "ended == true" has no effect in this context. You've merely compared two things and discarded the value."

oops, that was actually a typo, and it didnt work so i put break in there.
here is the entire program if it helps:

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
109
110
111
112
113
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
#include <stdlib.h>
#include "Prototypes.h"

using namespace std;

/*
File encryptor does exactly what its name implies, it encrypts text files.
*/

int main()
{
    string str;

    MainMenu(str);

    return 0;
}

void MainMenu(string &tempTxtStorage)
{
    CompressDocument(tempTxtStorage);
}

void CompressDocument(string &tempTxtStorage)
{
    ifstream openDoc;
    string fileName;
    tempTxtStorage;
    vector<string> storeTextFileContents;
    bool done = false;

    cout << "Enter the name of the text file to be encrypted" << endl;
    getline(cin, fileName);

    openDoc.open(fileName.c_str());

    while(getline(openDoc, tempTxtStorage))
    {
        ReplacingAlgorithym(tempTxtStorage);

        if(openDoc.eof())
        {
            done = true;
            cout << tempTxtStorage;
            cin.get();
            openDoc.close();
            break;
        }
    }
    cin.get();
}

/*

ReplacingAlgorithm()

This function Finds characters in the document and replaces them with symbols.
*/

void ReplacingAlgorithym(string &tempTxtStorage)
{
    size_t pos = 0;
    //string LA = "a";
    //string newstr = "8";
    int incrementAlpha = 0;
    int incrementNum = 0;

    bool ended = false;

    string alphabet[26] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
    string   number[26] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "#", "*", "(", ")", "@", "!", "%", "<", ",", "'", ">", "|", "[", "]", "{", "}"};


    /*
    while((pos = tempTxtStorage.find(LA, pos)) != string::npos)
    {
        tempTxtStorage.replace(pos, LA.length(), newstr);
        pos += newstr.length();
    }
    */

    //trying to get program to increment the array amount so it will loop through and replace all letters
    //But it will not work.

    while(!ended)
    {
        if("z" == alphabet[incrementAlpha])
        {
            ended = true;
        }
        while((pos = tempTxtStorage.find(alphabet[incrementAlpha], pos)) != string::npos)
        {
            tempTxtStorage.replace(pos, alphabet[incrementAlpha].length(), number[incrementNum]);
            //pos += number[incrementNum].length();
        }
        incrementAlpha++;
        incrementNum++;
        pos = 0;
    }

    cout << tempTxtStorage << endl;
}

void DecryptDocument()
{

}
While I haven't tried the program yet, I notice that on line 96 there is a single = that may be more appropriately replaced with ==.
"While I haven't tried the program yet, I notice that on line 96 there is a single = that may be more appropriately replaced with ==."

Why? it works just fine as it is?
also i found the problem, i was outputting it in another function i calle dit in as well.
Last edited on
Topic archived. No new replies allowed.