Error in loops
Apr 10, 2016 at 2:00pm UTC
Hey everyone I need some help figuring out what's wrong with my code. It's supposed to read data from a text file that contains values for frames, bins and amplitudes. There are 25 frames which contain 1024 bins and each bin has an amplitude value. The code is also supposed to output the highest amplitude per frame and the bin it lies in, however I am getting the full list of 25000+ rows of data and a random code under the amplitude value. I feel like there are two errors: 1 within the counter and one within the for or while loop. Any ideas?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int frame = 1;
int bin = 0;
int frame1 = 0, bin1 = 0;
int counter = 0, amplitude[25][1024];
int max = 0, highestMax = 0, highestFrame = 0, highestBin = 0;
ifstream audioFile;
audioFile.open("audioframes.txt" );
int data;
audioFile >> data;
ofstream outFile;
if (!audioFile.is_open()) {
cout << "Failure to open file." << endl;
}
else {
cout << "File opened successfully." << endl;
if (counter % 3 == 2)
{
amplitude[frame][bin] = data;
bin++;
}
if ((frame != 24) && (bin == 1024))
{
bin = 0;
frame++;
}
counter++;
}
audioFile.close();
int i = 0;
while (i < 25)
{
int j = 0;
if ((i == 0) && (j == 0))
{
cout << "The maximum amplitudes per frame are:" << endl;
cout << "Frame" << "\tBin" << "\tMaximum Amplitude:" << endl;
outFile << "The maximum amplitude per frame is:" << endl;
outFile << "Frame" << "\tBin" << "\tMaximum Amplitude" << endl;
}
i++;
for (int j = 0; j < 1024; j++)
{
if (j == 0)
{
max = amplitude[i][j];
}
if (amplitude[i][j] > max)
{
max = amplitude[i][j];
frame1 = i;
bin1 = j;
}
if (amplitude[i][j] > highestMax)
{
highestMax = amplitude[i][j];
highestFrame = i;
highestBin = j;
}
cout << frame1 << "\t" << bin1 << "\t" << max << endl;
}
}
return 0;
}
Last edited on Apr 10, 2016 at 2:01pm UTC
Apr 10, 2016 at 2:17pm UTC
You only seem to be reading one item from your input file and you seem to be using your amplitude array before it has been totaly initialized.
Topic archived. No new replies allowed.