Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using cout to print the entire contents of a character array

Tags:

c++

arrays

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);
}
like image 307
Chandy Avatar asked Oct 15 '13 01:10

Chandy


People also ask

How do I print a whole character array?

Use printf With %s Specifier to Print Char Array in C If we add the null byte at the end of our char array, we can print the whole array with a single-line printf call.

Can you cout an array?

We use the loop to cout our array; this makes our code short and saves our time and space. Now we can see that we initialized a long array with the length of 10 and assigned members at each index. Then we write a loop, and the limit of the loop is the same as the limit of the array in the main body of the code.

Can you cout a char?

We can use cast type here, by casting into char we are able to get result in character format. We can use cout<<char(65) or cout<<char(var), that will print 'A'. (65 is the ASCII value of 'A'). Here, output will be A.


2 Answers

Start with something simple:

char c_array[3];
c_array[0] = 'a';
c_array[1] = 'b';
c_array[2] = 'c';

for(int i=0 ; i<3 ; ++i)
{
  cout << c_array[i];
}
cout << endl;

Do not go farther until you understand that much perfectly. Now notice that if you null-terminate the array, you can pass the whole thing to cout, and operator<< will know when to stop:

char c_array[4];
c_array[0] = 'a';
c_array[1] = 'b';
c_array[2] = 'c';
c_array[3] = 0;

cout << c_array << endl;

You cannot do that with arrays of most other types. Now notice that you can assign a char[] this way, and it will be null-terminated:

char c_array[20] = "abc";
cout << c_array << endl;

You can even omit the size of the array, and the compiler will infer it:

char c_array[] = "abc";    // this is a char[4];
cout << c_array << endl;

There are a couple of different ways to read user input into an array, but it sounds as if you know that already, and this answer is getting long.

like image 52
Beta Avatar answered Sep 29 '22 22:09

Beta


Writing each character individually using operator<<(char) is inefficient.

Converting to an std::string using the (const char*, size_t) constructor, and writing that using operator<<(const std::string&) is also inefficient.

The proper way is simply to use http://en.cppreference.com/w/cpp/io/basic_ostream/write

PS: Note that your code is not valid C++. char name[] is basically synonymous with char* name and doesn't know its length (and there's no .length() on it too). And your nameArray is not initialized. Sized, yes; initialized, no. You're missing a std::copy or strncpy call to copy the content of fullName into nameArray.

like image 35
ddevienne Avatar answered Sep 29 '22 22:09

ddevienne