Download presentation
Presentation is loading. Please wait.
Published byDennis Thompson Modified over 9 years ago
1
Quiz 2 Results
2
What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!" << endl; } return(0); }
3
What Is Wrong? (Corrections) #include using namespace std; int main() { int i; for(i = 0; i < 3; i++) { cout << "Hello World!" << endl; } return(0); }
4
Files Data in Main Memory is “volatile” File: Place for “permanent” data storage C: drive, A: drive, Flash drive, etc. Disk File Main Memory progEx2Prob3.exe main() int num; string firstname;
5
Output File Streams In C++, the Programmer Can Declare Output Streams #include using namespace std; ofstream outputStream; //ofstream object int num1, num2, num3; cout << “Enter 3 Numbers for datafile: “; cin >> num1 >> num2 >> num3; outputStream.open(“datafile.txt”); outputStream << num1 << “ “; outputStream << num2 << “ “; outputStream << num3; outputStream.close();
6
Input File Streams In C++, the Programmer Can Declare Input Streams #include using namespace std; ifstream inputStream; //ifstream object int num1, num2, num3; inputStream.open(“datafile.txt”); inputStream >> num1 >> num2 >> num3; inputStream.close();
7
To Read or Write from a File Declare the File Stream Object Name Associate a File Name with the File Stream Object by Opening the File Read or Write to the File using > Operators Close the File
8
#include 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 6 14.335 Bill 0 35.67e9 Mary -23 -4.55 Smith -3 -4e3 xyz 8 9.3 Jon 6 14.335 Bill 0 3.567e+010 Mary -23 -4.55 Smith -3 -4000 xyz Output:
9
Functions Lines of code with a Name() Name() can be executed over and over again in the same program.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.