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[])
{
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();
};
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();
};
To copy to clipboard, switch view to plain text mode
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
) {
for(int i = 0; i< currency_list.size();i++)
{
if(line.contains(currency_list.at(i),Qt::CaseInsensitive))
{
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())
{
msgBox.setText("There is no such file");
msgBox.exec();
return 1;
}
{
msgBox.setText("Error while opening file");
msgBox.exec();
return 1;
}
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();
}
}
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::CaseInsensitive))
{
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();
}
}
To copy to clipboard, switch view to plain text mode
now i have a calculator class :
.h
class Calculator
{
public:
explicit Calculator(QMap<QString,double> ¤cy_map);
void multiply();
double getTotal();
private:
double total, firstCurr, secondCurr;
QMap<QString,double> ↦
};
class Calculator
{
public:
explicit Calculator(QMap<QString,double> ¤cy_map);
void multiply();
void getValues(QString strFrom, QString strTo);
double getTotal();
private:
double total, firstCurr, secondCurr;
QMap<QString,double> ↦
};
To copy to clipboard, switch view to plain text mode
.cpp
#include "calculator.h"
Calculator::Calculator(QMap<QString,double> ¤cy_map):map(currency_map)
{
total = 0;
firstCurr = 0;
secondCurr= 0;
}
{
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;
}
#include "calculator.h"
Calculator::Calculator(QMap<QString,double> ¤cy_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;
}
To copy to clipboard, switch view to plain text mode
then i create a Calculator object in my mainWindow class
.h
{
Q_OBJECT
public:
explicit MainWindow
(QMap<QString,double>
¤cy_map,
QWidget *parent
= 0);
~MainWindow();
private slots:
void on_convert_button_clicked();
private:
Ui::MainWindow *ui;
Calculator calc;
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QMap<QString,double> ¤cy_map, QWidget *parent = 0);
~MainWindow();
private slots:
void on_convert_button_clicked();
private:
Ui::MainWindow *ui;
Calculator calc;
};
To copy to clipboard, switch view to plain text mode
.cpp
MainWindow
::MainWindow(QMap<QString, double>
¤cy_map,
QWidget *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());
}
MainWindow::MainWindow(QMap<QString, double> ¤cy_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());
}
To copy to clipboard, switch view to plain text mode
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
Bookmarks