In the code below both blue cout statements should produce the same result because in both cases name[] variable is being worked on. But you see in one case I get this "¶". Why is so? Please let me know. Thanks.
When a string is entered using cin a space is considered to be end of a string. While entering the name using the for loop I inserted two spaces but they were ignored. Why is so?
Please help me. Thanks.
// 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>
#include <string>
using namespace std;
const int C = 20;
int main()
{
int i;
char name[C];
string urname;
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. ";
[COLOR="blue"]for (i=0; i<C; i++)
{
cout << name[i];
}[/COLOR]
cout << endl;
[COLOR="blue"]cout << "your name is: Mr. " << name << endl;[/COLOR]
cout << "\nenter your name: ";
cin.get(name, C);
cout << "enter your name: ";
getline(cin, urname);
cout << "your name is: Mr. " << name << endl;
system("pause");
return 0;
}
OUTPUT
enter your name: jackson your name is: Mr. jackson enter your name: j a c k s o n [COLOR="red"]//space[/COLOR] h e i g h t s [COLOR="red"]//space[/COLOR] 0 0 0 0 0 0 your name is: Mr. jacksonheights000000 your name is: Mr. jacksonheights000000[COLOR="red"]¶[/COLOR] enter your name: jackson heights enter your name: your name is: Mr. jackson heights Press any key to continue . . .


Sign In
Create Account


Back to top











