Need Help With Char Vectors

Pages: 12
I'm having trouble with casting int's into char's for some reason in my vector. Whether this is the casting itself i'm not quite sure, i think it's more todo with the scope but i still need HELP!

Basically this program imitates the stereo-typicle visual output of a password cracker but i need help with something.
This program could be used for a game or such, but for now i just want to get the letter cycling working properly and it's running through a console but i'm having a little trouble.

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
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

short i;
short temp = 32;
short k;
vector<char> word;

int main()
{
    word.push_back(' ');
    while(true){
        i = ( word.size() - 1);
            while(temp < 127){
            word[i] = static_cast<char>(temp);
            for(short a = 0; a < word.size(); a++)
            cout << word[a];
            cout << "\t\n";
            temp++;
        }
        
        
            k = static_cast<short>(word[i]);
            if(k == 126){
                word[i] = ' ';
                if(i == 0)
                    word.push_back(' ');
                else
                i--;
            }
            else{
                k++;
                word[i] = static_cast<char>(k);
                temp = 32;
                }
            system("pause");
        }
}


I added a console pause at the end so i could see what happened on out put and the 1st loop it cycles through all letters+symbols, 2nd loop it adds SP ' ' to the front which i expect and then cycles through all letters on right hand side, but on the 3rd loop the SP ' ' DOES NOT change to the '!' like i expect.

I'm not sure why this is happening and i've deliberately made most my variables global because earlier i had problems due to the scope.
Does anybody know what the problem is with my program and possibly a Simple way that this may be fixed please? I'm hoping that i may have just made a simple syntax error or such that i've not noticed.

Thanks to anyone that may be able to help.
Last edited on
So, do you want your output to look like this on the third try?:

!
!!
!"
!#
and so forth...

And this on the fourth try?:

!"
!"!
!""
!"#
there is no real reason to use a vector of chars. strings are more specifically designed for words and are a better idea for this.

also it is VERY bad practice to declare global variables.

seconds, static_cast conversions are not the safest thing to do and i would suggest changing this to make your program more stable, although it will generally run fine without it.
ui uiho
I am using the vectors because the string is an array which has a set number of fields whereas in a vector i am able to pushback as many fields as i like...

I am also aware that declaring global variables is considered bad practice however in this program there is no conflict from other classes so it doesn't matter...

And how would you suggest i change my charecters into integers so i can increment the ANSII code rather than using the casting method?



Kazekan
The program output should look like this:

3rd try:
!
!!
!"
!#

4th try:
"
"!
""
"#

etc... etc...
but unfortunately like i said in description it pushes back and does NOT increment with each loop like it should
Last edited on
Is this what you wanted? :) Once 'k' maxes out, it will restart with no space in the vector and do it all over again.

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
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;



int main()
{

    short i;
    short temp = 32;
    short k=temp;
    vector<char> word;

    word.push_back(' ');

    while(true)
    {
        i = ( word.size() - 1);
            while(temp < 127)
            {
                word[i] = static_cast<char>(temp);

                for(short a = 0; a < word.size(); a++)
                {
                    cout << word[a];
                }
                cout << "\t\n";
                temp++;

            }


        if(word.size()==1)
            word.push_back(' ');

        else if(k == 127)
        {
            word.pop_back();
            temp=32;
            k=temp;
        }
        else
        {
            word[i-1] = static_cast<char>(k);
            k++;
            temp = 32;
        }
        system("pause");
    }
}
Last edited on
Not really...

at the bottem you've made it so if there's only 1 char it'll add to make 2 but no more than that which is a waste of time to cycle through each time when it's only used once...

i dont really need k to reset because it'll set to word[i] when the program loops (i just use it to find out what word[i] is and then if it's maxed goto next letter/new letter, if it's not then add 1 to word[i] and reset temp to re-enter initial loop at top)

and word.pop_back() deletes the last vector field doesn't it?!...



If anything this is much worse but i do appreciate you trying to help!
but for now i just want to get the letter cycling working properly and it's running through a console but i'm having a little trouble.



It's doing exactly as you wanted. It's cycling correctly. Maybe you should be a bit more clear about how you want it displayed on the console, or what your ultimate goal is so that people can give you what you want past the 3rd or 4th pass.

word.pop_back() deletes the 2nd space in the vector so that it starts over again. If you instead wanted a 3rd space in the vector, and for it to continue cycling then just say so and it can easily be changed.

I am using the vectors because the string is an array which has a set number of fields whereas in a vector i am able to pushback as many fields as i like...

I am also aware that declaring global variables is considered bad practice however in this program there is no conflict from other classes so it doesn't matter...

And how would you suggest i change my charecters into integers so i can increment the ANSII code rather than using the casting method?

ok strings are char arrays BUT they have no determined length...
so you can do
std::string myname; and your name can be as long your ram can hold. ALSO you do not need to use static cast to change char values. think if it like a character line. try this program.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string> 

int main(){
    std::string words = "!"; 
    
    for(int i = 0; i < 10; i++){
        words += (words[i] + 1);
        std::cout << words << '\n'; 
    }
    return EXIT_SUCCESS; 
}

as you can see, i can make the length of the string as long as i want and change the characters with just a +1 instead of using static_cast which can be very unstable.
Right...
I want the program to cycle though the charecters and when it reaches the end clock up the next chatecter.

So it'll do SP through to ~ then add a new character and do SP SP to eventually ~ ~

It'll do this...

Sp
!
"

Etc...

Sp sp
Sp !
Sp "

Etc...

But it won't continue onto this...

! Sp
! !
! "




This is what u need help with please, it doesn't count up the charecters on the left after they've been through loop. Hopefully this makes more sense and you are able to help?
After it cycles through to ~~, what do you want it to do? Mine continues on to:

!Sp
!!
!"
etc...

just fine, but I'm unsure of where you want it to go when it's finished with ~~. I simply had it start all over again, but if you want to just start on a 3rd line then that's easy enough. Do you want this:

Sp
!
"
...

Sp Sp
Sp !
Sp "
...

...
~|
~}
~~

then

Sp Sp Sp
Sp Sp !
Sp Sp "

Sp ! Sp
Sp ! !
Sp ! "

????
YES!
this is what i want it to do...
I want it to cycle all the way through to ~~ and then add a new character to goto SP SP SP, but i'm not wanting to be given a code...

I want to know what's wrong with my program and fix it myself if possible!
If you're able to give me suggestions though it would be much appreciated and if i still cant figure it out then i'll keep you posted.
Last edited on
as i already showed, using strings and no static_cast along with removing the system pause and having a return value are all good starts.
Sorry ui uiho, I must have missed your previous message...
I do believe I've tried adding onto a string which does not work, although it has no determined length, neither does an array but it stays constant to the first legnth its given!
And i've tried simply adding 1 to a charecter (in seperate programs) but both of these crashed my program...
However I am willing to give it a try and thanks for your help.

And i added the system("pause"); simple so i could see what the program was doing on each loop.

Keep on it however just incase it doesn't work, thanks!
Last edited on
may i start again??
I am not looking directly for a solution (though that would be much appreciates), i am asking why isn't my CURRENT program working as i expect it to?
Is it a scope problem? a casting problem? etc...

pasted code from above for convenience

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
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

short i;
short temp = 32;
short k;
vector<char> word;

int main()
{
    word.push_back(' ');
    while(true){
        i = ( word.size() - 1);
            while(temp < 127){
            word[i] = static_cast<char>(temp);
            for(short a = 0; a < word.size(); a++)
            cout << word[a];
            cout << "\t\n";
            temp++;
        }
        
        
            k = static_cast<short>(word[i]);
            if(k == 126){
                word[i] = ' ';
                if(i == 0)
                    word.push_back(' ');
                else
                i--;
            }
            else{
                k++;
                word[i] = static_cast<char>(k);
                temp = 32;
                }
            system("pause");
        }
}


I am expecting this program to do this...
1
2
3
SP
!
" 

......
1
2
3
4
5
~

SP SP
SP !
SP " 

......
1
2
3
! SP
! !
! " 

......
1
2
3
4
~ ~

Sp Sp SP
Sp Sp !


I think you should get the picture by that (hopefully)
I want to know why my program DOES NOT do this, instead it get's stuck on 2 letters with the letter on the right looping as it should but the letter on the left stuck on SP...
WHY IS THIS, and how might i go about fixing it so my program can cycle through all combinations AND add letters when it reaches final loop with the number of letters it's currently on???
Last edited on
A string is not an array. It's a string. Regardless of its implementation, it provides any standard string operations, including push_back (or, more logically, appending with the "+=" operator).

I'm not even sure what you're code is supposed to do. How do you expect it to ever go beyond size 2?

Line 37 adds a letter slot, but only if size == 1.

Aside from that, your code flow just doesn't make sense. You want to iterate over a variable number of letter slots, but instead, you do some odd stuff. I'm not even sure why there's a pop_back() in there anyway. Nowhere did you give a reason why you would want to remove a letter from the back.
Whoops...
This isn't my code, this is someone else's code that tried to help but I spotted the same obvious errors you have...
Give me a moment i'll replace it with my code...

EDIT: OK, i've replaced it with my code, perhaps now you could take a look for me and possibly help me figure out why its not doing what i expect?
Last edited on
The pop_back may be gone, but the structure is still bogus. Why not simply use a control loop?

As far as I can see, all you need is three loops:
1) Word size
2) Character slot
3) Character

Loop 1 just increases the word size (push_back(' ') or whatever you want to use). Loop 2 runs from i[word.size()-1 -> 0] and resets 'temp' (or 'k'? I'm not sure). Loop 3 is the loop you've got on lines 17 to 23.

Start from that structure instead of this odd construct.
OMG i've done it!!! :D
Although not an awful lot of help was passed around i do Really appreciate anyone who tried to help.
I guess if there was a main contributor it would have been Gaminic, just through helping how to re-order the program so thanks to you.
But still a great thanks to anyone who tried to help!

and here's my new and improved code that performs EXACTLY how i wanted it to!!

(btw while i was redo-ing my code i added comments all throughout it to make sure i wasn't missing anythin)

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
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<char> word;
    word.push_back(' '); //To add first field to vector before while loop
    
    short asSet = 32;
    short asGet;
    short pos; //used to index position of word[]
    
    while(true){
        pos = 0;
              
        while(asSet < 127){ //To cycle through 1st number
            word[0] = static_cast<char>(asSet); //Set word[0] to asSet ASCII
            for(short a = 0; a < word.size(); a++) //output whole word to console
                cout << word[a];
            cout << endl;
            asSet++; //increment ASCII code for word[0]
        }//CLOSE while(temp < 127)
        
        
        while(asSet != 32){
            asGet = static_cast<short>(word[pos]); //gets ASCII code for word[pos]
            if(asGet == 126){ //if ASCII for word[pos] is maxed
                word[pos] = ' '; //reset this to SP (ASCII=32)
                if(pos == (word.size() -1)) //if this is last character (i.e last combination)
                    word.push_back('!'); //add another charater for new combinations
                else //if this is NOT last character
                    pos++; //move to next character for testing
            }//CLOSE if(asGet == 126)
            
            else{ //if ASCII for word[pos] is NOT maxed
                word[pos] = static_cast<char>(asGet +1); //increment it's current ASCII value
                asSet = 32; //reset iterater for word[0] to exit loop and re-enter top loop
            }//CLOSE else
        }//while(asSet != 32)
    }//CLOSE while TRUE
}


Once again thanks to anyone that tried to contribute, goodbye!
Woulda been a lot easier to help given a more detailed explanation of what you wanted.
why would you not use strings?
Pages: 12