1337codez™
Would you like to react to this message? Create an account in a few clicks or log in to continue.



 
HomeHome  PortalPortal  Latest imagesLatest images  RegisterRegister  Log inLog in  

 

 Lesson 3: Basic Input and Output

Go down 
2 posters
AuthorMessage
Wander
C++ Expert
Wander


Posts : 72
Join date : 2010-08-10
Location : Florida

Lesson 3: Basic Input and Output Empty
PostSubject: Lesson 3: Basic Input and Output   Lesson 3: Basic Input and Output EmptyWed Aug 11, 2010 3:25 pm

Input/Output
Ahh, 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.


  1. 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;

    }

  2. 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! geek
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! afro

Feel free to email me what you make. I can give you pointers on how to improve. Smile
letslearncpp@gmail.com

Next lesson will be on Operators and Math.
Back to top Go down
http://letslearncpp.forumstech.com/
DarK_DemoN
»[\...VIP.../]«™ AdminiStratoR
DarK_DemoN


Posts : 584
Join date : 2010-07-20
Age : 31
Location : Hacking Kingdom, USA

Lesson 3: Basic Input and Output Empty
PostSubject: Re: Lesson 3: Basic Input and Output   Lesson 3: Basic Input and Output EmptyWed Aug 11, 2010 4:37 pm

lol i took your v1.0, changed some stuff and added a "Second favorite" lol

This is the src:
//Random crap
#include <iostream>
using namespace.std;
int main()
{
cout << "Whats up? Im your computer so you better answer my questions... What number do you like the best?" << endl;
int number;
cin >> number;
cout << number << "Why do you like that one? Nevermind, whats your second favorite?" << endl;
int number;
cin >> number;
cout << number << "Oh thats cool. Thank you." << endl;
system("PAUSE");
return 0;
}

Back to top Go down
http://odstofficalsite.webs.com
Wander
C++ Expert
Wander


Posts : 72
Join date : 2010-08-10
Location : Florida

Lesson 3: Basic Input and Output Empty
PostSubject: Re: Lesson 3: Basic Input and Output   Lesson 3: Basic Input and Output EmptyWed Aug 11, 2010 4:42 pm

Cool! Nice coding! One thing Smile

1) The only thing that I see wrong is that whenever it gets the second favorite it overwrites the first favorite. For your purposes in that code. It works fine, but if you ever wanted to display both variables at the same time, it would display the same number two times.

PS: Did you get your IDE working?
Back to top Go down
http://letslearncpp.forumstech.com/
DarK_DemoN
»[\...VIP.../]«™ AdminiStratoR
DarK_DemoN


Posts : 584
Join date : 2010-07-20
Age : 31
Location : Hacking Kingdom, USA

Lesson 3: Basic Input and Output Empty
PostSubject: Re: Lesson 3: Basic Input and Output   Lesson 3: Basic Input and Output EmptyWed Aug 11, 2010 4:51 pm

lol just saw that. how do i fix it?

do i put it like this:
cin >> number2;
cout << number2 << ?


i havent started compiling yet i just wrote it down lol
Back to top Go down
http://odstofficalsite.webs.com
Wander
C++ Expert
Wander


Posts : 72
Join date : 2010-08-10
Location : Florida

Lesson 3: Basic Input and Output Empty
PostSubject: Re: Lesson 3: Basic Input and Output   Lesson 3: Basic Input and Output EmptyWed Aug 11, 2010 4:54 pm

Yup! Exactly right! Just don't forget to declare the second variable.

Code:
int firstNumber;
int secondNumber;

cin >> firstNumber;
cout << firstNumber << endl;

cin >> secondNumber;
cout << secondNumber << endl;
Back to top Go down
http://letslearncpp.forumstech.com/
DarK_DemoN
»[\...VIP.../]«™ AdminiStratoR
DarK_DemoN


Posts : 584
Join date : 2010-07-20
Age : 31
Location : Hacking Kingdom, USA

Lesson 3: Basic Input and Output Empty
PostSubject: Re: Lesson 3: Basic Input and Output   Lesson 3: Basic Input and Output EmptyWed Aug 11, 2010 5:02 pm

ok so the entire "second" number choice command would be:

inr number2;
cin >>number2;
cout << number2 << "Oh thats cool, Thank You" << endl;
Back to top Go down
http://odstofficalsite.webs.com
Wander
C++ Expert
Wander


Posts : 72
Join date : 2010-08-10
Location : Florida

Lesson 3: Basic Input and Output Empty
PostSubject: Re: Lesson 3: Basic Input and Output   Lesson 3: Basic Input and Output EmptyWed Aug 11, 2010 5:05 pm

Yes. Except its supposed to be 'int', not 'inr' Razz
I expect that was a typo.
Back to top Go down
http://letslearncpp.forumstech.com/
DarK_DemoN
»[\...VIP.../]«™ AdminiStratoR
DarK_DemoN


Posts : 584
Join date : 2010-07-20
Age : 31
Location : Hacking Kingdom, USA

Lesson 3: Basic Input and Output Empty
PostSubject: Re: Lesson 3: Basic Input and Output   Lesson 3: Basic Input and Output EmptyWed Aug 11, 2010 5:09 pm

lol yeh it was a typo, im on my dsi and the keys are tiny lol
Back to top Go down
http://odstofficalsite.webs.com
Wander
C++ Expert
Wander


Posts : 72
Join date : 2010-08-10
Location : Florida

Lesson 3: Basic Input and Output Empty
PostSubject: Re: Lesson 3: Basic Input and Output   Lesson 3: Basic Input and Output EmptyWed Aug 11, 2010 5:12 pm

lol okay. XD
Back to top Go down
http://letslearncpp.forumstech.com/
Sponsored content





Lesson 3: Basic Input and Output Empty
PostSubject: Re: Lesson 3: Basic Input and Output   Lesson 3: Basic Input and Output Empty

Back to top Go down
 
Lesson 3: Basic Input and Output
Back to top 
Page 1 of 1
 Similar topics
-
» Lesson 4: Operators and Math
» Lesson 2: Variables and Data Types
» Lesson 1: Structure
» Lesson 5: If Statements
» Tell me when you're ready for the next lesson. :)

Permissions in this forum:You cannot reply to topics in this forum
1337codez™ :: Programing :: C++ Programinng Basics-
Jump to: