Would anyone know how to put user input into a Char Array. I've provided the code below
It doesn't work so I've tried many variations like making char wrd as char wrd[20] but still nothing.
I think I'm missing a fundamental point here.

All I want to do is have a prompt which says please enter a word and then enter
three letters starting from the second character in char myarray[20]. I just want that arrary to receive
user input so I can manipulate the string later.

Any clues as to how to go about this? Thanks

   #include<iostream>
    #include<cstdlib>
    using namespace std;

        int main()
        {

        char wrd;
        cout << "Enter a word, any word \n";
        cin >> char wrd;

    // below is where I'm getting the problem.  I know this is wrong 
    // but I just can't figure out how to fix it.  
        char myarray[20] = wrd;
        cout << myarray[1] << "\n";
        cout << myarray[2] << "\n";
        cout << myarray[3] << "\n";
        system("pause>nul");
        }

What you have right now is a sigle letter. If you want to use a c-style string you need to define wrd like char wrd[80] where 80 is the number of charactures max that it can hold. You can change 80 to be what every you want but you want to make sure it is large enough to fit what you want to put into it. you also want to make sure you have enough room at the end for the null terminator that gets automaiticly instered to the end of the input.

int main()
{
    char word[80];  // this will hold 79 letters
    cout << "Please enter a word: ";
    cin >> word;
}

Thanks NathanOliver, that works. I took out line 14 in my code and it works fine.

What I'm trying to do, and this is a hypothetical scenario.
Can I make one Char array equal to another. I've done the below but it doesn't work.
See comments.

myarray = wrd; // right here,  this doesn't seem to work.  Even if I
               // chang it to myarray[80] = char word[80]  // also declaring
               // char myarray[80] at the top of main. 
cout << myarray[1] << "\n";
cout << myarray[2] << "\n";
cout << myarray[3] << "\n";
system("pause>nul");
}

No need to answer that. I've solved it.
It's using the below but you need to include <string.h>, on dev c++ just <string> doesn't seem to work. I've repasted the working code below for reference. strcpy(myarray,wrd);

#include<iostream>
#include<cstdlib>
#include<string.h>



using namespace std;
//using std::string;

int main()
{
char myarray[80];
char wrd[80];
cout << "Enter a word, any word \n";
cin >> wrd;
//alternatively you can use the below line instead of the line above.
//cin.getline(wrd, 80);

strcpy(myarray,wrd); // copying the contents from one Char Array to the Other.
cout << myarray[1] << "\n";
cout << myarray[2] << "\n";
cout << myarray[3] << "\n";
system("pause>nul");
}

It's using the below but you need to include <string.h>, on dev c++ just <string> doesn't seem to work.

That's because string is the header for the C++ string class. string.h (or more correctly in C++ cstring) is for c-strings.

Properly getting input

  1. Use a char array
  2. Use the get function to accept input
  3. Use the ignore function to remove extra input
    note : char array's are actually pointers, but the cout function derefferences automatically. This is important to know for passing the array to a function.

    char word[80]; // initialize an array of size

    cout << " Enter a word : ";

    cin.get(word, 80, '\n'); //get input until 80 or newline
    cin.ignore(100, '\n'); // safty

    cout << "The word you have entered is " << word;

char array's are actually pointers

This is a common misconception. An array is not a pointer, but when used in value context, the name of an array is converted to a pointer to the first element in the array. In object context, the name of an array is not converted, such as as an operand to the sizeof operator.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.