Setting up precision C++ - c++

So, i'm attempting to set the precision for input values in my code. I want the values to be printed with two decimal points afterwards of precision though i'm not exactly sure how.
Here's my code.
#include <iostream>
#include <iomanip>
float uphill, wellD, waterLvl, buckVol;
float buckAscRate, downHill, volume;
float last;
float timeReq;
int scene = 1;
void timeRequired()
{
std::setw(2);
std::setprecision(2);
std::cout << "Scenario " << scene << ":" << std::endl;
std::cout << "up hill" << " " << uphill << " sec" << std::endl;
std::cout << "well diamter" << " " << wellD << " in" << std::endl;
std::cout << "water level" << " " << waterLvl << " in" << std::endl;
std::cout << "bucket volume" << " " << buckVol << " cu ft" << std::endl;
std::cout << "bucket ascent rate" << " " << buckAscRate << " in/sec" << std::endl;
std::cout << "down hill" << " " << downHill << " sec" << std::endl;
std::cout << "required volume" << " " << volume << " cu ft" << std::endl;
timeReq = (uphill + downHill);
std::cout << "TIME REQUIRED" << " " << timeReq << " sec" << std::endl;
std::cout << " " << std::endl;
}
void scenarioCONT()
{
do
{
std::cin >> wellD;
std::cin >> waterLvl;
std::cin >> buckVol;
std::cin >> buckAscRate;
std::cin >> downHill;
std::cin >> volume;
std::cin >> last;
if (uphill <= 1) uphill = 2;
if (wellD <= 0) wellD = 1;
if (waterLvl <= 0) waterLvl = 1;
if (buckVol <= 0) buckVol = 1;
if (buckAscRate <= 0) buckAscRate = 1;
if (downHill <= 0) buckAscRate = 1;
if (volume <= 0) volume = 1;
if (last > 1)
{
uphill = last;
scenarioCONT();
}
} while (last != 0);
}
void scenario()
{
do
{
std::cin >> uphill;
std::cin >> wellD;
std::cin >> waterLvl;
std::cin >> buckVol;
std::cin >> buckAscRate;
std::cin >> downHill;
std::cin >> volume;
std::cin >> last;
if (uphill <= 1) uphill = 2;
if (wellD <= 0) wellD = 1;
if (waterLvl <= 0) waterLvl = 1;
if (buckVol <= 0) buckVol = 1;
if (buckAscRate <= 0) buckAscRate = 1;
if (downHill <= 0) buckAscRate = 1;
if (volume <= 0) volume = 1;
if (last > 1)
{
timeRequired();
uphill = last;
scenarioCONT();
}
scene++;
timeRequired();
} while (last != 0);
}
int main()
{
scenario();
system("pause");
}
I've been told to use ionmanip to set the precision, though i'm not 100% on how to do it. Any suggestions?

You can use std::setprecision function. The below example is directly taken from http://www.cplusplus.com/reference/iomanip/setprecision/
double f =3.14159;
std::cout << std::setprecision(5) << f << '\n';
std::cout << std::setprecision(9) << f << '\n';

If you want to output 2 decimal digits, you should use std::fixed, together with std::setprecision().
Take a look here.
For easier understanding, here is an example:
cout << setprecision(2)<< 3.1415; outputs 3.1 (2 digits in total) and
cout << setprecision(2)<<fixed<< 3.1415; outputs 3.14 (2 digits after floating point)

Related

Input File Reading Just Repeats First Data Line from 1 of 2 Files

I'm working on a project. The idea is that it has two input files, we'll call them TimeFile and FullFile.
TimeFile gives two timestamps in the format:
DD-MM-YY HH:MM:SS DD-MM-YY HH:MM:SS
And FullFile is in the format of a timestamp and some data:
YYYY-MM-DD HH:MM:SS Value Error Error
The idea is that the program reads the timestamps from TimeFile, then goes through all the lines in FullFile, and if it finds a timestamp from FullFile that lands between two from TimeFile, it copies the whole line into a new smaller data file. In essence, I want to go from one gigantic data file to a bunch of smaller data files divided up by time intervals.
Needless to say, it doesn't work. It does most of what I want, but the resulting smaller data files are always empty.
The strange part is why. It appears to read the timestamps from TimeFile just fine, but it screws up reading from FullFile and just reads the first line over and over again. I don't really have a solid idea why either, best I can determine is that they're reading the same way, but one works and the other doesn't.
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <tuple>
#include <vector>
#include <stdio.h>
#include <math.h>
#include <complex>
#include <stdint.h>
#include <time.h>
#include <string.h>
int main(){
//Read Run-Times-File - Cycle 1
std::ifstream TimeFile;
TimeFile.open("jet_run_times.dat");
if(!TimeFile.good()){
std::cout << "TimeFile Didn't Work" << std::endl;
return 1;
}
//Read Full-File - Cycle 2
std::ifstream FullFile;
FullFile.open("jet_full.txt");
if(!FullFile.good()){
std::cout << "FullFile Didn't Work" << std::endl;
return 1;
}
std::cout << "Both Files Worked" << std::endl;
std::ofstream results;
//Initial Time - A ; Final Time - B ; Jet Pressure Time - C
int iday, ihour, imin, isec, fday, fhour, fmin, fsec, imon, fmon;
char dash, colon ;
std::string imonth, fmonth ;
//std::ostringstream temp;
int run = 0;
int year, month, day , hour, min;
double sec, value, neg, pos, AfterStart, BeforeEnd;
for(int i = 0 ; i < 192 ; i++) {
//Cycle 1
//Write jet_run_XXX.txt
run++;
std::ostringstream temp;
if(run <= 9) temp << "jet_run_00" << run << ".txt" ;
if( (run >= 10) && (run <= 99) ) temp << "jet_run_0" << run << ".txt" ;
if(run >= 100) temp << "jet_run_" << run << ".txt" ;
results.open(temp.str());
std::cout << temp.str() << " File Made" << std::endl;
TimeFile >> iday >> dash >> imonth >> ihour >> colon >> imin >> colon >> isec >> fday >> dash >> fmonth >> fhour >> colon >> fmin >> colon >> fsec;
/*
std::cout << "iday " << iday << std::endl;
std::cout << "dash " << dash << std::endl;
std::cout << "imonth " << imonth << std::endl;
std::cout << "ihour " << ihour << std::endl;
std::cout << "colon " << colon << std::endl;
std::cout << "imin " << imin << std::endl;
std::cout << "isec " << isec << std::endl;
std::cout << "fday " << fday << std::endl;
std::cout << "dash " << dash << std::endl;
std::cout << "fmonth " << fmonth << std::endl;
std::cout << "fhour " << fhour << std::endl;
std::cout << "colon " << colon << std::endl;
std::cout << "fmin " << fmin << std::endl;
std::cout << "fsec " << fsec << std::endl;
*/
if( imonth == "Apr-22") imon = 4;
if( fmonth == "Apr-22") fmon = 4;
if( imonth == "May-22") imon = 5;
if( fmonth == "May-22") fmon = 5;
/*
std::cout << "imon " << imon << std::endl;
std::cout << "fmon " << fmon << std::endl;
*/
//Cycle 2
for(int j = 0 ; j < 5833 ; j++){
FullFile >> year >> dash >> month >> dash >> day >> hour >> colon >> min >> colon >> sec >> value >> neg >> pos;
std::cout << j << std::endl;
/*
std::cout << "year " << year << std::endl;
std::cout << "dash " << dash << std::endl;
std::cout << "month " << month << std::endl;
std::cout << "dash " << dash << std::endl;
std::cout << "day " << day << std::endl;
std::cout << "hour " << hour << std::endl;
std::cout << "colon " << colon << std::endl;
std::cout << "min " << min << std::endl;
std::cout << "sec " << sec << std::endl;
std::cout << "value " << value << std::endl;
std::cout << "neg " << neg << std::endl;
std::cout << "pos " << pos << std::endl;
*/
//Set-Up the Check if A <= C <= B
AfterStart = (sec - isec) + (min - imin)*100 + (hour - ihour)*10000 + (day - iday)*1000000 + (month - imon)*100000000;
BeforeEnd = (fsec - sec) + (fmin - min)*100 + (fhour - hour)*10000 + (fday - day)*1000000 + (fmon - month)*100000000;
std::cout << "AfterStart " << AfterStart << std::endl;
std::cout << "BeforeEnd " << BeforeEnd << std::endl;
//If A <= C <= B, copy all of C to jet_run_XXX.txt
if ( (AfterStart >= 0.0) && (BeforeEnd >= 0.0) ){
results << year << dash << month << dash << day << " " << hour << colon << min << colon << sec << " " << value << " " << pos << " " << neg << '\n' << std::endl;
std::cout << "Got One!" << std::endl;
}
}
//End Cycle 2
results.close();
}
//End Cycle 1
return 0;
}
I'm stumped, any help would be appreciated.
Edit: Thinking it might help if I give a few lines of each of my data files to see if it's a formatting issue, so here's the first 3 lines of each:
TimeFile
03-Apr-22 22:42:19 03-Apr-22 22:56:13
03-Apr-22 22:58:25 03-Apr-22 23:15:14
03-Apr-22 23:17:23 03-Apr-22 23:35:32
FullFile
2022-04-13 12:39:37.500000000 70.00000 0.0 0.0
2022-04-13 12:43:52.500000000 70.00000 0.0 0.0
2022-04-13 12:48:07.500000000 70.00000 0.0 0.0
Edit2:- Important discovery. Running this code with that block uncommented out reproduces the data in the line but instead of 70 0 0 you get 1 0 43.1495 for the last three values. GetLine doesn't seem to do this but I'm having trouble understanding how to cut that open.
Not entirely sure what this means beyond that somehow that's not reading those values properly but when I try to account for a tab it's not an improvement.

Football tournament with matrix

I'm trying to make a football tournament in C++, in which the user inputs the name of the teams(8 teams) and then each team has to play with the other one 1 time. Firstly, I don't know how to read the team names, I mean I tried to use .getline or just cin of a char array, but then I need to put the teams into the matrix and after the final game my program should print the table. So there's the first question: how to read the names and basically make the program think they are numbers or does it work with just with names, no need to use int? And then the users inputs the result for every game, but here comes the hard part. After all the results have been introduced, the matrix rotates cyclic and then the result stored in the variables(you will see in code victory/losses) overwrites themselves, so at the end, I cannot print the right table. So that's the second question: How can I make them store to the right 'team' while they rotate? Sorry if I didn't quite explain very well how it works, hope you understand it. Cheers!
// FOOTBALL TOURNAMENT
int map[2][4];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
cout << "map[" << i << "][" << j << "]= ";
cin >> map[i][j];
}
}
cout << "The map looks like this:" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
cout << map[i][j] << " ";
}
cout << endl;
}
map[0][0] = 1;
int temp = 0, temp2 = 0, temp3 = 0, temp4 = 0, temp5 = 0, temp6 = 0;
int a, b, c, d, e, f, g, h, round = 0;
int victory_m00(0), losses_m10(0), victory_m10(0), losses_m00(0), victory_m01(0), losses_m11(0), victory_m11(0), losses_m01(0);
int victory_m02(0), losses_m12(0), victory_m12(0), losses_m02(0), victory_m03(0), losses_m13(0), victory_m13(0), losses_m03(0);
do
{
// Insert result for every game
cout << "Enter the result of the first game between " << map[0][0] << " vs. " << map[1][0] << endl;
cin >> a >> b;
if (a > b) {
victory_m00++;
losses_m10++;
}
else if (a < b)
{
victory_m10++;
losses_m00++;
}
cout << "Enter the result of the first game between: " << map[0][1] << " vs. " << map[1][1] << endl;
cin >> c >> d;
if (c > d) {
victory_m01++;
losses_m11++;
}
else if (c < d)
{
victory_m11++;
losses_m01++;
}
cout << "Enter the result of the first game between: " << map[0][2] << " vs. " << map[1][2] << endl;
cin >> e >> f;
if (e > f) {
victory_m02++;
losses_m12++;
}
else if (e < f)
{
victory_m12++;
losses_m02++;
}
cout << "Enter the result of the first game between: " << map[0][3] << " vs. " << map[1][3] << endl;
cin >> g >> h;
if (g > h) {
victory_m03++;
losses_m13++;
}
else if (g < h)
{
victory_m13++;
losses_m03++;
}
round++;
// Map switching
temp = map[1][0];
map[1][0] = map[0][1];
temp2 = map[1][1];
map[1][1] = temp;
temp3 = map[1][2];
map[1][2] = temp2;
temp4 = map[1][3];
map[1][3] = temp3;
temp5 = map[0][3];
map[0][3] = temp4;
temp6 = map[0][2];
map[0][2] = temp5;
map[0][1] = temp6;
// Table calculating and printing ~ also this has to be outside the loop (but at first i wanted to print the table after every 'round'
cout << "This is how the table looks like after the " << round << " round: \n";
cout << map[0][0] << " has: " << victory_m00 << " victory(-ies) and " << losses_m00 << " loss-es!\n";
cout << map[0][1] << " has: " << victory_m01 << " victory(-ies) and " << losses_m01 << " loss-es!\n";
cout << map[0][2] << " has: " << victory_m02 << " victory(-ies) and " << losses_m02 << " loss-es!\n";
cout << map[0][3] << " has: " << victory_m03 << " victory(-ies) and " << losses_m03 << " loss-es!\n";
cout << map[1][0] << " has: " << victory_m10 << " victory(-ies) and " << losses_m10 << " loss-es!\n";
cout << map[1][1] << " has: " << victory_m11 << " victory(-ies) and " << losses_m11 << " loss-es!\n";
cout << map[1][2] << " has: " << victory_m12 << " victory(-ies) and " << losses_m12 << " loss-es!\n";
cout << map[1][3] << " has: " << victory_m13 << " victory(-ies) and " << losses_m13 << " loss-es!\n";
cout << endl;
cout << endl;
} while (map[0][1] != 2);
```

Globalisation of variables within code block

I am having trouble with using the array/matrix which was created within the first if statement (if (choice == 1)) at a later point within the code. I would like to edit the array (when I mention if (choice == 2), but the array created within the first "if" code block is localised. How can I get around this?
#include "pch.h"
#include <iostream>
#include <vector>
int main()
{
if (choice == 1)
{
std::cout << "Choose a letter from A to C, where A = 1" << std::endl;
short matNamechoice;
std::cin >> matNamechoice;
if (matNamechoice == 1)
{
bool matAlive = true;
std::vector<int> matrixA(0);
std::cout << "How many elements would you like to add to Matrix" << matNamechoice << "?" << std::endl;
short matAelements;
std::cin >> matAelements;
std::cout << "For the " << matAelements << "elements that you have created, please choose \
each element value: " << std::endl;
for (int n = 0; n < matAelements; ++n)
{
std::cout << "ELEMENT NO " << n << ":" << std::endl;
int elementValue = 0;
std::cin >> elementValue;
matrixA.push_back(elementValue);
}
std::cout << matAelements << " elements have been appended to matrixA." << std::endl;
}
if (matNamechoice == 2)
{
bool matBLive = true;
std::vector<int> matrixB(0);
std::cout << "How many elements would you like to add to Matrix " << matNamechoice << "?" << std::endl;
short matBelements;
std::cin >> matBelements;
std::cout << "For the " << matBelements << "elements that you have created, please choose \
each element value: " << std::endl;
for (int n = 0; n < matBelements; ++n)
{
std::cout << "ELEMENT NO " << n << ":" << std::endl;
int elementValue = 0;
std::cin >> elementValue;
matrixB.push_back(elementValue);
}
std::cout << matBelements << " elements have been appended to matrixB." << std::endl;
}
if (matNamechoice == 3)
{
bool matCLive = true;
std::vector<int> matrixC(0);
std::cout << "How many elements would you like to add to Matrix" << matNamechoice << "?" << std::endl;
short matCelements;
std::cin >> matCelements;
std::cout << "For the " << matCelements << "elements that you have created, please choose \
each element value: " << std::endl;
for (int n = 0; n < matCelements; ++n)
{
std::cout << "ELEMENT NO " << n << ":" << std::endl;
int elementValue = 0;
std::cin >> elementValue;
matrixC.push_back(elementValue);
}
std::cout << matCelements << " elements have been appended to matrixC. \n \n" << std::endl;
}
}
if (choice == 2 && (matAlive == true))
std::cout << "LOADING MATRICIES..." << std::endl;
for (int n = 10; n <= 100; n += 10)
{
std::cout << n << "%" << std::endl;
}
std::cout << "MATRIX DATA LOAD SUCCESSFUL..." << std::endl;
std::cout << "Enter 1 to edit a Matrix. Enter 2 to print a Matrix. Enter 3 to clear a Matrix." << std::endl;
short userChoice = 0;
std::cin >> userChoice;
}
int x = 0;
if (x < 100)
{
int y = 1;
}
return 0;
}
Move the variable outside of inner block:
bool matAlive = false;
std::vector<int> matrixA(0);
if (choice == 1) {
if (matNamechoice == 1) {
matAlive = true;
// ...
}
}
// ...
if (choice == 2 && (matAlive == true)) {
// ...
}

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";
}
}
}

Newbie in C++. First text game

I study C ++ and try to create the first game. Here's the code:
#include <iostream>
#include <string>
using namespace std;
void info () {
int LVL = 1;
int money = 1000;
int EXP = 0;
int work = 200;
int learn = 15;
int k = 0;
}
void menu ()
{
info ();
char menu_items;
cout << "Choose action: " << "\n" << "1. Work" << "\n" << "2. Learn" << endl;
cin >> k;
if (k == 1){
int money = money + work;
cout << "U worked (+ "<< money << " dollars)" << "\n" << endl;
} if (k == 2) {
int EXP = EXP + learn;
cout << "U learned (+ " << EXP << " EXP)" << "\n" << endl;
} else {
cout << "ERROR" << endl;
}
}
int main()
{
info ();
while (LVL == 10) {
cout << "End game!";
}
while (LVL != 10) {
cout << "Your data: " << "\n" << "Money: " << money << "\n" << "EXP: " << EXP << "\n" << "LVL: " << LVL << "\n" << endl;
menu ();
}
}
Please correct the following:
1) The cyclic output data after rewrite
2) Proper cycle add money and experience when choosing one of the following actions
Info should probably be a class or struct. You only want to instantiate it once, and persist the values over your calls.
One option might be:
#include <iostream>
#include <string>
using namespace std;
struct info {
int lvl = 1;
int money = 1000;
int exp = 0;
const int work = 200;
const int learn = 15;
};
int main()
{
info i;
string k;
while (i.lvl < 10)
{
cout << "Your data: " << "\n" << "Money: " << i.money << "\n" << "EXP: " << i.exp << "\n" << "LVL: " << i.lvl << endl;
cout << "Choose action: " << "\n" << "1. Work" << "\n" << "2. Learn" << endl;
cin >> k;
if (k == "1")
{
i.money += i.work;
cout << "You worked (+ " << i.work << " dollars, now " << i.money << ")" << endl;
}
else if (k == "2")
{
i.exp += i.learn;
cout << "You learned (+ " << i.learn << " EXP, now " << i.exp << ")" << endl;
}
else
{
cout << "ERROR" << endl;
}
}
cout << "You won!" << endl;
}
#include <iostream>
#include <string>
using namespace std;
void menu()
{
int money = 1000;
int EXP = 0;
int work = 200;
int learn = 15;
int k = 0;
cout << "Choose action: \n 1. Work \n 2. Learn "<< endl;
cin >> k;
if (k == 1){
money =money + work;
cout << "U worked (+ " << money << " dollars)" << "\n" << endl;
} if (k == 2) {
EXP = EXP + learn;
cout << "U learned (+ " << EXP << " EXP)" << "\n" << endl;
}
else {
cout << "ERROR" << endl;
}
}
int main()
{
int money = 1000;
int LVL = 1;
int EXP = 0;
while (LVL == 10) {
`cout << "End game!"; `
}
while (LVL != 10) {
cout << "Your data: " << "\n" << "Money: " << money << "\n" << "EXP: " << EXP << "\n" << "LVL: " << LVL << "\n" << endl;
menu();
}
return 0;
}