C++ में cin और double greater than >> की help से user input को capture किया जाता है। आप जिस type की value keyboard से enter करेंगे automatically आपको उसी type में मिल जायगी।

C++ user input example
CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;
int main() {
  int number1, number2;
  cout << "Enter first number : ";
  cin >> number1;
  cout << "Enter second number : ";
  cin >> number2;
  cout << "Addition : " << number1 + number2;
  return 0;
}
Output
Enter first number : 12
Enter second number : 23
Addition : 35

same इसी तरह से आप string या other type की value भी capture कर सकते हैं।

हालाँकि , अगर आप space देते हैं तो वो capture नहीं हो पाएगा , सिर्फ वही input मिलेगा जहाँ तक space नहीं है -

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;
int main() {
  string full_name;
  cout << "Enter full name : ";
  cin >> full_name;
  cout << "Fullname : " << full_name;
  return 0;
}
Output
Enter full name : Rahul kuamr
Fullname : Rahul

C++ getline()

तो जैसा कि आप example में देख सकते हैं कि , space के बाद का input capture नहीं हुआ है , इस problem को resolve करने के लिए हम getline() function का use करते हैं।

C++ getline() example
CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;
int main() {
  string full_name;
  cout << "Enter full name : ";
  // use getline() to capture whole input line.
  getline(cin,full_name);
  cout << "Fullname : " << full_name;
  return 0;
}
Output
Enter full name : Rahul Kumar
Fullname : Rahul Kumar

I Hope, अब आपको C++ में user input अच्छे से समझ आ गया होगा।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers