Input/OutputAhh, input and output, the integral parts of any program. Can you imagine a program without input and output? Think of your favorite game without instructions on the screen, or analog sticks to move around with... pretty boring huh? Now hopefully you realize the full meaning of what you're going to learn. Input and output, even at its most basic form, can make or break a game.
In this lesson, I'm assuming you know the material from lesson 2 which talks about data types. Heres a quick refresher:
- there are many different data members
- data members include: int, char, and bool
- data members are used to store input
As for output, you should remember cout (See-owt).
- Code:
-
cout << "Text" << endl;
Well today we introduce a new bit of code. This code is the mighty "cin". (See-in) You will use cin many times throughout your programming career, so listen carefully (well, not literally as this is text...) Cin prompts for input from the user. Cin's syntax is this:
- Code:
-
cin >> variable;
basically you say: "cin" to tell the compiler you want input. Then you say ">>" look familiar? think back to cout: "cout
<<... The operators for cout are <<. Think of arrows pointing outward to the screen. Whereas the operators for cin are arrows pointing back to the program (>>).
So, since I'm supposed to integrate input and output, heres an example with both cin and cout in it:
- Code:
-
// Bob's Question V 1.0
// Demonstrates cin
#include <iostream> // preprocessor directive that tells the compiler to include the .h file "iostream"
using namespace std; // use the standard namespace "std::cout" to "cout" same with "cin"
int main() // function main with a return type of integer and no parameters
{
cout << "Hi! My name is Bob, whats your favorite number?" << endl;
int number; // makes an int called number
cin >> number; // tells program to get input from the user and
store it in the variable "number"
system("PAUSE");
return 0;
}
Now you know how to get input. Whats that you say? How do we display it? I'm glad you asked. No one asks for a favorite number, then walks away without saying anything!
- Code:
-
//Bob's Question V 1.1
//Demonstrates cin and cout
#include <iostream>
using namespace std;
int main()
{
cout << "Hi, my name is Bob, whats your favorite number?" << endl;
int number;
cin >> number;
cout << number << " is your favorite number? Mine too!" << endl;
system("PAUSE");
return 0;
}
Whats new here? well after Bob asks your favorite number, he then repeats the number to you, exclaiming that it is his favorite number too. You'll notice that I didn't put "" around the variable number when I displayed it "cout << number << ..." as it is not text.
When displaying a variable, simply type the variable name.
Ints aren't the only data types you can perform I/o with. In the next few programs I will display variations of the Bob program that reflect I/O being performed on different data types. By the end you should be sick of Bob and his mannerisms, but don't rejoice to much when you are done, Bob will be a recurring character in my programs.
-
- Code:
-
//Bob's Question V 1.2
//Demonstrates cin and cout with chars
#include <iostream>
using namespace std;
int main()
{
cout << "Hi, my name is Bob, whats your favorite letter?" << endl;
char letter;
cin >> letter;
cout << letter << " is your favorite letter? Mine too!" << endl;
system("PAUSE");
return 0;
}
- Code:
-
//Bob's Question V 1.3
//Demonstrates cin and cout **THE INCORRECT WAY**
#include <iostream>
using namespace std;
int main()
{
cout << "Hi, my name is Bob, whats your favorite number?" << endl;
int number;
cin >> number;
cout << "number" << " is your favorite number? Mine too!" << endl; // ERROR: Number isn't text, its a variable. Drop the quotes!
system("PAUSE");
return 0;
}
- Code:
-
//Bob's Question V 1.4
//Demonstrates cin and cout
#include <iostream>
using namespace std;
int main()
{
cout << "Hi, my name is Bob, whats your favorite decimal number?" << endl;
float dNumber; // float is a data type with a number with decimal points
cin >> dNumber;
cout << dNumber << " is your favorite decimal number? Mine too!" << endl;
system("PAUSE");
return 0;
}
And so forth and so on... As you get into strings input will be a little different, but study this for now.
[/u][list=1][*]
You Try! 1. Create a Text based game that takes input from chars and ints
2. Create a program that takes a series of numbers from the user and displays them back
I know this is all boring, but in time, you'll learn strings and it will be fun! Have a good Day! Stay Cool!
Feel free to email me what you make. I can give you pointers on how to improve.
letslearncpp@gmail.com
Next lesson will be on Operators and Math.