gpt4 book ai didi

c++ - 将变量添加到 vector 列表的C++问题

转载 作者:行者123 更新时间:2023-12-02 09:54:45 25 4
gpt4 key购买 nike

我创建了一个程序,该程序可以读取不同的文本文件并将每个文件存储在其自己的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 列表的C++问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61167210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com