Q:

how to get input from the console in c++

/*there are 2 ways of doing it.*/
#include <iostream> // including the main thing needed
int main(){
  std::cout << "Text here.";
  //you could put using namespace std; so you just have to do
  cout << "Text Here.";
  //this isnt reccomended though.
  printf("hi");
  //is also an option.
  return 0;
}
1
// cin with strings
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string mystr;
  cout << "What's your name? ";
  getline (cin, mystr);
  cout << "Hello " << mystr << ".\n";
  cout << "What is your favorite team? ";
  getline (cin, mystr);
  cout << "I like " << mystr << " too!\n";
  return 0;
}
6
int age;
cin >> age;
2

New to Communities?

Join the community