element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Code Exchange
  • Technologies
  • More
Code Exchange
Blog C++ Tutorial - Reading User Input
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: oneleggedredcow
  • Date Created: 9 Jan 2013 4:20 AM Date Created
  • Views 2288 views
  • Likes 1 like
  • Comments 2 comments
  • code_exchange
  • Code Exchange
  • raspberry_pi
  • code
  • learnc++
Related
Recommended

C++ Tutorial - Reading User Input

oneleggedredcow
oneleggedredcow
9 Jan 2013

Introduction

In the previous tutorial we learned how to compile a program and write a message to the screen.  In this tutorial we are going to learn how to read in data that they user types in and store that information.

Code

So, let’s jump right into an example.  Here’s how to read in user input and repeat it back to the user:

 

#include <iostream>

using namespace std;

 

int main()

{

                cout << “Enter your favorite number:” << endl;

                int num = 0;

                cin >> num;

                cout << “Your favorite number is “ << num << endl;

 

                return 0;

}

 

Now if you have read the first tutorial, most of this code should seem very familiar.  There are a couple of new pieces, the first one being this line:

 

int num = 0;

 

This line declares a variable, which tells the computer that we want it to store some information for us. In this case, we want the computer to store an integer number.  If we wanted the computer to store a fractional value, we would change int to be double (double precision floating point number).  If we wanted the computer to store a letter we would use a char (character).  Finally, if we wanted to store either true or false we would use a bool (boolean).  This line also sets the value of the variable to zero.

 

The next line is also new:

 

                cin >> num;

 

This tells the computer to read in a number from the command line and store it in the num variable.  There’s some nice symmetry here: the command to write to the screen is cout (console output) and we provide direction symbols going from our message to the screen (<<), and the command to read from the screen is cin (console input) and we provide direction symbols going from the screen to the variable that we would like to store the input in (>>).

 

With the cout (or cin) command, you can specify one value or variable after another to write to (or read from) the screen.  That’s why the following command works:

 

cout << “Your favorite number is “ << num << endl;

 

It writes out the message that we want and the immediately follows it with the value stored in the num variable.  The endl tells the computer that it is the end of the line, so the next piece of text will start on the next line (much like hitting enter).

Summary

In this tutorial we discussed how to read in data from the user and store it in a variable.  I encourage you to make changes to the program and read in fractional numbers (double) and letters (char) from the user.  Experiment and see what happens!

 

The next step will be making decisions based on what the user types in.  This will let us create our first game: guess what number I’m thinking of.

 

If you have any questions or comments about what was covered here, post them to the comments.  I watch them closely and will respond and try to help you out.

Tutorial Index

01 - Hello Raspberry Pi

02 - Reading User Input

03 - If Statement

04 - For Loops

05 - While Loops

06 - Functions

07 - Data Types

08 - Arrays

09 - Structures

10 - Classes

11 - Pointers

12 - Dynamic Arrays

13 - Linked List

14 - Linked List Operations

15 - STL List/Vector

16 - Templates

17 - Inheritance

18 - File I/O

19 - Strings

20 - Constants

21 - Hash Tables

Attachments:
02_FavoriteNumber.cpp.zip
  • Sign in to reply
Parents
  • tech2art
    tech2art over 6 years ago

    Hi, I am a newbie to this. Should this tutorial be compiled as the previous one to be able to run it?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Sean_Miller
    Sean_Miller over 6 years ago in reply to tech2art

    Yes.  You definitely have to compile it.

     

    Sean

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • Sean_Miller
    Sean_Miller over 6 years ago in reply to tech2art

    Yes.  You definitely have to compile it.

     

    Sean

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube