c++ - C++无法返回已添加到 vector 列表中的所有变量

标签 c++ arrays file vector

在当前正在编写的程序中,im达到了一个阶段,要求使用已通过文件读取的数据。其中一些数据包含数字元素,因此已进行了适当的转换,以便将它们存储在string类型的 vector 中。到此为止有效。

我有一个名为robotComplexity的函数,该函数根据文件中找到的其他值(已添加到 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.

如下面的代码所示,该文件已由冒号和句号处的定界符分隔,以首先分隔变量,然后分隔记录。如代码所示,变量已存储在其各自的持有人中。名为robotComplexity的函数包含一个for循环
for(std::size_t i=0; i< partsVector.size(); ++i) { 
 if(partsVector[i] == userChoice) {
   cout << userChoice << stoi(stringMaximum) << endl;
   }

我的代码的问题源于此for循环。该程序可以循环遍历文件,并重新标识各个值A,B,C,D,E的第一个变量partCode(转换为newChar)。因此,例如,当用户输入A时,它返回A,输入B时返回B等。现在,我的问题来自尝试打印出存储在 vector 中的其他变量。在cout << userChoice ... etc行中,它成功返回了Letter(示例A),但没有为转换为int的stringMaximum返回正确的Value。返回的值是0,在这里应该等于partCode A的值(在这种情况下)

我问是否有人可以创建/修复我的for循环,以便当调用cout <<“Variable”时,它可以根据partCode成功地将变量的值打印到控制台

例如,如果用户输入A作为零件代码,则输出应为


cout << userChoice << partName << stringMaximum  << stringMinimum << stringComplexity << endl;

输出
A 
Head 
1 
2 
15

包含功能的文件
struct Part {
char partCode;
std::string partName;
int maximum;
int minimum;
int complexity;
} myPart;

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

std::string outputFile = "output.txt";
std::string input;

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

std::string stringMaximum = std::to_string(myPart.maximum);
std::string stringMinimum = std::to_string(myPart.minimum);
std::string stringComplexity = std::to_string(myPart.complexity);

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 << myPart.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:
        myPart.partName = token;
        break;
      case 2: 
      myPart.maximum =stoi(token);
        break;
      case 3: 
      myPart.minimum = stoi(token);
        break;
        case 4:
         myPart.complexity = stoi(token);
        break;
      default:
        break;
      }
      counter++;//increasing counter
    }

partsVector.push_back(newChar);
partsVector.push_back(myPart.partName);
partsVector.push_back(stringMaximum);
partsVector.push_back(stringMinimum);
partsVector.push_back(stringComplexity);

   } 
}

double robotComplexity() { 

double complexity;
string userChoice;

cout << "Enter a part code A ,B ,C ,D or E" << endl;
cin >> userChoice;

for(std::size_t i=0; i< partsVector.size(); ++i) { 
 if(partsVector[i] == userChoice) {
   cout << userChoice << stoi(stringMaximum) << endl;
   }
} 
} 

谢谢您提供的任何帮助。如果需要进一步解释,请随时询问。 PS:我知道全局变量并不是最好的用法,但是一旦我的函数正常运行,我就会使用局部变量来清理代码。

最佳答案

这里有几个问题,但是您的主要问题是

std::string stringMaximum = std::to_string(myPart.maximum);
std::string stringMinimum = std::to_string(myPart.minimum);
std::string stringComplexity = std::to_string(myPart.complexity);

是全局变量,不是函数。在程序开始时,它们只会被评估一次。因此,您给我们提供的代码中的readFile已被破坏。我要做的第一件事是删除全局状态(即删除所有全局变量)并修复不再编译的代码。

你做
partsVector.push_back(stringMaximum);
partsVector.push_back(stringMinimum);

readFile中使用,而无需设置这两个变量,因此您始终在 vector 中推送相同的值。

下一个问题是,为什么要使用字符串 vector 而不是Part vector ?您已经将文件解析为Part对象,因此只需使用它们即可。此外,您希望通过查询Part.partCode的用户输入来访问这些部分,因此我们可以使用以partCode作为键的查找表(在本例中为std::unordered_map<char, Part>)。

所有这些看起来都像下面这样(readFile中的内部while循环也是一场噩梦,所以我也将其删除了):
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <iostream>
#include <unordered_map>

struct Part
{
    char partCode = 0;
    std::string partName;
    int maximum = 0;
    int minimum = 0;
    int complexity = 0;
};

std::stringstream partsList(
    R"(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.)");

std::string outputFile = "output.txt";
std::string input;

std::unordered_map<char, Part> readFile() //function to read Builders, Customers and Parts text file
{
    std::unordered_map<char, Part> parts;
    std::string line;

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

        std::getline(ss, token, ':');
        part.partCode = token[0];
        std::getline(ss, part.partName, ':');
        std::getline(ss, token, ':');
        part.maximum = std::stoi(token);
        std::getline(ss, token, ':');
        part.minimum = std::stoi(token);
        std::getline(ss, token, ':');
        part.complexity = std::stoi(token);

        parts.emplace(part.partCode, std::move(part));
    }

    return parts;
}

double robotComplexity(std::unordered_map<char, Part> const& parts)
{
    double complexity = 10;
    char partCode;

    std::cout << "Enter a part code A ,B ,C ,D or E" << std::endl;
    std::cin >> partCode;

    auto const& part = parts.at(partCode);
    std::cout << part.maximum;

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

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

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

int main()
{
    auto parts = readFile();
    writeFile();
    robotComplexity(parts);
    return 0;
}

关于c++ - C++无法返回已添加到 vector 列表中的所有变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61222986/

相关文章:

javascript - Javascript 中的对象数组 - 使用 "push"

c - time.h 和文件中的算法 C 代码

delphi - Delphi中简单的读/写记录.dat文件

linux - root创建的所有文件都被写保护

c++ - 如何使用 openCv 获取图像的相似度百分比?

c++ - 编译器如何处理仿函数中重载的函数调用运算符?

c++ - 尝试从文本文件读取到对象数组

python - 在 for 循环中使用 zip 分配给 numpy 数组

c++ - C++ 中的 DevIL 和 OpenGL

c++ - error : expected constructor, 析构函数,或者静态模板中 ‘'之前的类型转换