Hi, i'm making an currency converter app.
I go online, download a html and save it into a txt file, which i parse afterwards for the exchange rates.
.h
#ifndef DOWNLOADER_H
#define DOWNLOADER_H
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include <QFile>
#include <QDebug>
#include <QMessageBox>
#include <string>
#include <Windows.h>
#include <qstring.h>
{
Q_OBJECT
public:
explicit downloader
(QObject *parent
= 0);
void Do_download();
public slots:
void replyFinished (QNetworkReply *reply);
private:
QNetworkAccessManager *manager;
};
#ifndef DOWNLOADER_H
#define DOWNLOADER_H
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include <QFile>
#include <QDebug>
#include <QMessageBox>
#include <string>
#include <Windows.h>
#include <qstring.h>
class downloader : public QObject
{
Q_OBJECT
public:
explicit downloader(QObject *parent = 0);
void Do_download();
public slots:
void replyFinished (QNetworkReply *reply);
private:
QNetworkAccessManager *manager;
};
To copy to clipboard, switch view to plain text mode
.cpp
#include "downloader.h"
#include "strings.h"
#include <string>
#include <Windows.h>
downloader
::downloader(QObject *parent
) :{
}
void downloader::Do_download()
{
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),
this,SLOT(replyFinished(QNetworkReply*)));
manager
->get
(QNetworkRequest
(QUrl("http://www.hnb.hr/tecajn/hvazeca.htm")));
}
void downloader::replyFinished(QNetworkReply *reply)
{
if(reply->error())
{
msgBox.setText(strings::msgBoxDownReplyError);
msgBox.exec();
}
else
{
QFile *file = new QFile("C:/Qt/test/downloaded.txt");
{
file->write(data);
file->flush();
file->close();
}
delete file;
}
reply->deleteLater();
}
#include "downloader.h"
#include "strings.h"
#include <string>
#include <Windows.h>
downloader::downloader(QObject *parent) :
QObject(parent)
{
}
void downloader::Do_download()
{
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),
this,SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl("http://www.hnb.hr/tecajn/hvazeca.htm")));
}
void downloader::replyFinished(QNetworkReply *reply)
{
if(reply->error())
{
QMessageBox msgBox;
msgBox.setText(strings::msgBoxDownReplyError);
msgBox.exec();
}
else
{
QFile *file = new QFile("C:/Qt/test/downloaded.txt");
QByteArray data = reply->readAll();
if(file->open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text))
{
file->write(data);
file->flush();
file->close();
}
delete file;
}
reply->deleteLater();
}
To copy to clipboard, switch view to plain text mode
parser.h
#ifndef PARSER_H
#define PARSER_H
#include <QMessageBox>
#include <QStringList>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <string>
#include <Windows.h>
class parser
{
public:
explicit parser();
std::list<std::string> curr_list;
void process_line(std::string line, std::map<std::string,double> &my_map, std::string curr_string);
int read_line(std::map<std::string,double> &my_map);
void init_list();
std::string getExePathParser();
private:
};
#endif // PARSER_H
#ifndef PARSER_H
#define PARSER_H
#include <QMessageBox>
#include <QStringList>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <string>
#include <Windows.h>
class parser
{
public:
explicit parser();
QStringList currency_list;
std::list<std::string> curr_list;
void process_line(std::string line, std::map<std::string,double> &my_map, std::string curr_string);
int read_line(std::map<std::string,double> &my_map);
void init_list();
std::string getExePathParser();
private:
};
#endif // PARSER_H
To copy to clipboard, switch view to plain text mode
parser.cpp
#include "parser.h"
#include "calculator.h"
#include "strings.h"
#include <string>
parser::parser()
{
init_list();
}
void parser::process_line(std::string line, std::map<std::string,double> &my_map, std::string curr_string)
{
double curr;
std::string temp;
temp = line.substr(52,8);
std::replace(temp.begin(),temp.end(),',','.');
curr = std::stod(temp,NULL);
my_map.insert(std::make_pair(curr_string,curr));
}
int parser::read_line(std::map<std::string,double> &my_map)
{
std::list<std::string>::iterator iter;
std::string linija;
std::ifstream file_("C:/Qt/test/downloaded.txt");
if(file_.fail())
{
msgBox.setText(strings::msgBoxOpeningError);
msgBox.exec();
}
else if(file_.is_open())
{
while(std::getline(file_,linija))
{
for(iter=curr_list.begin(); iter != curr_list.end(); ++iter)
{
if(linija.find(*iter) != std::string::npos)
process_line(linija, my_map, *iter);
}
}
file_.close();
}
return 0;
}
void parser::init_list()
{
currency_list << "HRK" << "CAD" << "CZK" << "DKK" << "HUF"
<< "JPY" << "NOK" << "SEK" << "CHF" << "GBP"
<< "USD" << "EUR" << "PLN" << "AUD";
curr_list.push_back("HRK");
curr_list.push_back("CAD");
curr_list.push_back("CZK");
curr_list.push_back("DKK");
curr_list.push_back("HUF");
curr_list.push_back("JPY");
curr_list.push_back("NOK");
curr_list.push_back("SEK");
curr_list.push_back("CHF");
curr_list.push_back("GBP");
curr_list.push_back("USD");
curr_list.push_back("EUR");
curr_list.push_back("PLN");
curr_list.push_back("AUD");
}
#include "parser.h"
#include "calculator.h"
#include "strings.h"
#include <string>
parser::parser()
{
init_list();
}
void parser::process_line(std::string line, std::map<std::string,double> &my_map, std::string curr_string)
{
double curr;
std::string temp;
temp = line.substr(52,8);
std::replace(temp.begin(),temp.end(),',','.');
curr = std::stod(temp,NULL);
my_map.insert(std::make_pair(curr_string,curr));
}
int parser::read_line(std::map<std::string,double> &my_map)
{
std::list<std::string>::iterator iter;
std::string linija;
std::ifstream file_("C:/Qt/test/downloaded.txt");
if(file_.fail())
{
QMessageBox msgBox;
msgBox.setText(strings::msgBoxOpeningError);
msgBox.exec();
}
else if(file_.is_open())
{
while(std::getline(file_,linija))
{
for(iter=curr_list.begin(); iter != curr_list.end(); ++iter)
{
if(linija.find(*iter) != std::string::npos)
process_line(linija, my_map, *iter);
}
}
file_.close();
}
return 0;
}
void parser::init_list()
{
currency_list << "HRK" << "CAD" << "CZK" << "DKK" << "HUF"
<< "JPY" << "NOK" << "SEK" << "CHF" << "GBP"
<< "USD" << "EUR" << "PLN" << "AUD";
curr_list.push_back("HRK");
curr_list.push_back("CAD");
curr_list.push_back("CZK");
curr_list.push_back("DKK");
curr_list.push_back("HUF");
curr_list.push_back("JPY");
curr_list.push_back("NOK");
curr_list.push_back("SEK");
curr_list.push_back("CHF");
curr_list.push_back("GBP");
curr_list.push_back("USD");
curr_list.push_back("EUR");
curr_list.push_back("PLN");
curr_list.push_back("AUD");
}
To copy to clipboard, switch view to plain text mode
and my main
int main(int argc, char *argv[])
{
std::map<std::string,double> currency_map;
downloader d;
d.Do_download();
parser p;
p.read_line(currency_map);
MainWindow w(currency_map);
w.show();
return a.exec();
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
std::map<std::string,double> currency_map;
downloader d;
d.Do_download();
parser p;
p.read_line(currency_map);
MainWindow w(currency_map);
w.show();
return a.exec();
};
To copy to clipboard, switch view to plain text mode
It was working good, but then i changed the path for the .txt file i create.
And the first time i run the app, i get a msgBoxError from my parser class
if(file_.fail())
{
msgBox.setText(strings::msgBoxOpeningError);
msgBox.exec();
}
if(file_.fail())
{
QMessageBox msgBox;
msgBox.setText(strings::msgBoxOpeningError);
msgBox.exec();
}
To copy to clipboard, switch view to plain text mode
this part of code pops up.
then i shut it down. run the app again, and it's all working well.
so everytime i change the file path, the first running of the app wont work.
can someone help me out, and spot what's wrong here?
(i have to use basic c++ in some parts of the code, don't ask)
Bookmarks