我创建了一个程序,可以读取不同的文本文件并将每个文件存储在其自己的 vector 列表中。然而,该程序的 vector 列表比例不起作用。下面提供的是我当前正在处理的文本文件以及程序本身
文本文件
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.
主文件
#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;
}
包含函数的实现文件
#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.
{
}
我当前遇到问题的代码如下
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;
}
当这个程序被编译并执行时,以下内容将打印到控制台
A
A
B
A
B
C
A
B
C
D
A
B
C
D
E
这显然是错误的,因为它应该按照我的程序规范的要求打印每个字母之一。
A
B
C
D
E
这样做的目的是让我知道该函数可以成功识别每个记录。需要注意的是,其他变量(例如partName)也会出现这种情况。我推断这是我如何将变量添加到 vector 的问题,但我不确定为什么。任何帮助都会很棒谢谢。
最佳答案
所以问题就在于您打印 partsVector
的位置,它在您的读取循环内,而它应该在读取循环之后。所以应该是这样的
while (std::getline(partsList, line)) {
...
partsVector.push_back(newChar);
}
for(string x: partsVector)
cout << x << endl;
而不是这个
while (std::getline(partsList, line)) {
...
partsVector.push_back(newChar);
for(string x: partsVector)
cout << x << endl;
}
因为您在读入零件 vector 时打印它,所以您会显示这些重复的值。
关于C++ 将变量添加到 vector 列表时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61167210/