I am quite new to C++ (just a shaky background in Java) and I'm stumped about how to print out the entire contents of a char array. I believe I need to use a loop, and base the loop on the length of the array, but my attempts to compile aren't meeting with success. This is what I have right now. Thanks in advance for your help!
#include <iostream> #include <string> using namespace std; void namePrinting(char name[]) { int i = 0; cout << "Name: "; while(i <= name.length() ) { cout << name[i]; i++; } } int main() { string fullName; cout << "Enter name: "; cin >> fullName; char nameArray[fullName.length()]; namePrinting(nameArray); }
std::string.cout<<fullName;operator<<to print out a complete NUL-terminated string.char nameArray[fullName.length()];it is not legal. The length of the array should be known at compile time. You need to allocate dynamically your array (usingnew)