#include <iostream>
#include <string>
using namespace std;
int main() {
string last, first, middle;
int age;
string birthday;
string gender;
string address;
cout << "Enter you lastname: ";
cin >> last;
cout << "Enter you firstname: ";
cin >> first;
cout << "Enter you middlename: ";
cin >> middle;
cout << "Enter your birthday: ";
cin >> ws;
getline(cin, birthday);
cout << "Enter your age: ";
cin >> age;
cout << "Enter your gender: ";
cin >> gender;
cout << "Enter your permanent address: ";
cin >> ws;
getline(cin, address);
cout << "Hello " << first << " " << middle << " " << last << endl;
cout << "Your birthday is " << birthday
<< " and now you are " << age << " years old." << endl;
cout << "You are " << gender << endl;
cout << "and you live at " << address << endl;
return 0;
With explanation
#include <iostream> // library to work with console input/output.
#include <string> // library to work with strings.
// Here we say to the programm we use the SDT namespace.
// That means we don't need to write it before std methods.
// e.g: std::cin >> ... || std::cout << ...
using namespace std;
// Main function. From here our program starts working.
int main() {
string last, first, middle; // Declare string variables.
int age; // Declare an integer variable.
string birthday; // Declare a string variable.
string gender; // Declare a string variable.
string address; // Declare a string variable.
// Here we ask the user's input.
// "cout" writes a text to the console, but "cin" accepts the user's input.
cout << "Enter you lastname: ";
cin >> last; // save the user's input into the string variable.
cout << "Enter you firstname: ";
cin >> first; // again...
cout << "Enter you middlename: ";
cin >> middle; // again...
cout << "Enter your birthday: ";
cin >> ws; // and again.
//The "ws" variable is not declared before and it's never used later. Maybe it should be removed?"
// Don't be scared, this function does the same: accepts user's input.
// There are 2 arguments:
// cin - the stream from where to get data;
// birthday - string variable where to save incomming data.
getline(cin, birthday);
cout << "Enter your age: ";
cin >> age; // save the user's input into the integer variable.
// If you enter here non-integer variable, you'll receive an error.
cout << "Enter your gender: ";
cin >> gender;
cout << "Enter your permanent address: ";
cin >> ws;
//The "ws" variable is not declared before and it's never used later. Maybe it should be removed?"
getline(cin, address); // save user's input into address string variable.
// The main output of the program starts here.
// "cout" can write to the console not only text, but numbers and value of variables as well.
// You can use special format syntax to combine text, numbers and variables.
// This syntax is represented below.
cout << "Hello " << first << " " << middle << " " << last << endl;
cout << "Your birthday is " << birthday
<< " and now you are " << age << " years old." << endl;
cout << "You are " << gender << endl;
cout << "and you live at " << address << endl;
// The end of program.
// As main function returns integer, so we returning here 0.
// It's common code for successfully finished program.
return 0;
}
Comments
Leave a comment