1
I am not able to read a txt file correctly and transfer to a map in c++.
The code I developed works only if you don’t have spaces " " in txt, if you loop and it doesn’t work. Here’s the code:
void dados::pesquisarProdutos(){
arq.open("produtos.txt",ios::in);
if (arq.fail()){
perror("erro ao pesquisar: ");
sleep(1);
return;
}
string nome;
float preco;
int qnt;
while (!arq.eof()){
arq >> nome >> preco >> qnt;
map1.insert(make_pair(nome,loja(nome,preco,qnt)));
}
cout << "Qual produto procura: ";
getline(cin,nome);
it = map1.find(nome);
cout << it->first << "preco: "<< it->second.getPreco() << "quantidade: "<<
it->second.getEstoque();
arq.close();}
If the product name is, for example, rice, it saves in the map and works normal, now if it is white rice, error. Apparently the problem is reading space, but I don’t know how to solve.
Thanks for your help
Why is the C tag? C and C++ a completely different program.
– user5299