Trying to create a running total with many data types

I am trying to create a program that only adds the distance of the feeder if all of the feeder "attributes" are the same (except for the distance). If the attributes are not the same, I want to append onto the output file, but if the attributes match with a feeder that is already stored in the output file, I want it to just add the distance to that matching feeder and keep the other data already being stored in the file. Please help!

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

int main()
{
int menu1;
int conduitNum;
int conduitType;
int conductorNum;
int conductorType;
int conductorSize;
int groundSize;
double distance;
double conduitSize;
char menu2;
char menu3;
char menu4;
string fileName;
string conduitTypeS;
string conductorTypeS;
string conductorSizeS;
string groundSizeS;
ifstream inputFile;
ofstream outputFile;

cout << "(1)New File or (2)Open Existing?\n";
cin >> menu1;

if (menu1 == 1)
{
cout << "\nPlease enter a name for the file: ";
cin >> fileName;
cout << "\nThank you. Now creating \"" << fileName << "\"\n" << endl;
ofstream outputFile;
outputFile.open(fileName);
system("pause");
outputFile.close();
}

else if (menu1 == 2)
{
cout << "\nPlease enter name of file: ";
cin >> fileName;
cout << "\nThank you, now opening \"" << fileName << "\"\n" << endl;
ifstream inputFile;
system("pause");
inputFile.open(fileName);
}

else
{
cout << "Invalid choice, please retry. ";
system("pause");
return main();
}

do
{
double distanceInput;
int conduitNumInput;
double conduitSizeInput;
string conduitTypeSInput;
int conductorNumInput;
string conductorSizeSInput;
string conductorTypeSInput;
string groundSizeSInput;
string groundTypeSInput;

inputFile.open(fileName);
inputFile >> distanceInput;
inputFile >> conduitNumInput;
inputFile >> conduitSizeInput;
inputFile >> conduitTypeSInput;
inputFile >> conductorNumInput;
inputFile >> conductorSizeSInput;
inputFile >> conductorTypeSInput;
inputFile >> groundSizeSInput;
inputFile >> groundTypeSInput;

start:
cout << "\nFeeder distance (feet): ";
cin >> distance;

//Conduit
cout << "\nNumber of conduits: ";
cin >> conduitNum;
cout << "\nType of conduit:\n1. EMT\n2. IMC\n3. PVC\n";
cin >> conduitType;
switch (conduitType)
{
case 1: conduitTypeS = "EMT";
break;
case 2: conduitTypeS = "IMC";
break;
case 3: conduitTypeS = "PVC";
break;
default: cout << "Invalid selection. ";
system("pause");
return main();
}
cout << "\nSize of " << conduitTypeS << " conduit (inches): ";
cin >> conduitSize;

//Conductor
cout << "\nNumber of conductors: ";
cin >> conductorNum;
cout << "\nType of conductor:\n1. Copper\n2. Aluminum\n";
cin >> conductorType;
switch (conductorType)
{
case 1: conductorTypeS = "Copper";
break;
case 2: conductorTypeS = "Aluminum";
break;
default: cout << "Invalid selection. ";
system("pause");
return main();
}
cout << "\nSize of " << conductorTypeS << " conductor:\n";
cout << "1. #14 11. #2/0\n";
cout << "2. #12 12. #3/0\n";
cout << "3. #10 13. #4/0\n";
cout << "4. #8 14. #250MCM\n";
cout << "5. #6 15. #300MCM\n";
cout << "6. #4 16. #350MCM\n";
cout << "7. #3 17. #400MCM\n";
cout << "8. #2 18. #500MCM\n";
cout << "9. #1 19. #600MCM\n";
cout << "10. #1/0 20. #750MCM\n";
cin >> conductorSize;
switch (conductorSize)
{
case 1: conductorSizeS = "#14";
break;
case 2: conductorSizeS = "#12";
break;
case 3: conductorSizeS = "#10";
break;
case 4: conductorSizeS = "#8";
break;
case 5: conductorSizeS = "#6";
break;
case 6: conductorSizeS = "#4";
break;
case 7: conductorSizeS = "#3";
break;
case 8: conductorSizeS = "#2";
break;
case 9: conductorSizeS = "#1";
break;
case 10: conductorSizeS = "#1/0";
break;
case 11: conductorSizeS = "#2/0";
break;
case 12: conductorSizeS = "#3/0";
break;
case 13: conductorSizeS = "#4/0";
break;
case 14: conductorSizeS = "#250MCM";
break;
case 15: conductorSizeS = "#300MCM";
break;
case 16: conductorSizeS = "#350MCM";
break;
case 17: conductorSizeS = "#400MCM";
break;
case 18: conductorSizeS = "#500MCM";
break;
case 19: conductorSizeS = "#600MCM";
break;
case 20: conductorSizeS = "#750MCM";
break;
default: cout << "Invalid selection. ";
system("pause");
return main();
}

//Ground
cout << "\nSize of " << conductorTypeS << " ground:\n";
cout << "1. #14 11. #2/0\n";
cout << "2. #12 12. #3/0\n";
cout << "3. #10 13. #4/0\n";
cout << "4. #8 14. #250MCM\n";
cout << "5. #6 15. #300MCM\n";
cout << "6. #4 16. #350MCM\n";
cout << "7. #3 17. #400MCM\n";
cout << "8. #2 18. #500MCM\n";
cout << "9. #1 19. #600MCM\n";
cout << "10. #1/0 20. #750MCM\n";
cin >> groundSize;
switch (groundSize)
{
case 1: groundSizeS = "#14";
break;
case 2: groundSizeS = "#12";
break;
case 3: groundSizeS = "#10";
break;
case 4: groundSizeS = "#8";
break;
case 5: groundSizeS = "#6";
break;
case 6: groundSizeS = "#4";
break;
case 7: groundSizeS = "#3";
break;
case 8: groundSizeS = "#2";
break;
case 9: groundSizeS = "#1";
break;
case 10: groundSizeS = "#1/0";
break;
case 11: groundSizeS = "#2/0";
break;
case 12: groundSizeS = "#3/0";
break;
case 13: groundSizeS = "#4/0";
break;
case 14: groundSizeS = "#250MCM";
break;
case 15: groundSizeS = "#300MCM";
break;
case 16: groundSizeS = "#350MCM";
break;
case 17: groundSizeS = "#400MCM";
break;
case 18: groundSizeS = "#500MCM";
break;
case 19: groundSizeS = "#600MCM";
break;
case 20: groundSizeS = "#750MCM";
break;
default: cout << "Invalid selection. ";
system("pause");
return main();
}
string groundTypeS = conductorTypeS;

cout << "\n\nIs the following information correct (Y or N)?\n";
cout << distance << "' of (" << conduitNum << ") " << conduitSize << "\" ";
cout << conduitTypeS << " w/ (" << conductorNum << ") " << conductorSizeS;
cout << " " << conductorTypeS << " conductors and (1) " << groundSizeS;
cout << " " << groundTypeS << " ground.\n";
cin >> menu3;
menu3 = toupper(menu3);

if (menu3 == 'Y')
{
cout << "\n";
system("pause");
}

else if (menu3 == 'N')
{
cout << "\nWould you like to re-enter the data (Y or N)?\n";
cin >> menu4;
menu4 = toupper(menu4);
if (menu4 == 'Y')
{
goto start;
}
else
{
cout << "Program will now close. ";
system("pause");
}
}

if (
conduitNum == conduitNumInput &&
conduitSize == conduitSizeInput &&
conduitTypeS == conduitTypeSInput &&
conductorNum == conductorNumInput &&
conductorSizeS == conductorSizeSInput &&
conductorTypeS == conductorTypeSInput &&
groundSizeS == groundSizeSInput&&
groundTypeS == groundTypeSInput)
{
distance += distanceInput;
outputFile.open(fileName);
outputFile << distance << endl;
outputFile << conduitNum << endl;
outputFile << conduitSize << endl;
outputFile << conduitTypeS << endl;
outputFile << conductorNum << endl;
outputFile << conductorSizeS << endl;
outputFile << conductorTypeS << endl;
outputFile << groundSizeS << endl;
outputFile << groundTypeS << endl;
outputFile.close();
}

else
{
outputFile.open(fileName, ofstream::app);
outputFile << distance << endl;
outputFile << conduitNum << endl;
outputFile << conduitSize << endl;
outputFile << conduitTypeS << endl;
outputFile << conductorNum << endl;
outputFile << conductorSizeS << endl;
outputFile << conductorTypeS << endl;
outputFile << groundSizeS << endl;
outputFile << groundTypeS << endl;
outputFile.close();
}


cout << "Your data has been saved to \"" << fileName << "\"\n";
cout << "Would you like to add another feeder (Y or N)?\n";
cin >> menu2;
menu2 = toupper(menu2);
} while (menu2 == 'Y');
}
I may or may not look at this later, depends if I am still busy, but it would be more help to other forum members reading this if you gave more information as to what exactly is going wrong -- not just a source code dump.

- What is your input? If it's a file, show us the file.
- What is your expected output?
- What is your actual output?
Those three things are the basis for how people make unit tests for their code.

Also, read: http://sscce.org/

Side note: main() should not be recursive, this should be refactored into a loop. It's technically against the standard, but probably not the source of your problem. (You also have a LOT of repeated code patterns that could be shortened a lot using for loops and arrays)
Last edited on
Thanks Ganado,

Sorry, this is my first time posting to this forum, and I'm also fairly new to C++. The inputs for this program are distance, conduitNum, conduitSize, conduitType, conductorNum, conductorSize, conductorType, and groundSize. I want the program to prompt the user for this information and then compare all of those attributes to those that were previously entered. If every attribute (besides distance) matches, I want it to add the new distance and previous together and save it to a file. I am able to get the program to do this only if I consecutively enter the same attributes, but if the similar attributes are not entered consecutively, the program overwrites the stored data onto the output file.
I know that this might sound a bit confusing, but I'm trying the best I can to explain it.
Topic archived. No new replies allowed.