-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathcin.cpp
80 lines (63 loc) · 2.27 KB
/
cin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*******************************************************************************
*
* Program: cin Basics
*
* Description: Demonstration of how to use cin to accept user input with C++.
*
* YouTube Lesson: https://www.youtube.com/watch?v=hKqNb3Wo6z0
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
// cin is included in the iostream library
#include <iostream>
using namespace std;
int main()
{
// prompt the user for an int value
int x;
cout << "Enter an integer: ";
// use cin to store the value the user enters into x... if for some reason
// it cannot be done, the expression will evaluate to false
if (cin >> x)
{
// if an integer was successfully received, output the value
cout << "You entered the integer " << x << endl;
}
else
{
// if there was an error, clear the error, and inform the user
// they provided invalid input
cin.clear();
cout << "Invalid input" << endl;
}
// will clear the input stream of any characters, up to either 1000
// characters or the first newline whichever comes first
cin.ignore(1000, '\n');
// variables for rectangle dimensions, area
double height = 0;
double width = 0;
double area = 0;
// prompt the user for a height and width value
cout << "Enter height and width (separated by a space):";
// we can use cin to accept multiple values at once, so inputting a height
// value and a width value separated by whitespace characters will work,
// e.g. entering 5 20.5
cin >> height >> width;
area = height * width;
cout << height << " x " << width << " = " << area << endl;
// clear the input stream of any characters (e.g. a newline)
cin.ignore(1000, '\n');
// prompt the user for their name
string name;
cout << "Enter your name: ";
// will only store the string up until the first space character, so a
// name like 'kevin browne' will only be stored as 'kevin' into name
// cin >> name;
// will store the string up until the first newline character into name,
// so a name like 'kevin browne' will be stored entirely
getline(cin, name);
// say hello to the user by using their name :-)
cout << "Hello " << name << "!" << endl;
return 0;
}