Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to use cin

+3 votes
asked Apr 21, 2022 by Janciel Carandang (150 points)

4 Answers

0 votes
answered Apr 22, 2022 by KAMLESH SANJAY BAVISKAR (180 points)
cin Is the object of input stream (ostream).

we use cin for taking input from user.

ex.,

cin>>var_name;
+1 vote
answered Apr 22, 2022 by Peter Minarik (84,720 points)
edited Apr 23, 2022 by Peter Minarik

Please, consider the code below.

You can redirect the input into any variable you want (that is supported by the >> operator).

#include <iostream>
#include <string>

int main()
{
    std::string name;
    std::cout << "Please, enter your name: ";
    std::cin >> name;
    
    int age;
    std::cout << "Please, enter your age: ";
    std::cin >> age;
    
    std::cout << "Hello " << name << ", " << age << " years old.";

    return 0;
}
0 votes
answered Apr 22, 2022 by Ajay Manivannan (140 points)
#include <iostream>
using std::cout;
using std::cin;
int main()
{
    int num;
    cout << "Enter a Number : ";
    cin >> num;
    cout << "You Entered " << num;

    return 0;
}

or

#include <iostream>

using namespace std;
int main()
{
    int num;
    cout << "Enter a Number : ";
    cin >> num;
    cout << "You Entered " << num;

    return 0;
}
0 votes
answered Apr 25, 2022 by userdotexe (1,340 points)

First example (waits until you enter something):

#include <iostream>

using namespace std;

int main()
{
    string INPUT;
    cin>>INPUT;
    return 0;
}

Second example (whatever you enter first it takes):

#include <iostream>

using namespace std;

int main()
{
    string INPUT;
    getline (cin, INPUT);
    return 0;
}

Between the two of them, I only changed 1 line.

:)

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...