In this code I am trying to write a code that will count the number of 0's and the number of 1's from a ifstream...
My result should look like this:
Sample contents of digital.dat:
1 0 0 0 0 1 1 0 1 0 1 1 1 1 0 0 0 1
Message to be displayed:
Emit 1270-hz tone for 1 time units
Emit 1070-hz tone for 4 time units
Emit 1270-hz tone for 2 time units
Emit 1070-hz tone for 1 time units
Emit 1270-hz tone for 1 time units
Emit 1070-hz tone for 1 time units
Emit 1270-hz tone for 4 time units
Emit 1070-hz tone for 3 time units
Emit 1270-hz tone for 1 time units
Here is my current code:
#include <iostream>
#include <fstream>
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
int lastDidgit = -1; // I set to -1 because of the possibility that the file can start with 0.
int currentDidgit;
int count =0;
ifstream indata;
indata.open("digital.txt");
if(indata.good()) // see if we got the file open correctly
{
// now loop throught the file to the end. eof stands for end of file.
while(indata.eof() != true)
{
// read in the next didgit
indata >> currentDidget;
// see if its the same as lastDidgit
if(currentDidget != lastDidgit)
{
// output info
cout << "Emit 1270 for" << count << "times." << endl;
// reset the count.
count = 0;
}
else
{
count++;
}
// set the lastDidgit for the next pass of the loop
lastDidgit = currentDidgit;
} // end of while
}
else // for a good file
{
cout << "Could not open Input stream for some reason!" << endl;
}
return 0;
}