the code is supposed to take 1-10 char word and copy the word into another array.
it somewhat does that but some mysteries are included

#include <iostream>
using namespace std;

int main()
{
    char word[10];
    cin>> word;

    char *Arr;
     int index=0;

    for (int i=0; i<10; i++)
        if (word[i] >= 'a' and word[i] <= 'z')
        {
           index++;
            Arr=new char[index];
            Arr[index] =word[i];
            cout<< Arr[index]; //here it works somewhat fine except adding an extra 'x' at the end
        }
        cout<< endl;
for (int j=0; j<index; j++) //here it puts symbols
cout<< Arr[j];
        return 0;
}

1) why does it add an 'x'?
2) why does it write other symbols when i try to view what is the word outside the first cycle?

line 6: if you can enter 1-10 characters then that array isn't big enough -- need to make it size of 11 to hold the string null terminating character.

line 9: does your assignment require you to use dynamic memory allocation? If not, why are you using pointers ?

line 16: delete that line and allocate all the memory at one time on line 9 like this: char* Arr = new char[11];

thanks for replying,
i used dynamic memory because i wanted my new array to have no expand limitations.
Also you wrote char* Arr = new char[11];
does that mean that it'll hold up to a maximum of 11 characters, or can it expand once limit reached? and if i enter only 1 char, will that mean it's length will be considered same as char *Arr=new char[2] ?

does that mean that it'll hold up to a maximum of 11 characters

Yes and no. It will hold up to 11 chars if you want to. But if you want to use it as a string (word), it will hold 10 characters and an end-of-string character = '\0'.

or can it expand once limit reached?

No

and if i enter only 1 char, will that mean it's length will be considered same as char *Arr=new char[2] ?

strlen(arr) will only be one if arr[2] == '\0'. The length of a char array (string) is the number of chars preceding the '\0'

Hope this makes some sense .


ps. Also when using new, you will have to delete the memory you allocated at the end of the program.

Edited 6 Years Ago by Nick Evan: n/a

I know this is solved now but, i see you've got

if (word[i] >= 'a' and word[i] <= 'z')

I thought the "and" opperator was "&&" ?

made a few examples and understood. Thx for helping

to Black Magic: i'm using code::blocks and it allows using and/or, so you're free to choose the way you like better

You´re absolutely right. (although I think some compilers actually don't have a problem with this syntax, but I can't give an example)

[edit] Aha, code::blocks...

But to keep your program standard, you should use '&&' and not 'and'

or can it expand once limit reached?

If you want something that will auto-expand when the current limit is reached then use std::string. Otherwise you have to allocate a completly new string, which can be somewhat a pain in the a** to code correctly.

This question has already been answered. Start a new discussion instead.