#include "stdafx.h"
#include <iostream>
#include <string>
#include<fstream>
usingnamespace std;
int main()
{
int a, b;
a = 2;
b = 5;
cout << "The variable 'a' is \n" << a<<endl;
cout << "the variable 'b' is \n" << b;
cout << "\nso a + b = " << a + b<<endl;
cout << "What was your name again?\n";
string name;
int c;
string state;
getline(cin, name);
cout << "what year were you born in?\n";
cin >> c ;
cout << "and what state were you born in?\n";
getline(cin, state);
cout << "so your name is " << name << " and you were born in the year, " << c << " and you were born in the state of "<<state<<endl;
cout << "What year is it?";
int d;
cin >> d;
cout << "you were born " << d - c << " years ago\n";
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10 11 12 13
The variable 'a' is
2
the variable 'b' is
5
so a + b = 7
What was your name again?
jemeu
what year were you born in?
1990
and what state were you born in?
so your name is jemeu and you were born in the year, 1990 and you were born in the state of
What year is it?2020
you were born 30 years ago
So I think I understand getline (cin,input) but I'm not understanding where I'm using
1 2
int c
cin>> c;
and how it's going to the next line right away. I'm hoping to get a closer age with questioning the month and day, but I'm just learning how to do with what I'm learning so I don't move too fast forward. thanks
#include <iostream>
#include <string>
int main()
{
int a, b;
a = 2;
b = 5;
std::cout << "The variable 'a' is " << a << '\n';
std::cout << "the variable 'b' is " << b << '\n';
std::cout << "so a + b = " << a + b << "\n\n";
std::cout << "What was your name ? ";
std::string name;
std::getline(std::cin, name);
std::cout << "what year were you born in? ";
int birth_year;
std::cin >> birth_year;
// ignore all the characters in the stream until the endline, extracting that
std::cin.ignore(32667, '\n');
std::cout << "and what state were you born in? ";
std::string state;
// mixing std::getline and std::cin cause input problems
std::getline(std::cin, state);
std::cout << '\n';
std::cout << "So your name is " << name << " and you were born in the year, " << birth_year
<< "\nand you were born in the state of " << state << "\n\n";
std::cout << "What year is it? ";
int current_year;
std::cin >> current_year;
std::cout << "you were born " << current_year - birth_year << " years ago\n";
}