> loops; for(i= 0 ; i < loops; i++) input >> integer; input >> decimal; input >> name; cout << integer << " "; cout << decimal << " "; cout << name << endl; } return(0); mydata.txt file Jon Bill e9 Mary Smith -3 -4e3 xyz Output: Jon Bill e+010 Mary Smith xyz"> > loops; for(i= 0 ; i < loops; i++) input >> integer; input >> decimal; input >> name; cout << integer << " "; cout << decimal << " "; cout << name << endl; } return(0); mydata.txt file Jon Bill e9 Mary Smith -3 -4e3 xyz Output: Jon Bill e+010 Mary Smith xyz">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

File Review Declare the File Stream Object Name

Similar presentations


Presentation on theme: "File Review Declare the File Stream Object Name"— Presentation transcript:

1 File Review Declare the File Stream Object Name
ofstream for output to file ifstream for input from file Associate a File Name with the File Stream Object by Opening the File Read or Write to the File using << or >> Operators Close the File

2 mydata.txt file 5 8 9.3 Jon 6 14.335 Bill 0 35.67e9 Mary
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream input; int loops, integer, i; float decimal; string name; input.open("mydata.txt"); input >> loops; for(i= 0 ; i < loops; i++) input >> integer; input >> decimal; input >> name; cout << integer << " "; cout << decimal << " "; cout << name << endl; } return(0); mydata.txt file 5 8 9.3 Jon Bill e9 Mary Smith -3 -4e3 xyz Output: 8 9.3 Jon Bill e+010 Mary Smith xyz

3 Functions Lines of code with a Name()
Name()can be executed over and over again in the same program.

4 Functions displayVals() { int main() cout << …; { …
return; /*back to where we left off */ } int main() { displayVals(); return(0); }

5 Function Attributes Function Name: Identifier Used to Call Function
Function Parameter(s) or Argument(s): Value(s) Passed into Function for Use by Function Code Function Return Value: Value Returned by Function Back to Calling Function

6 Function Parameters (Arguments)
May Pass as Many Parameters as Necessary to Function A Copy of the Value of the Parameter Is Passed to the Function Changing the Value of the Parameter in the Function Does Not Affect the Value of the Original Variable This Is Called Pass-by-Value

7 Declaring a Function Function Prototype: Declaring a Function and How It Is Called Syntactically Used by the Compiler to Signal Syntax Errors Pseudocode Example: return_type function_name(parameter_type parameter_name); Example: double calculate_avg(int totOfItems, int numItems);

8 Functions – Return Values
Functions Are Typed According to Their Return Values: void, int, double, etc. Functions Are Declared by Function Prototypes (Declarations) Found Above main() Function Returns a Value to Calling Function via return Statement Standard Functions (e.g., Those in STL) Have Function Prototypes in Header Files

9 Variable Scope Scope: Area of a Program within which a Variable Can Be Referenced Variable Definitions Are Recognized in the Curly Braces in which They Were Defined Variables Declared Outside of Functions Are Recognized from the Point Declaration through the Rest of the Program

10 Function Attributes #include <iostream> using namespace std;
void printNum(int); // function prototype int main() { int myNumber = 7; printNum(myNumber); // function call printNum(9); return 0; } // begin function definition void printNum(int numToPrint) cout << numToPrint;

11 Another Function Example
#include <iostream> using namespace std; double calcAverage(int total, int numItems); int main() { int allGrades = 974, numStudents = 10; double avgGrade = 0.0; avgGrade = calcAverage(allGrades,numStudents); cout << avgGrade << endl; return 0; } double calcAverage(int total, int numItems) return (double) total / numItems;

12 What If? Write a program that prompts for the names and locations of 1000 employees. Store the information in the program for later use.

13 Program for Previous string empName1, empName2, empName3,…
string empLocation1, empLocation2, … cout << “Enter employee name 1:”; cin >> empName1; cout << “Enter Employee name 2:”; cin >> empName2; //Can we use a loop?

14 Arrays Syntax: type variableName[size];
Memory Is Set Aside for size Items of type Each Variable Location in Array Is Accessed by Offsetting into the Array with an Integer Expression Legitimate Offsets Are 0 to size-1 Example: lastname[0] = ‘H’;

15 Array Example char lastname[100]; lastname[0] = ‘H’;
lastname[1] = ‘a’; lastname[2] = ‘\0’; cout << lastname[0]; cout << lastname;

16 Array Example int values[15], i = 0; values[i] = 150;
cout << values[0]; values[3] = values[0] + 6;

17 Array Example const int ARRAY_SIZE = 100; int offset;
int numArray[ARRAY_SIZE]; for(offset = 0; offset < ARRAY_SIZE; offset++) { numArray[offset] = 0; }

18 Array Example const int ARRAY_SIZE = 10; int offset, sum = 0, average;
int numArray[ARRAY_SIZE]; for(offset = 0; offset < ARRAY_SIZE; offset++) { cout << “Enter Score ” << offset << “ : “; cin >> numArray[offset]; } sum = sum + numArray[offset]; average = sum / ARRAY_SIZE;

19 Exam 2 25% of Final Grade Know: loops, switch/case
Files (.open(), >> and <<, .close() ) Input Failure (cin, cin.clear(), cin.ignore()) Functions Arrays

20 Initializing Arrays int main() { char cArray3[5] = {'a', 'b', 'c'};
int iArray[] = {1, 2, 3, 4}; int iArray2[10] = {10, 13, 15, 17}; double dArray[] = {3.4, 5.67e4}; double dArray1[5] = {6.7, 7.8, 9.5}; }

21 Initializing string Arrays
#include <string> using namespace std; int main() { string sArray[] = {"one", "two", "three"}; cout << sArray[0]; }

22 Two Dimensional Arrays
char cArray[10][20]; int iArray[100][50]; cArray[0][0] = ‘a’; cArray[0][1] = ‘b’; cArray[9][19] = ‘x’; iArray[0][0] = 99; iArray[1][5] = 135; iArray[99][49] = 0;


Download ppt "File Review Declare the File Stream Object Name"

Similar presentations


Ads by Google