Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)

Similar presentations


Presentation on theme: "Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)"— Presentation transcript:

1 Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)

2 Overview Getting Started  Semi-colons, curly-braces, whitespace  Comments  #include's  Main program Input / Output A little more detail on the compiling process

3 Nearly every line of C/C++ code ends with a semi- colon  Frustrating at first, second-nature later... White-space is generally ignored “Containing” is indicated by pairs of curly-braces  No requirements they appear on different lines int i=5; // N ote – this line has a blank line (whitespace) i++; while (i < 10)// Note: no semi-colon here { cout << i << endl; } int i=5; // N ote – this line has a blank line (whitespace) i++; while (i < 10)// Note: no semi-colon here { cout << i << endl; }

4 A way to add descriptive text to a source file  More readable To you To other people that have to look at it  Documentation What a section of code does, and how Doesn't affect the size of the executable  (although the size of the source files is increased)

5 Comments, cont. Syntax:  Complete single-line  End-of-line  Multi-line (or in-line) Bold black text is actual C/C++ code // This is a single line comment x = 5; // This is an end-of-line comment /* This is a multi- line Comment */ // The 10 below is an in-line comment for (int i=0; i < /*10*/ 5; i++) { } // This is a single line comment x = 5; // This is an end-of-line comment /* This is a multi- line Comment */ // The 10 below is an in-line comment for (int i=0; i < /*10*/ 5; i++) { }

6 A technique to include a collection (library) of pre- defined symbols  Functions  Classes  Constants …… Just a (text) file with a collection of C/C++ code  [Look in $CodeBlocks\MinGW\lib\gcc\mingw32\4.7.1\includ e\c++\iostream] For now, just “built-in” collections  Later: our own collections, downloaded libraries  Later: more precise definition of what they really are...

7 #include, cont. Syntax: Put at the top of a.c or.cpp file. #include is part of the Pre-processor sub-language of C/C++  [Note the # at the beginning] #include // A standard include file #include “my_file.h”// A “local” include file (later) #include // A standard include file #include “my_file.h”// A “local” include file (later)

8 #include, cont. How it works:  #include textually copy/pastes the content of the referenced file in-place of the #include directive  This happens before the compiler even starts. Example:  [Local include file, with a const variable – show what compiler sees]

9 Where the program starts  In a.c or.cpp file  Can only appear once in a project For console app's, 4 types:  void return type (C++ only)  int return type (C/C++)  With command-line arguments  without command-line arguments Windows, OSX, etc. app's might have a different form...

10 Main Program, cont. void main()// C++ only { /* Program code goes here... */ } void main()// C++ only { /* Program code goes here... */ } int main() { /* Program code goes here... */ return 0; // Indicates everything ran OK // Return non-zero to indicate errors. } int main() { /* Program code goes here... */ return 0; // Indicates everything ran OK // Return non-zero to indicate errors. } void main(int argc, char ** argv)// C++ only { /* Program code goes here... */ } void main(int argc, char ** argv)// C++ only { /* Program code goes here... */ } int main(int argc, char ** argv) { /* Program code goes here... */ return 0; } int main(int argc, char ** argv) { /* Program code goes here... */ return 0; }

11 A way to input or output data from our program  Input Keyboard [Mouse another day, hopefully]  Output Command-line output (screen) (As will become the norm) we'll look at this in both C and C++  Most of the time they'll be the same...

12 Output to command-line (C++) Uses the header (collection)  Uses namespaces (we'll look at these later...)  For now, just put “using namespace std;” below the #include's Uses the pre-defined cout object  By default, the screen (can be “remapped”)  A stream Write data elements to it using the chevron operator << Conversions are applied as necessary (automatically)

13 Output to command-line (C++), cont. #include using namespace std; void main() { cout << “Hello, World!”; cout << “A (fake) second line.”; cout << endl << “The real second line!” << endl; cout << “An integer: “ << 5 << “, A double: “ << 3.14159 << “, A string: “ << “'Hi!'” << endl; } #include using namespace std; void main() { cout << “Hello, World!”; cout << “A (fake) second line.”; cout << endl << “The real second line!” << endl; cout << “An integer: “ << 5 << “, A double: “ << 3.14159 << “, A string: “ << “'Hi!'” << endl; }

14 Escape sequences A way to put a “hard-to-type” character in a string Start with a backslash and then a 1-character code indicating which escape sequence to use. \n// New-line \t// Tab \\// An actual back-slash \”// A quote cout << “****\n@\t@\n****” << endl; Output: **** @ ****

15 Output to command-line (C) Also defaults to the screen. You need to include...  if compiling with a C++ compiler  if compiling with either a C or a C++ compiler Done with the printf function. Uses a “format string” followed by 0 or more values %d// An integer %f// A float or double %s// A string // There are more – these are just the basics

16 Output to command line (C), cont. #include int main() { printf(“Hello, World!”); printf(“A (fake) second line.”); printf(“\nThe real second line!\n”); printf(“An integer: %d, A double: %f, A string: %s\n”, 5, 3.14159, “'Hi!'”); return 0; } #include int main() { printf(“Hello, World!”); printf(“A (fake) second line.”); printf(“\nThe real second line!\n”); printf(“An integer: %d, A double: %f, A string: %s\n”, 5, 3.14159, “'Hi!'”); return 0; }

17 Input from the command line (C++) By default, reads keyboard input Also included by  Don't forget the “using namespace std;” line Uses the pre-made cin object In the example, I'll use variables  We'll discuss these in detail in the next section Also uses the chevron operator, but this time they go the other way (>>)  I think of this as pointing from cin to the variables

18 Input from the command-line (C++), cont. #include using namespace std; int main() { int myInt; float myFloat; string myString; cout << "Enter an integer, then press enter: "; cin >> myInt; cout << "Enter a float and a string separated by a "; cout << "space, then press enter: "; cin >> myFloat >> myString; cout << "Your int was: " << myInt << endl; cout << "Your float was: " << myFloat << endl; cout << "Your string was: '" << myString << "'" << endl; return 0; } #include using namespace std; int main() { int myInt; float myFloat; string myString; cout << "Enter an integer, then press enter: "; cin >> myInt; cout << "Enter a float and a string separated by a "; cout << "space, then press enter: "; cin >> myFloat >> myString; cout << "Your int was: " << myInt << endl; cout << "Your float was: " << myFloat << endl; cout << "Your string was: '" << myString << "'" << endl; return 0; }

19 Input from the command-line (C) Also defaults to the keyboard. You need to include...(just like printf)  if compiling with a C++ compiler  if compiling with either a C or a C++ compiler Done with the scanf function. Uses a “format string” like printf.  However, you pass the address of variables in which you wish to store the typed value.

20 Input from command-line (C), cont. #include int main() { int myInt; float myFloat; char myString[256]; printf("Enter an integer, then press enter: "); scanf("%d", &myInt); printf("Enter a float and a string separated by a "); printf("space, then press enter: "); scanf("%f %s", &myFloat, myString); printf("Your int was: %d\n", myInt); printf("Your float was: %f\n", myFloat); printf("Your string was: '%s'\n", myString); return 0; } #include int main() { int myInt; float myFloat; char myString[256]; printf("Enter an integer, then press enter: "); scanf("%d", &myInt); printf("Enter a float and a string separated by a "); printf("space, then press enter: "); scanf("%f %s", &myFloat, myString); printf("Your int was: %d\n", myInt); printf("Your float was: %f\n", myFloat); printf("Your string was: '%s'\n", myString); return 0; }

21 Input / Output to (Text and Binary) Files Very similar syntax to command-line input/output We'll look at this shortly  After we've looked at variables


Download ppt "Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)"

Similar presentations


Ads by Google