PDA

View Full Version : Can't read map data



bikonja
15th July 2015, 16:04
Hello, i have a problem with reading my map data or possibly even writing it in.

the app i'm making is a basic currency converter.

I download the exchange rates from a website, save it into txt file, which i then parse to save the values into a map and use my calculator class to do the conversion.

the downloader class works flawless (i got it of offical QT forums) so i wont post it since the problem aint there.

the code: main.cpp


int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QMap<QString,double> currency_map;

downloader d;
d.Do_download();

parser p;
p.read_line(currency_map);

p.print_map(currency_map);// this line works, and it prints out the map

MainWindow w(currency_map);
w.show();

return a.exec();
};

parser.cpp
im pretty sure it works well because the print_map function does its job.


void parser::process_line(QString line, QMap<QString, double> &my_map)
{

QStringList temporary_list;

for(int i = 0; i< currency_list.size();i++)
{
if(line.contains(currency_list.at(i),Qt::CaseInsen sitive))
{

temporary_list=line.split(" ",QString::SkipEmptyParts);

temporary_list.replaceInStrings(",",".");
my_map.insert(currency_list.at(i),temporary_list[6].toDouble());
}
}

}

int parser::read_line(QMap<QString, double> &my_map)
{

QFile file("C:/Qt/test/downloaded.txt");

if(!file.exists())
{
QMessageBox msgBox;
msgBox.setText("There is no such file");
msgBox.exec();
return 1;
}
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox msgBox;
msgBox.setText("Error while opening file");
msgBox.exec();
return 1;
}

QTextStream in_stream(&file);
QString line=in_stream.readLine();

while (!line.isNull())
{
process_line(line, my_map);
line = in_stream.readLine();
}
return 0;
}

void parser::print_map(QMap<QString, double> &my_map)
{
QMapIterator<QString, double> i(my_map);
while(i.hasNext())
{
i.next();
qDebug()<< i.key() << ": " << i.value();
}
}

now i have a calculator class :

.h


class Calculator
{
public:
explicit Calculator(QMap<QString,double> &currency_map);
void multiply();
void getValues(QString strFrom, QString strTo);
double getTotal();
private:
double total, firstCurr, secondCurr;
QMap<QString,double> &map;

};

.cpp


#include "calculator.h"

Calculator::Calculator(QMap<QString,double> &currency_map):map(currency_map)
{
total = 0;
firstCurr = 0;
secondCurr= 0;
}

void Calculator::getValues(QString strFrom, QString strTo)
{
QMap<QString, double>::iterator i;
for(i=map.begin();i!=map.end();i++);
{

if(!i.key().compare(strFrom))
firstCurr=i.value();
if(!i.key().compare(strFrom))
secondCurr = i.value();
}
//firstCurr = 2;
//secondCurr = 3;
}

void Calculator::multiply()
{
total = firstCurr * secondCurr;
}

double Calculator::getTotal()
{
return total;
}

then i create a Calculator object in my mainWindow class
.h


class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QMap<QString,double> &currency_map, QWidget *parent = 0);

~MainWindow();


private slots:
void on_convert_button_clicked();

private:
Ui::MainWindow *ui;
Calculator calc;

};

.cpp


MainWindow::MainWindow(QMap<QString, double> &currency_map, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),calc(currency_map)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_convert_button_clicked()
{
calc.getValues(ui->from_Combox->currentText(),ui->to_Combox->currentText());
calc.multiply();
ui->lcdNumber->display(calc.getTotal());
}


but i cant seem to get any values from the map.
the wierd thing also is when i debugg(i use visual studio, have some trouble setting up a debbuger in QT Creator) its always shows the map as empty, which i cant grasp since the print function works.

any help would be appreciated. thx

anda_skoa
15th July 2015, 16:37
for(i=map.begin();i!=map.end();i++);
{
/// ...
}


You have a for loop without body, followed by a block that operates on an invalid iterator.

Cheers,
_

yeye_olive
15th July 2015, 16:48
On top of that, getValues() does not use its strTo parameter. I suppose that the compiler warns about it, doesn't it? By the way, the whole point of QMap is that you do not have to traverse the whole collection to find the value mapped to a key.

bikonja
15th July 2015, 17:33
You have a for loop without body, followed by a block that operates on an invalid iterator.

Cheers,
_

check again


On top of that, getValues() does not use its strTo parameter. I suppose that the compiler warns about it, doesn't it? By the way, the whole point of QMap is that you do not have to traverse the whole collection to find the value mapped to a key.

yes, that was a brain lapse. it was the wrong parameter.

i appreciate the hints, and i will use them but i still don't know why i can't read my map values.

yeye_olive
15th July 2015, 17:39
check again
anda_skoa is right. Look at the last character of the line with 'for'.

bikonja
15th July 2015, 17:55
Okey, you got me there. I was looking at the wrong place. (didnt solve my problem tho)
But still, i dont know why my debugger says the map is empty, and if it actually is empty, where did my print function get the data it writes out.
an example would be
debugger: (error), 0
print_map_"AUD" : 5.13678 and 11 more lines of different examples

d_stranz
15th July 2015, 23:04
yeye_olive is right - you don't seem to know how maps are used. The whole point of a map is that you can write something like:



firstCurr = currencyMap[ strFrom ];


You don't need to iterate over the whole map and examine everything in it. If you want to make sure the key is actually in there, you can check QMap::contains() first.

Other than the extra ";" that you should have removed by now (in fact, the whole for() loop needs to go), I don't see anything obviously wring with the code. You do know that the default for QString::compare() is Qt::CaseSensitive, right, which means that the currency names you are getting from your line edits better match the values in your map exactly?

bikonja
16th July 2015, 12:18
Thx alot for the tips. I dont really know why i was iterating the map, i'm aware of its functionality :p

And yes all my strings are in capital letters, so that wont be a problem.

Is it possible that visual studio can't read my QT objects and that's why the debugger shows that my map is empty?

d_stranz
16th July 2015, 15:46
Is it possible that visual studio can't read my QT objects and that's why the debugger shows that my map is empty?

The VS debugger works just fine looking into Qt objects. You might have to click through a couple of levels of indirection, since most Qt classes are implemented using a private pointer to the true contents (Envelope - Letter paradigm).

Have you simply put a qDebug() statement inside the constructors for your MainWindow and Calculator classes to see what the map looks like there?


qDebug() << currency_map;