Hi
1: I don't understand how one could enter the all the elements on a character array with one cin. This seeming works as is shown in the OUTPUT code at the bottom.
One need to use cin.get() when there is a white space between the elements because white space is taken to be a null character which signals the end of the string.
2: Why doesn't the for loop work? Why do I have to enter the array element in a column form?
3: Is "C" in cin.get() always the size of the array?
Code:
// read_your_name.cpp
// read and print your name using an array and using
// cin, for loop, cin.get()
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
const int C = 20;
int main()
{
int i;
char name[C];
cout << "enter your name: ";
cin >> name;
cout << "your name is: Mr. " << name << endl;
cout << "enter your name: ";
for (i=0; i<C; i++)
{
cin >> name[i];
cin.ignore();
}
cout << "your name is: Mr. ";
for (i=0; i<C; i++)
{
cout << name[i];
}
cout << "\nenter your name: ";
cin.get(name, C);
cout << "your name is: Mr. " << name << endl;
system("pause");
return 0;
}
OUTPUT
Code:
enter your name: jackson
your name is: Mr. jackson
enter your name: j
a
c
k
s
o
n
1
1
1
1
1
1
1
1
1
1
1
1
1
your name is: Mr. jackson1111111111111enter your name: jackson heights
your name is: Mr. jackson heights
Press any key to continue . . .