Hi
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.
OUTPUTCode:// 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. "; for (i=0; i<C; i++) { cout << name[i]; } cout << endl; cout << "your name is: Mr. " << name << endl; 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; }
Code:enter your name: jackson your name is: Mr. jackson enter your name: j a c k s o n //space h e i g h t s //space 0 0 0 0 0 0 your name is: Mr. jacksonheights000000 your name is: Mr. jacksonheights000000¶ enter your name: jackson heights enter your name: your name is: Mr. jackson heights Press any key to continue . . .



LinkBack URL
About LinkBacks




