【发布时间】:2020-04-12 05:21:53
【问题描述】:
我创建了一个程序,它可以读取不同的文本文件并将每个文件存储在自己的向量列表中。但是,程序的向量列表比例不起作用。下面提供的是我当前正在处理的文本文件以及程序本身
文本文件
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。我推断这是我如何将变量添加到向量的问题,但我不确定为什么。任何帮助都会很棒谢谢。
【问题讨论】:
-
请不要使用全局变量。在需要它们的函数中声明变量,并使用参数和返回值在函数之间传递信息。随着程序变大,快速使用全局变量会使代码变得难以管理。如果我是你的教授,你会因此失去很多分数。不是完全失败,而是接近。一次处理一个全局变量,然后将它们移动到实际需要的位置。
-
@john 道歉。我重写了问题以使其更易于阅读,删除了与问题无关的代码。希望有帮助
-
我希望你能修复你的代码而不是编辑你的帖子。说真的,如果你在编码风格上不失分,那么你的教授是一个非常宽容的人。除了风格之外,您的代码确实表明您可以管理棘手的需求,它比我们在这里看到的一些代码要好得多。