appending arrays

Pages: 12
The reason I use separate strings within the array "A","B","C" etc is so that when I input a number it can correspond to one of the letters via a number. If I make the array one string "ABCDEFG" then wont everything be considered 1? Or would I HAVE to make a multidimensional array?
"ABCDEFG" is a string literal that is read like an array of chars with a null char terminating..

const char chararray[] = "ABCDEFG";

is approximately equal to

const char chararray[] = {'A','B','C','D','E','F','G','\0'};

Note the extra char at the end.. if you output that as a number it's just 0, so you can just say:


const char chararray[] = {'A','B','C','D','E','F','G',0};

that means that
const char* chararray[] = {"A","B"};
is equal to
const char* chararray[] = {{'A','/0'},{'B','/0'}}; two arrays of chars, not chars


- edit: note that this is why we make arrays 1 larger for the string literal to catch the null terminator..
Last edited on
oh, you probably don't want to use the const... not sure why I put that there

... and a an array char *chararray[]; is multidimensional
Last edited on
I changed the loop to this and its funny because this is what I had before but I thought it would be easier to use a different iterator..
1
2
3
4
5
6
7
8
for(i = 1; i < 26; i = i +2)
            {
              
			    pletter2[i] = pphrase5;
				   cout <<pletter2[i];
             }
			
	    


The output for this looks like: xxxxxxxxxxxxxxxxxxxxx

The desired output is: AxCxExGxIxKxMxO etc.
you are outputting the char you just changed... the array is probably correct... i have a little work to do... i~ll be back in a bit if someone doesn~t answer first
So cranium your saying that if I take out all the quotes and make the array a single string it will have the same output?
... well, i have to wait... people!

either way...

pphrase5 should be a char not a char* and should = 'x' if you are going to do that...

right now you are setting the char pointers in your string array to point to where the x is... if this is an assignment, that will be really bad. you should be changing the value, not the pointer.

to change the value you actually have to do something like this
1
2
3
4
5
6
for(i = 1; i < 26; i = i +2)
            {
              
			    pletter2[i][0] = pphrase5[0];
				   cout <<pletter2[i]; // you can still do this though, it's just a char array
             }


actually before you do anything else, test this out...
1
2
3
4
5
6
7
char str[] = "hello world";

std::cout << str << std::endl;

str[0] = 'x';

std::cout << str << std::endl;


that little bit of code should show you that you can access that string just like an array of chars, because it is an array of chars...
I tried that for loop but it is saying there is an error on the pphrase5 line saying subscript requires array or pointer type?
because pphrase5 has to change type also... you are making an array of char so pphrase5 shouldn't be a char* and should be set to a char... if you already did that, then pprase5 needs to equal a char so "A" is a char array that equals {'A','\0'}, not a char

'A' is a char
Last edited on
I tried making the array all one string but it does not work.... unless I have to take away the * from everything? If I took the asterisk away wouldn't that take away the pointer definition from everything? I wanted to use pointers.
Sorry for being so complicated. I understand if I use char and not char* I can access the array differently but I am trying to use pointers for this assignment even though they are more confusing for me haha. Thanks for your help and explanations.

I understand the test replaces the first letter of hello world with x so the output is xello world because it made str[0] = 'x'; but that is with a char type and Im using char*.

I am just confused as to how i can replace letters within the array with x and still have the output show AxCxExGxIxKxMxOxQxSxUxWxYx rather than simply only showing xxxxxxxxxxxxxx ? Sorry again if I am being annyoing.
Ok so I was sitting here thinking and I came up with this.....
1
2
3
4
5
6
7
8
9
10
11
	for(i = 1; i < 26; i = i +2)
            {
              
			    pletter2[i] = pphrase5;
				
                  
				 for(int j = 0; j<26; j = j+2)

					 cout <<pletter2[j]<<pletter2[i];
			        
			}


The ONLY problem is it prints like 13 times and I don't know why? One thing after another.
FINALLY I don't know how I came up with this but I just did all by myself :D. With some help from you forum guys too >.<. Thanks. For some reason it printed 13 times which is half of 26 which is how many chars i have in my array. So I fiddled with the nested for loop but that changed how long the array printed horizontally not vertically. Then I fiddled with the first for loop and for some reason when I changed i<26 to i<2 it printed the correct output!.
1
2
3
4
5
6
7
8
9
10
11
for(i = 1; i < 2; i = i +2)
            {
              
			    pletter2[i] = pphrase5;
				
                  
				 for(int j = 0; j<26; j = j+2)

					 cout <<pletter2[j]<<pletter2[i];
			         cout <<endl;
			}

I am not sure how to do this with 1 instance of the array but I did it with 2 for now. If you could help with using 1 instance of the array I would be much obliged.
Topic archived. No new replies allowed.
Pages: 12