New line character seemingly coming from nothing - c++

for(int x = 4999; x >= 0; x--)
{
outFile <<left<<setw(9)<< arr[x].Rank << "\t";
outFile <<setw(6)<< arr[x].id << "\t";
outFile << setw(37) << arr[x].names << "\t";
outFile << arr[x].pointSum << "\r\n";
}
for(int x = 4999; x >= 0; x--)
{
outFile <<left<<setw(9)<< arr[x].Rank << "\t";
//outFile <<setw(6)<< arr[x].id << "\t";
outFile << setw(37) << arr[x].names << "\t";
outFile << arr[x].pointSum << "\r\n";
}
The first loop outputs this on all of its lines:
275
160760 lcucujuyeikljuikg HighSchool 11
The second one outputs this:
275 lcucujuyeikljuikg HighSchool 11
For some reason getting rid of that one line somehow gets rid of a new line character. What could be causing this? I am at a complete loss here. I get the same results from cout as well

Related

Changing the first line of file and going to the last without losing data c++

I'm working on a school project for C++ with involves on working with polimorfism, dynamic objects, heritage. My project consists on an main.cpp with will run a loop with options for the user to select. I will be storing some data in a file, the data will be appended so I don't loose the data that was already in the file, but my file works like the following example:
2
Data;12;21;0
Data2s;22;61;7
This is what I want to happen:
3
Data;12;21;0
Data2s;22;61;7
Data3s;8;2;9
What I'm attempting to do is, I have to change the first line to the next variable number, and I can't loose the data under it.
After I change the line, I will go to the end of the file (after numb 7) so I can add another line of data.
attempt 1:
void Empresa::save_data(){
ofstream arquivoE;
arquivoE.open("data.txt", ios::app);
if (temp > 0){
arquivoE.seekp(0) << num_func;
arquivoE.seekp(0, ios_base::end);
arquivoE << endl;
}else{
arquivoE << num_func << endl;
}
for (int i = temp; i < num_func; i++)
{
arquivoE << func[i]->getNome() << ";" << func[i]->getEmissao() << ";" << func[i]->getSalario() << ";" << func[i]->last_field() << endl;
}
cout << " Sucesso ao gravar arquivo!" << endl;
arquivoE.close();
}
attempt 2:
void Empresa::save_data(){
ofstream arquivoE;
arquivoE.open("data.txt", ios::out);
arquivoE.seekp(0, ios::beg) << num_func;
arquivoE.seekp(0, ios::end);
arquivoE << endl;
for (int i = temp; i < num_func; i++)
{
arquivoE << func[i]->getNome() << ";" << func[i]->getEmissao() << ";" << func[i]->getSalario() << ";" << func[i]->last_field() << endl;
}
cout << " Sucesso ao gravar arquivo!" << endl;
arquivoE.close();
}
num_func is the the variable from the first line, which I need to change
Temp variable is just to keep track of where to add the line
This is the data line:
arquivoE << func[i]->getNome() << ";" << func[i]->getEmissao() << ";" << func[i]->getSalario() << ";" << func[i]->last_field() << endl;

FLTK output showing the latest input

main.cpp
void torsoOPCB(Fl_Widget *w, void* p) {
for(std::size_t i=0; i < torso.size(); i++) {
cout << "Name: " << torso[i].GetName() << endl;
cout << "Part Number: " << torso[i].GetPartNumber() << endl << endl;
} // This loop is to check if the inputs are in the vector torso
dialog = new Fl_Window(340, 300, "Robot Part");
Fl_Multiline_Output* output = new Fl_Multiline_Output(100, 10, 400, 200, "Torso list:");
for(std::size_t i = 0; i < torso.size(); i++) {
output->value(torso[i].print().c_str());
}
dialog->end();
dialog->set_non_modal();
dialog->show();
}
I'm learning how to use FLTK in C++ and I'm not sure why it keeps showing me the latest user's inputs. For example, if I entered the inputs for torso[0] and torso[1], the output will only show torso[1] which is the latest input. The inputs are stored correctly I think but I'm not sure why it won't show both torso[0] and torso[1].
Here is my print fucntion
Torso.cpp
std::string Torso::print()
{
ostringstream of;
of << "Part name: " << GetName()
<< endl << "Part #: " << GetPartNumber()
<< endl << "Weight: " << GetWeight()
<< endl << "Cost: " << GetCost()
<< endl << "Battery Comp: " << GetDescription()
<< endl << "Description: " << GetBatteryCompartmentSize() << endl;
return of.str();
}
I'm using FLTK version 1.3.4. Thank you in advanced
You are only showing the very last item in
for(std::size_t i = 0; i < torso.size(); i++) {
output->value(torso[i].print().c_str());
}
The output value is always being overwritten. If you wish to show all the output, gather it up first before putting it into the widget
std::ostringstream oss;
for(std::size_t i = 0; i < torso.size(); i++) {
oss << torso[i].print() << "\n";
}
output->value(oss.str().c_str());
If you don't mind multiple outputs on the same line, change the "\n to ". "

c++ - Need help parsing a convoluted file

[Setup]
There are three data files (csv – space delimited).
Each file, and each line in that file, represents a data that will be used.
Line 1 of each file makes subset_1, Line 2 of each file makes subset_2 and so on until you have four subsets. Then you have a full data set consisting of lines 1~4 in each file.
Since there are 16 lines in each file and each data set consists of 4 lines.... there is a total of four datasets to be dealt with.
What happens now is that each line of data for each file is run through the parser.
Line 1 (all three files) – subset_1
The percentage difference between multiple points is calculated.
The percentage difference generated from step one is turned into an absolute value
The non-absolute value, from step one, is set as the non-absolute subtotal
The absolute value, from step two, is set as the absolute subtotal.
Line 2 (all three files) – subset_2
The percentage difference between multiple points is calculated.
The percentage difference generated from step one is turned into an absolute value
The non-absolute value, from step one, is added to the non-absolute subtotal
The absolute value, from step two, is added to the absolute subtotal.
…
The same thing is done for lines 3 and 4. After line four you have dataset 1 non-absolute/absolute totals
…
Line 5 (all three files) – subset_5
repeat all the same steps you did previously for subsets_1~4
…
Repeat again and again until all sixteen lines have been read and four datasets have been created.
At present all of the above steps are working fine.
[What I am trying to do]
For each full data set that is created I want to output this dataset to an array so that I can use this data in more math/calculations. I have tried and succeeded at putting the data into an array, however, getting the data out of an array is not so easy because of one reason – a decimal point. Column 1 and column 2 of each data file contains a number with a decimal point. I cannot figure out how to get the data out of the array that I put it into because I don't know how :(
Can someone help me get this working?
XXX WORKING FILES XXX
Statistics_BOB_1400MHz.csv _http://pastebin.com/9xXZDSxQ
Statistics_BOB_1700MHz.csv _http://pastebin.com/a6yGcv6Z
Statistics_BOB_2300MHz.csv _http://pastebin.com/tnGKwAyB
XXXXXXXXXXXXXXXXXXXXX
/*
* main.cpp
*
* Created on: Jul 30, 2015
* Author: youngc
*/
#include <iostream>
#include <string>
#include <sstream>
#include <cerrno>
#include <errno.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>
using namespace std;
//step info & names for the raw data array
struct stepData
{
// The following letters correspond to the Columns in the spread sheet
// "" 0 <-- Does not exist
double VEUP; // A 1
double VEDOWN; // B 2
string info; // C 3
int group; // D 4
int step; // E 5
double pwr;//measured chip power from Arduino // F 6
double calcDynPwr; // G 7
double calcChipPwr; // H 8
double estDynErr; // I 9
double estChipPwr; // J 10
double estDynErrAbs; // K 11
double estChipErrAbs; // L 12
string staP5PH; // Place Holder // M 13
double staP5; // N 14
string dynP5PH; // Place Holder // O 15
double dynP5; // P 16
string chipP5PH; // Place Holder // Q 17
double chipP5; // R 18
string staP4PH; // Place Holder // S 19
double staP4; // T 20
string dynP4PH; // Place Holder // U 21
double dynP4; // V 22
string chipP4PH; // Place Holder // W 23
double chipP4; // X 24
string staP3PH; // Place Holder // Y 25
double staP3; // Z 26
string dynP3PH; // Place Holder // AA 27
double dynP3; // AB 28
string chipP3PH; // Place Holder // AC 29
double chipP3; // AD 30
string staP2PH; // Place Holder // AE 31
double staP2; // AF 32
string dynP2PH;// Place Holder // AG 33
double dynP2; // AH 34
string chipP2PH; // Place Holder // AI 35
double chipP2; // AJ 36
string staP1PH; // Place Holder // AK 37
double staP1; // AL 38
string dynP1PH; // Place Holder // AM 39
double dynP1; // AN 40
string chipP1PH; // Place Holder // AO 41
double chipP1; // AP 42
double chipEnergyMeasure; // AQ 43
double chipEnergy5; // AR 44
double chipEnergy4; // AS 45
double chipEnergy3; // AT 46
double chipEnergy2; // AU 47
double chipEnergy1; // AV 48
double junk1; // AW 49
double junk2; // AX 50
};
// There are 48 variables
// Split the line up into an array, 0 - 50 variables starting at 0, Array Size = 51
stepData columnData1, columnData2, columnData3;
// String variable names
string fileData1, fileData2, fileData3;
// Needed for the file.open
ifstream inputFile1, inputFile2, inputFile3;
int main ()
{
// variable name for the number of rows in a spread sheet
int numLines;
int numLines1=0, numLines2=0, numLines3=0;
int rowsCount=0, dataSetCount=0;
int i, j;
// Open one of the files to get the number of lines that you will be working with
inputFile1.open("Statistics_BOB_1400MHz.csv");
inputFile2.open("Statistics_BOB_1700MHz.csv");
inputFile3.open("Statistics_BOB_2300MHz.csv");
// Make sure that all of the input files have the same number of lines
if ( inputFile1.is_open() )
{
//cout << "Opening the " << inputFile_1 << "file \n";
while (getline (inputFile1, fileData1))
{
numLines1++;
}
}
// Close the file
inputFile1.close();
//**********
numLines = numLines1;
//**********
if ( inputFile2.is_open() )
{
//cout << "Opening the " << inputFile_2 << "file \n";
while (getline (inputFile2, fileData2))
{
numLines2++;
}
}
// Close the file
inputFile2.close();
if ( inputFile3.is_open() )
{
//cout << "Opening the " << inputFile_3 << "file \n";
while (getline (inputFile3, fileData3))
{
numLines3++;
}
}
// Close the file
inputFile3.close();
cout << "What is the row count for each file of input" << "\n";
cout << "*************************************************\n";
cout << "***** The rowCount_1 is " << numLines1 << " *****\n";
cout << "***** The rowCount_2 is " << numLines2 << " *****\n";
cout << "***** The rowCount_3 is " << numLines3 << " *****\n";
cout << "*************************************************\n\n";
//cout << "Start int main()" << "\n";
// Working variable names (data imported from each line)
double mesChip[17][11][11][4][4];
double estChip[17][11][11][4][4];
// Create array variable names
double diffChip[17][5][5][4][4]; // find the difference in power
double absDiffChip[17][5][5][4][4]; // find the absolute difference in power
double sumDiffChip[17][5][5][4][4]; // sum up the differences in power
double absSumDiffChip[17][5][5][4][4]; // sum up the absolute differences in power
// Open each results file and iterate import the data from each line of the file.
inputFile1.open("Statistics_BOB_1400MHz.csv");
inputFile2.open("Statistics_BOB_1700MHz.csv");
inputFile3.open("Statistics_BOB_2300MHz.csv");
//cout << "Start line by line import (loop)\n" << endl;
// Iterate through each line of all five files to put
// the data, of each line, into a raw data array
for (rowsCount=1; rowsCount <=16; rowsCount++)
{
cout << "\n ***** Starting rowsCount = " << rowsCount << "*****\n\n";
//DEBUG
//DEBUG
//cout << "\n ***** dataSetCount = " << dataSetCount << "*****\n\n";
//cout << "import file 1" << "\n";
// Iterate through the file
if ( inputFile1.is_open() )
{
inputFile1 >>
//columnData1 <<-- does not exist
columnData1.VEUP >>
columnData1.VEDOWN >>
columnData1.info >>
columnData1.group >>
columnData1.step >>
columnData1.pwr >>
columnData1.calcDynPwr >>
columnData1.calcChipPwr >>
columnData1.estDynErr >>
columnData1.estChipPwr >>
columnData1.estDynErrAbs >>
columnData1.estChipErrAbs >>
columnData1.staP5PH >>
columnData1.staP5 >>
columnData1.dynP5PH >>
columnData1.dynP5 >>
columnData1.chipP5PH >>
columnData1.chipP5 >>
columnData1.staP4PH >>
columnData1.staP4 >>
columnData1.dynP4PH >>
columnData1.dynP4 >>
columnData1.chipP4PH >>
columnData1.chipP4 >>
columnData1.staP3PH >>
columnData1.staP3 >>
columnData1.dynP3PH >>
columnData1.dynP3 >>
columnData1.chipP3PH >>
columnData1.chipP3 >>
columnData1.staP2PH >>
columnData1.staP2 >>
columnData1.dynP2PH >>
columnData1.dynP2 >>
columnData1.chipP2PH >>
columnData1.chipP2 >>
columnData1.staP1PH >>
columnData1.staP1 >>
columnData1.dynP1PH >>
columnData1.dynP1 >>
columnData1.chipP1PH >>
columnData1.chipP1 >>
columnData1.chipEnergyMeasure >>
columnData1.chipEnergy5 >>
columnData1.chipEnergy4 >>
columnData1.chipEnergy3 >>
columnData1.chipEnergy2 >>
columnData1.chipEnergy1 >>
columnData1.junk1 >>
columnData1.junk2;
}
else
{
cout << "Something went wrong! errno " << errno << ": ";
cout << strerror(errno) << "\n";
return 1;
}
//cout << "import file 2" << "\n";
// Iterate through the file
if ( inputFile2.is_open() )
{
inputFile2 >>
//columnData2 <<-- does not exist
columnData2.VEUP >>
columnData2.VEDOWN >>
columnData2.info >>
columnData2.group >>
columnData2.step >>
columnData2.pwr >>
columnData2.calcDynPwr >>
columnData2.calcChipPwr >>
columnData2.estDynErr >>
columnData2.estChipPwr >>
columnData2.estDynErrAbs >>
columnData2.estChipErrAbs >>
columnData2.staP5PH >>
columnData2.staP5 >>
columnData2.dynP5PH >>
columnData2.dynP5 >>
columnData2.chipP5PH >>
columnData2.chipP5 >>
columnData2.staP4PH >>
columnData2.staP4 >>
columnData2.dynP4PH >>
columnData2.dynP4 >>
columnData2.chipP4PH >>
columnData2.chipP4 >>
columnData2.staP3PH >>
columnData2.staP3 >>
columnData2.dynP3PH >>
columnData2.dynP3 >>
columnData2.chipP3PH >>
columnData2.chipP3 >>
columnData2.staP2PH >>
columnData2.staP2 >>
columnData2.dynP2PH >>
columnData2.dynP2 >>
columnData2.chipP2PH >>
columnData2.chipP2 >>
columnData2.staP1PH >>
columnData2.staP1 >>
columnData2.dynP1PH >>
columnData2.dynP1 >>
columnData2.chipP1PH >>
columnData2.chipP1 >>
columnData2.chipEnergyMeasure >>
columnData2.chipEnergy5 >>
columnData2.chipEnergy4 >>
columnData2.chipEnergy3 >>
columnData2.chipEnergy2 >>
columnData2.chipEnergy1 >>
columnData2.junk1 >>
columnData2.junk2;
}
else
{
cout << "Something went wrong! errno " << errno << ": ";
cout << strerror(errno) << "\n";
return 1;
}
//cout << "import file 3" << "\n";
// Iterate through the file
if ( inputFile3.is_open() )
{
inputFile3 >>
//columnData3 <<-- does not exist
columnData3.VEUP >>
columnData3.VEDOWN >>
columnData3.info >>
columnData3.group >>
columnData3.step >>
columnData3.pwr >>
columnData3.calcDynPwr >>
columnData3.calcChipPwr >>
columnData3.estDynErr >>
columnData3.estChipPwr >>
columnData3.estDynErrAbs >>
columnData3.estChipErrAbs >>
columnData3.staP5PH >>
columnData3.staP5 >>
columnData3.dynP5PH >>
columnData3.dynP5 >>
columnData3.chipP5PH >>
columnData3.chipP5 >>
columnData3.staP4PH >>
columnData3.staP4 >>
columnData3.dynP4PH >>
columnData3.dynP4 >>
columnData3.chipP4PH >>
columnData3.chipP4 >>
columnData3.staP3PH >>
columnData3.staP3 >>
columnData3.dynP3PH >>
columnData3.dynP3 >>
columnData3.chipP3PH >>
columnData3.chipP3 >>
columnData3.staP2PH >>
columnData3.staP2 >>
columnData3.dynP2PH >>
columnData3.dynP2 >>
columnData3.chipP2PH >>
columnData3.chipP2 >>
columnData3.staP1PH >>
columnData3.staP1 >>
columnData3.dynP1PH >>
columnData3.dynP1 >>
columnData3.chipP1PH >>
columnData3.chipP1 >>
columnData3.chipEnergyMeasure >>
columnData3.chipEnergy5 >>
columnData3.chipEnergy4 >>
columnData3.chipEnergy3 >>
columnData3.chipEnergy2 >>
columnData3.chipEnergy1 >>
columnData3.junk1 >>
columnData3.junk2;
}
else
{
cout << "Something went wrong! errno " << errno << ": ";
cout << strerror(errno) << "\n";
return 1;
}
cout << "columnData1.VEUP = " << columnData1.VEUP << "\n";
cout << "((columnData1.VEUP * 10.)- 10 = " << ((columnData1.VEUP * 10.)-10);
int VEUPLOOP = (int)(columnData1.VEUP * 10.) - 10;
cout << "VEUPLOOP = " << VEUPLOOP << "\n";
cout << "columnData1.VEDOWN = " << columnData1.VEDOWN << "\n";
cout << "((columnData1.VEUP * 10.)- 10 = " << ((columnData1.VEUP * 10.)-10);
int VEDOWNLOOP = (int)(columnData1.VEDOWN * 10.) - 10;
cout << "VEDOWNLOOP = " << VEDOWNLOOP << "\n";
//cout << "\n\nStart data calculations, VEUPLOOP, VEDOWNLOOP" << "\n\n";
// retrieve the relevant line data from the array
// -- Arduino measured power
mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][1][1] = columnData1.calcChipPwr;
mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][2][2] = columnData2.calcChipPwr;
mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][3][3] = columnData3.calcChipPwr;
// -- projected power data
estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][1][1] = columnData1.chipP1;
estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][2][1] = columnData2.chipP1;
estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][3][1] = columnData3.chipP1;
estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][1][2] = columnData1.chipP2;
estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][2][2] = columnData2.chipP2;
estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][3][2] = columnData3.chipP2;
estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][1][3] = columnData1.chipP3;
estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][2][3] = columnData2.chipP3;
estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][3][3] = columnData3.chipP3;
cout << "\nFrequency 1" << "\n";
cout << "mesChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][1][1] = " << mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][1][1] << " <<--\n";
cout << "estChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][1][1] = " << estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][1][1] << "\n";
cout << "estChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][2][1] = " << estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][2][1] << "\n";
cout << "estChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][3][1] = " << estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][3][1] << "\n";
cout << "\nFrequency 2" << "\n";
cout << "mesChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][2][2] = " << mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][2][2] << " <<--\n";
cout << "estChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][1][2] = " << estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][1][2] << "\n";
cout << "estChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][2][2] = " << estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][2][2] << "\n";
cout << "estChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][3][2] = " << estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][3][2] << "\n";
cout << "\nFrequency 3" << "\n";
cout << "mesChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][3][3] = " << mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][3][3] << " <<--\n";
cout << "estChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][1][3] = " << estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][1][3] << "\n";
cout << "estChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][2][3] = " << estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][2][3] << "\n";
cout << "estChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][3][3] = " << estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][3][3] << "\n\n";
// Calculate the difference in power for the measurements
for (i = 1; i <= 3; i++)
{
for (j = 1; j <= 3; j++)
{
diffChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i] = ((estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i] - mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][i][i])/mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][i][i]);
//diffDataStorage[dataPoint][loop][VEUPLOOP][VEDOWNLOOP][j][i] = diffChip[VEUPLOOP][VEDOWNLOOP][j][i];
cout << "diffChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][" << j << "][" << i << "] = " << \
((estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i] - \
mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][i][i])/mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][i][i]) << \
"\t>>>> " << \
diffChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i] << \
" =(" << estChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i] << \
" - " << mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][i][i] << ") / " <<\
mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][i][i] << "\n";
}
// end of for j diffChip
}
// end of for i diffChip
//add a line spacer
cout << "\n";
for (i = 1; i <= 3; i++)
{
for (j = 1; j <= 3; j++)
{
absDiffChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i] = fabs(diffChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i]);
cout << "absDiffChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][" << j << "][" << i << "] = " << \
fabs(diffChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i]) << "\n";
}
// end of for j diffChip
}
// end of for i diffChip
//add a line spacer
cout << "\n";
for (i = 1; i <= 3; i++)
{
for (j = 1; j <= 3; j++)
{
sumDiffChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i] += diffChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i];
cout << "sumDiffChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][" << j << "][" << i << "] = " << sumDiffChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i] << "\n";
}
// end of for j diffChip
}
// end of for i diffChip
//add a line spacer
cout << "\n";
cout << "\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" << "\n";
for (i = 1; i <= 3; i++)
{
for (j = 1; j <= 3; j++)
{
absSumDiffChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i] += absDiffChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i];
//int remainder = rowsCount % 52;
//if (remainder == 0)
//{
cout << "absSumDiffChip[" << rowsCount << "][" << VEUPLOOP << "][" << VEDOWNLOOP << "][" << j << "][" << i << "] = " << absSumDiffChip[rowsCount][VEUPLOOP][VEDOWNLOOP][j][i] << "\n";
//}
}
// end of for j diffChip
}
// end of for i diffChip
cout << "\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" << "\n";
//add a line spacer
cout << "\n";
// end of dataSetCount
//DEBUG
}
//end of if (rowsCount
inputFile1.close();
inputFile2.close();
inputFile3.close();
return 0;
}
Stop thinking of this as one problem. There are a minimum of three problems here (read in data, process data, output results) so start by breaking the program up with functions.
For example, with a slightly smarter stepData that knows how to read itself from a file:
struct stepData
{
double VEUP; // A 1
double VEDOWN; // B 2
...
double junk2; // AX 50
bool readin(ifstream &inputFile)
{
std::string line;
if (std::getline(inputFile, line)) // read in exactly one line
{
std::stringstream stream(line);// parse the line
if (stream >>
//columnData1 <<-- does not exist
VEUP >>
VEDOWN >>
...
junk2)
{
return true;
}
}
cout << "Something went wrong! errno " << errno << ": ";
cout << strerror(errno) << "\n";
return false;
}
};
The previous version would read off the end of a line and into the next if the file was malformed. It also did not check any of the reads for success. This one does. Note that the errno is not set if chipP5 is fed "Hippo". Better error reporting is still needed, but at least you now know there was an error.
In addition that massive reading block in the middle of main condenses into
if (!(columnData1.readin(inputFile1) &&
columnData2.readin(inputFile2) &&
columnData3.readin(inputFile3)))
{
return -1;
}
If you have a bug in the input, you now have only one place to look. Not three or five or 7000. Also lost about a hundred lines of code.
You can probably do the same with most if not all of your calculation and output code.
To me this looks like a general issue of understanding indexing. In general unless you have a specific reason for doing so, you should deal with your arrays as zero-offset since that's the languages native representation; e.g. a line like:
mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][1][1] = columnData1.calcChipPwr;
you should prefer as:
mesChip[rowsCount][VEUPLOOP][VEDOWNLOOP][0][0] = columnData1.calcChipPwr;
Second, your issue of the "floating point" index. When you are calculating VEUPLOOP and VEDOWNLOOP you transform that aspect of the value away:
int VEUPLOOP = (int)(columnData1.VEUP * 10.) - 10;
so 1 becomes 0 and 1.1 becomes 1. This points out another problem with your code -- all of your data files include VEUP values of 2, which results in a VEUPLOOP value of 10. Not all of your data structures are dimensioned to have valid storage at that index location, so I'd expect to see possibly garbaged values (or a crash) in calculations involving any of your ...Diff structures.
After working on this for a while and some help from a co-worker, here a solution that works.
for (int x = 0; x <=rowsCount; x +=dataGroupSize)
{
double sum_err[6][6];
// set(reset) the error to zero
double grp_err = 0.;
// set the array contents to zero
for (i = 1; i <= testFreqs; i++)
{
for (j = 1; j <= testFreqs; j++)
{
sum_err[i][j] = 0.;
}
}
//
for (int y = 0; y < dataGroupSize; y++)
{
for (i = 1; i <= testFreqs; i++)
{
for (j = 1; j <= testFreqs; j++)
{
sum_err[i][j] += diffChip[x+y][i][j];
}
}
}
for (i = 1; i <= testFreqs; i++)
{
for (j = 1; j <= testFreqs; j++)
{
grp_err += (sum_err[i][j]/dataGroupSize);
if (i ==testFreqs && j==testFreqs)
{
avgErrFile << veup_array[x] << "," << vedown_array[x] << ",grp_err," << (grp_err/25) << "\n";
}
}
}
}
// Calculate absolute data
for (int x = 1; x <=rowsCount; x +=dataGroupSize)
{
double sum_err_abs[6][6];
double grp_err_abs = 0.;
for (i = 1; i <= testFreqs; i++)
{
for (j = 1; j <= testFreqs; j++)
{
sum_err_abs[i][j] = 0.;
}
}
for (int y = 0; y < dataGroupSize; y++)
{
for (i = 1; i <= testFreqs; i++)
{
for (j = 1; j <= testFreqs; j++)
{
sum_err_abs[i][j] += absDiffChip[x+y][i][j];
}
}
}
for (i = 1; i <= testFreqs; i++)
{
for (j = 1; j <= testFreqs; j++)
{
grp_err_abs += (sum_err_abs[i][j]/dataGroupSize);
if (i ==testFreqs && j==testFreqs)
{
absAvgErrFile << veup_array[x] << "," << vedown_array[x] << ",grp_err_abs," << (grp_err_abs/25) << "\n";
}
}
}

Invalid array assignment in a child process

I have a code for a custom-made "mini shell" in which I am parsing a string supplied by the user into individual words in the terminal in a child process.
The following is the child code:
child_pid = fork();
if (child_pid == 0)
{
char* args[100];
int prev_pos = 0;
int pos;
int i = 0;
cout << "Inside loop" << endl;
while((pos = command.find(" ", prev_pos)) != string::npos)
{
//cout << command.substr(prev_pos, pos) << endl;
if (i >= 1)
{
cout << endl << "Before Assignment: " << i - 1 << " " << args[i - 1] <<"kwe"<< endl;
}
args[i] = const_cast<char*>((command.substr(prev_pos, (pos - prev_pos))).c_str());
cout << i << " " << args[i] <<"befre cndition aftr ass"<< endl;
//cout << command.substr(prev_pos, pos) << endl;
if (i >= 1)
{
int j=i-1;
cout << "After Assignment: "<< i - 1 << " " << args[j] <<"eefe"<< endl;
}
cout << i << " " << args[i] << endl;
if (i >= 1)
{
for (int j = 0; j <= i; j++)
{
cout << "\t" << j << " " << args[j] << endl;
}
}
prev_pos = (pos + 1);
i++;
}// 'while' loop
args[i] = const_cast<char*>((command.substr(prev_pos)).c_str());
cout << i << " " << args[i] << endl;
i++;
cout << "Outside loop" << endl << endl;
cout << *(args + 0) << endl << endl;
for (int j = 0; j < i; j++)
{
cout << j << " " << args[j] << endl;
}
args[i] = NULL;
cout << endl << endl << args[0] << endl << endl;
//execvpe(args[0], args, envp);
cout << "Unknown command\n";
exit(0);
}// 'if'
The problem is that whenever I assign a word of the input string to args[i], all the values starting from index 0 to (i - 1) also get assigned the same value. So, if the user enters, lets say, ls asd dajd adakjs, then I end up(at the end of the while loop) with all args[i]s having the value "adakjs" as shown in the following output (the cout statements are just for debugging purposes):
Inside loop
0 lsbefre cndition aftr ass
0 ls
Before Assignment: 0 lskwe
1 asdbefre cndition aftr ass
After Assignment: 0 asdeefe
1 asd
0 asd
1 asd
Before Assignment: 1 asdkwe
2 dajdbefre cndition aftr ass
After Assignment: 1 dajdeefe
2 dajd
0 dajd
1 dajd
2 dajd
3 adakjs
Outside loop
adakjs
0 adakjs
1 adakjs
2 adakjs
3 adakjs
adakjs
Unknown command
Why is this happening? And how can I modify my code so that at the end of the while loop, each of the words get stored into the arg[i]s? Thanks in advance!
The problematic line is this:
args[i] = const_cast<char*>((command.substr(prev_pos, (pos - prev_pos))).c_str());
The substr function returns a string object, of which you get a pointer, and then the string object is destructed, leaving you with a stray pointer to data that doesn't exist any more. This will lead to undefined behavior.
You need to allocate memory for the strings to make them permanent.

C++ - Unwanted characters printed in output file

This is the last part of the program I am working on. I want to output a tabular list of songs to cout. And then I want to output a specially formatted list of song information into fout (which will be used as an input file later on).
Printing to cout works great. The problem is that tons of extra character are added when printing to fout.
Any ideas?
Here's the code:
void Playlist::printFile(ofstream &fout, LinkedList<Playlist> &allPlaylists, LinkedList<Songs*> &library)
{
fout.open("music.txt");
if(fout.fail())
{
cout << "Output file failed. Information was not saved." << endl << endl;
}
else
{
if(library.size() > 0)
fout << "LIBRARY" << endl;
for(int i = 0; i < library.size(); i++) // For Loop - "Incremrenting i"-Loop to go through library and print song information.
{
fout << library.at(i)->getSongName() << endl; // Prints song name.
fout << library.at(i)->getArtistName() << endl; // Prints artist name.
fout << library.at(i)->getAlbumName() << endl; // Prints album name.
fout << library.at(i)->getPlayTime() << " " << library.at(i)->getYear() << " ";
fout << library.at(i)->getStarRating() << " " << library.at(i)->getSongGenre() << endl;
}
if(allPlaylists.size() <= 0)
fout << endl;
else if(allPlaylists.size() > 0)
{
int j;
for(j = 0; j < allPlaylists.size(); j++) // Loops through all playlists.
{
fout << "xxxxx" << endl;
fout << allPlaylists.at(j).getPlaylistName() << endl;
for(int i = 0; i < allPlaylists.at(j).listSongs.size(); i++)
{
fout << allPlaylists.at(j).listSongs.at(i)->getSongName();
fout << endl;
fout << allPlaylists.at(j).listSongs.at(i)->getArtistName();
fout << endl;
}
}
fout << endl;
}
}
}
Here's a sample of the output to music.txt (fout):
LIBRARY
sadljkhfds
dfgkjh
dfkgh
3 3333 3 Rap
sdlkhs
kjshdfkh
sdkjfhsdf
3 33333 3 Rap
xxxxx
PayröÈöè÷÷(÷H÷h÷÷¨÷È÷èøø(øHøhøø¨øÈøèùù(ùHùhùù¨ùÈùèúú(úHúhúú¨úÈúèûû(ûHûhûû¨ûÈûèüü(üHühüü¨üÈüèýý(ýHýhý
! sdkjfhsdf!õüöýÄõ¼5!
sadljkhfds!þõÜö|ö\
þx þ  þÈ þð ÿ ÿ# ÿh ÿ ÿ¸ ÿà 0 X ¨ Ð ø
enter code here
enter code here
Most likely, one of your methods returns an improper char * string (not null terminated).
Edit: actually, not just one: getPlaylistName(), getSongName() and getArtistName().