C++ Exercise

Bom dia,

Tenho que criar um programa em C++ que leia dois ficheiro txt.
Um deles denominado Encom.txt outro local.txt
No ficheiro Encom.txt - Tenho: (Loja; IDEncomenda; Produto; Quantidade) em cada linha
No ficheiro Local.txt - Tenho: (Produto; Local) em cada linha

O objetivo final será identicar os produtos pedidos em cada encomenda (no ficheiro encom.txt) e depois identifcar o local dos produtos da encomenda (no ficheiro local.txt).

Por forma a finalmente calcular a distância percorrida, pelo picker, em cada encomenda, que será sempre dada pelo: [máximo do nº local do produto i na encomenda j] - [minimo do nº do local do produto i na encomenda j].
(O nº local é dado pelo ficheiro Local.txt)

No fim, somam-se todas as distâncias.
Alguém me consegue ajudar?

Muito Obrigada!
Please note that this is not a homework site. We won't do your homework for you. The purpose of homework is that you learn by doing. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.

We didn't see your attempts to solve this problem yourself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again.


Sadly, I can't go back to basics of your language, for I have never had them in the first place. Please use english.
Im not asking for you to do my homework.

And you did not ask me any kind of question at a first place.

I have this code:

#include <iostream>
#include <fstream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <map>

using namespace std;

struct encomenda {
int loja;
int id;
int produto;
int quant;
};

struct local {
int produto;
int dist;
};

struct dist {
int max;
int min;
};

vector<string> readFile(string file) {
vector<string> array;
string line;

ifstream myFile (file.c_str());

if (myFile.is_open()) {
while ( getline(myFile, line) ) {
array.push_back(line);
}
myFile.close();
} else {
cout << "Unable to open file: " << file;
}

return array;
}

vector<int> splitLine(string str, char delimiter) {
replace(str.begin(), str.end(), delimiter, ' ');

vector<int> array;
stringstream ss(str);
int temp;

while (ss >> temp) {
array.push_back(temp);
}

return array;
}

encomenda createEncomenda(int loja, int id, int produto, int quant) {
encomenda temp;

temp.loja = loja;
temp.id = id;
temp.produto = produto;
temp.quant = quant;

return temp;
}

local createLocal(int produto, int dist) {
local temp;

temp.produto = produto;
temp.dist = dist;

return temp;
}

vector<encomenda> readEncomendas() {
vector<string> encomenFile = readFile("encomen.txt");
vector<encomenda> encomendas;

for (int i=1; i < encomenFile.size(); i++){
vector<int> line = splitLine(encomenFile[i], ';');

encomenda newEncomenda = createEncomenda(line[0], line[1], line[2], line[3]);
encomendas.push_back(newEncomenda);
}

return encomendas;
}

vector<local> readLocais() {
vector<string> localizFile = readFile("localiz.txt");
vector<local> locais;

for (int i=1; i < localizFile.size(); i++){
vector<int> line = splitLine(localizFile[i], ';');

local newLocal = createLocal(line[0], line[1]);
locais.push_back(newLocal);
}

return locais;
}

int main () {
vector<encomenda> encomendas = readEncomendas();
vector<local> locais = readLocais();
map<int, dist> mapEncomendasDist;

double total =0;

for (int i=0; i < encomendas.size(); i++) {
for (int j=0; j < locais.size(); j++) {
if (encomendas[i].produto == locais[j].produto) {
if (locais[j].dist > mapEncomendasDist[encomendas[i].id].max) {
mapEncomendasDist[encomendas[i].id].max = locais[j].dist;
}

if (locais[j].dist < mapEncomendasDist[encomendas[i].id].min || mapEncomendasDist[encomendas[i].id].min == 0) {
mapEncomendasDist[encomendas[i].id].min = locais[j].dist;
}

break;
}
}
}

for (int i=1; i <= mapEncomendasDist.size(); i++) {
cout << "EncomendaID " << i << ":" << endl;
cout << " " << "MAX: " << mapEncomendasDist[i].max << endl;
cout << " " << "MIN: " << mapEncomendasDist[i].min << endl;
cout << " " << "DIFF: " << mapEncomendasDist[i].max - mapEncomendasDist[i].min << endl;
}
for (int i=1; i <= mapEncomendasDist.size(); i++) {
total = total+ mapEncomendasDist[i].max - mapEncomendasDist[i].min << endl;
cout << total<< endl;
}

return 0;
}

When I run the code, it gives me an error. It says " Invalid operand of type double" in the total.

Thank you
The correct term is compile the code, not run the code.

Yes, error and suggestion:
138:71: error: invalid operands of types 'double' and '<unresolved overloaded function type>' to binary 'operator<<'
138:41: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]

They do refer to:
total = total+ mapEncomendasDist[i].max - mapEncomendasDist[i].min << endl;
The error says: invalid operands to binary 'operator<<'
Where is that?
total = total+ mapEncomendasDist[i].max - mapEncomendasDist[i].min << endl;

Lets try something simpler:
total = total << endl;
Yes, same error.
The total (and total+ mapEncomendasDist[i].max - mapEncomendasDist[i].min) are double
The endl (probably std::endl) has a type too.
(For some reason the compiler is not sure what and says <unresolved overloaded function type>.)

Perhaps you should not have << on that line?
The problem was the <<endl;

Thanks
Topic archived. No new replies allowed.