Imagine your little elementary brother asking you to help him with his assignment in mathematics. The instruction of their teacher was to get the average of a set of values. Because you feel the need to help your littler bro out, you glanced at his notebook and saw five sets of numbers to be averaged.
- 5 5 6 8
- 12 23 34 45
- 100 200 300 400
- 12 76 45 56
- 23 23 34 12
It was your little bro, giving you the pitiful "doggy eyes" look, showing you his notebook as he flips the pages, saying in a desperate voice, "brother, what about the other 500 sets of numbers to be averaged??"...
Even if we make a program to get the averages of the list of number sets. It's almost expected that after we get a working program, the task is still "strenuous". If the program developed would have no loops we need to
- compile the program
- re-run the program 500 times
- type the number sets 500 times
- get results and copy them to your brother's notebook 500 times
- compile the program
- enter number sets 500 times
- get results and copy them to your brother's notebook 500 times
- write all the number sets in a separate text file
- compile and run your program once only
- copy the results to your brother's notebook, (or technically your brother has to do all the copying, right??)
take a look at this sample code below
It somewhat works this way, God declared us to be to be of type human, having declared as such we find ourselves capable of some kind of skill or functions like singing, cooking our own meals, using our hands to produce things etc. Now notice that in line 9 infiler is declared to be of type ifstream , being declared as such infiler is granted the capability to function both as a bridge and a container.
- #include <iostream>
- #include <fstream>
- using namespace std;
- int main()
- {
- int num1, num2, num3, num4;
- ifstream infiler;
- infiler.open("assignmentOfLittleBro.txt");
- if (infiler.is_open())
- {
- while (!infiler.eof())
- {
- infiler >> num1 >> num2 >> num3 >> num4;
- //get the average of the four numbers and output it (left as an exercise)
- }
- }
- infiler.close();
- return 0;
- }
How is it a bridge?? Line 10 tells us that infiler has a skill .open() that allows it to open files located in the same folder or directory. Line 11 also demonstrates how infiler checks if the text file to be opened (orange text) was successfully opened using .is_open(). Being multi-talented infiler also was given the talent to check if we've already reached the end of the file we've just opened (Line 13) using .eof(). The while loop is there since we need to iterate through the data present in assignmentOfLittleBro.txt which would look something like this (you're the one to encode it).
5 5 6 8
12 23 34 45
100 200 300 400
12 76 45 56
23 23 34 12
How is it a container?? Line 15 could be translated as "get four integers from assignmentOfLittleBro.txt and store them into four variables namely the ones we've declared". combining Line 15 with Line 13 it would pretty much translate to "until we reach the end of the file get four integers from assignmentOfLittleBro.txt and store them into four variables namely the ones we've declared". Isn't this great??? Now try the following things out...
- file streaming will not work without #include <iostream> why is that???
- suppose you have the following input values
1 2 3 4 5 6 7 8 9 0
123 234 345 456
- will the code still work?? Why?? Why not??
Goodluck with your little brother! Make him proud. =]