Q:

C++ user input

#include <iostream> 
using namespace std; 
  
int main() 
{ 
    int age; 
  
    cout << "Enter your age:"; 
    cin >> age; 
    cout << "\nYour age is: " << age; 
  
    return 0; 
} 
3
#include <iostream>
int main(){
  std::string firstname; //variable created as a string
  std::cout << "What's your first name\n";
  std::cin >> firstname;//asking for the users' first name
  std:: cout << "Hello " << firstname
}
//Works for anyone, don't need any packages, just type this is in and run it.
5
int x; 
cout << "hurry, give me a number!: "; // Type a number and press enter
cin >> x; // Get user input from the keyboard
cout << "you picked: " << x << " !" // Display the input value

OR use:
getline >> (cin, variable-name);
instead of 
cin >> x; 
  
6
// 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

New to Communities?

Join the community