C++ issues with adding variables to a vector list

Viewed 61

I've created a program that reads different text files and stores each file in its own Vector list. However, the vector list proportion of the program is not working. Provided below is the text file im currently working on as well the program itself

Text file

A:Head:1:2:15.
B:Torso:0:6:5.
C:Leg:0:4:6.
D:Arm:0:4:8.
E:Tail:0:6:2.

Main file

#include <iostream>
#include <string>
#include <conio.h>
#include <stdio.h>
#include "driver.h"
#include "implementation.cpp"
#include <fstream>
#include <vector>
using namespace std;

int main() {
readFile();
writeFile();
robotComplexity();
getch();
return 0;
}

Implementation file containing functions


#include <iostream>
#include <string>
#include <sstream> 
#include <fstream>
#include <fstream>
#include <vector>
using namespace std;

//declaration of parts variables
char partCode;
std::string partName;
int maximum;
int minimum;
int complexity;

std::vector<string> partsVector;
std::ifstream partsList("Parts.txt");
std::string outputFile = "output.txt";
std::string input;

std::string newChar;
std::stringstream convertChar;

void readFile() //function to read Builders, Customers and Parts text file
{

   std::string line;

while (std::getline(partsList, line)) {
    line.pop_back();//removing '.' at end of line
    std::string token;
    std::istringstream ss(line);

    convertChar << partCode;
    convertChar >> newChar;


    // then read each element by delimiter
    int counter = 0;//number of elements you read
    while (std::getline(ss, token, ':')) {//spilt into different records
      switch (counter) {//put into appropriate value-field according to element-count

      case 0:
        newChar = token; //convert partCode from a char to a string 
        break;
      case 1:
        partName = token;
        break;
      case 2: maximum = stoi(token);
        break;
      case 3: minimum = stoi(token);
        break;
        case 4: complexity = stoi(token);
        break;
      default:
        break;
      }
      counter++;//increasing counter
    }

    partsVector.push_back(newChar);

    for(string x: partsVector)
    cout << x << endl;
}

}


double robotComplexity() { 


double complexity;

for(int i = 1; i < partsVector.size(); i++)
/*
if(newChar == "A") {
   cout << "Character: " << newChar;
} else   {
   cout << "Program isnt working! :(";


} */
   if(complexity > 100) {
      complexity = 100;
   }


cout << "\nThe Robot Complexity is: " << complexity << endl;
return complexity;
}


double robotVariability() {

double variability;


cout << "\nThe Robot Variability is: " << variability << endl;
return variability;

}

void writeFile() //writes to a file output.txt the end calculations. 
{

}

The code that im currently experiencing issues with is the following

while (std::getline(partsList, line)) {
    line.pop_back();//removing '.' at end of line
    std::string token;
    std::istringstream ss(line);

    convertChar << partCode;
    convertChar >> newChar;

    // then read each element by delimiter
    int counter = 0;//number of elements you read
    while (std::getline(ss, token, ':')) {//spilt into different records
      switch (counter) {//put into appropriate value-field according to element-count

      case 0:
        newChar = token; //convert partCode from a char to a string 
        break;
      case 1:
        partName = token;
        break;
      case 2: maximum = stoi(token);
        break;
      case 3: minimum = stoi(token);
        break;
        case 4: complexity = stoi(token);
        break;
      default:
        break;
      }
      counter++;//increasing counter
    }

    partsVector.push_back(newChar);

    for(string x: partsVector)
    cout << x << endl;
}

When this program is compiled and executed, the following is printed out to the console

A
A
B
A
B
C
A
B
C
D
A
B
C
D
E

This is clearly wrong, as it should be printing one of each letter, as required by my program specs.

A
B
C
D
E

The purpose of this is so i know the function can successfully identify Each Record. To note, this occurs with other varialbes such as partName. Ive deducted that it is an issue with how I'm adding the variables to the vector but I am unsure why. Any help would be great Thankyou.

1 Answers

So the problem is simply where you are printing out the partsVector, it's inside your reading loop when it should be after the reading loop. So it should be this

while (std::getline(partsList, line)) {
    ...
    partsVector.push_back(newChar);
}

for(string x: partsVector)
    cout << x << endl;

instead of this

while (std::getline(partsList, line)) {
    ...
    partsVector.push_back(newChar);
    for(string x: partsVector)
        cout << x << endl;
}

Because you are printing the parts vector as you read it in, you get those repeated values displayed.

Related