C++ to HTML conversion using namespaces - html

I'm trying to convert a .cpp file into a .html file.
Basically, at the end of the program, the html file when opened on chrome or whatever should look exactly like:
#include <iostream>
using namespace std;
int main()
{
int x = 4;
if (x < 3) x++;
cout << x << endl;
return 0;
}
I have three files, Source.cpp, fileToConvert.cpp, fileConverted.htm.
Source.cpp:
//This program will convert the selected file to another file for example .cpp to .html file.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
void conversion(ifstream& inStream, ofstream& outStream);
int main()
{
ifstream fin;
ofstream fout;
cout << "Begin editing files.\n";
fin.open("fileToConvert.cpp"); //input file (must in the same folder)
if (fin.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
fout.open("fileConverted.htm"); //output file (in the same folder)
if (fout.fail())
{
cout << "Output file opening failed.\n";
exit(1);
}
fout << "<PRE>" << endl; //<PRE> is the tag for HTML file that will convert all the spacing according to the input file
addPlusPlus(fin, fout);
fout << "</PRE>" << endl; //</PRE> is the tag for HTML file that will close the <PRE> tag
fin.close();
fout.close();
cout << "End of editing files.\n";
return 0;
}
void conversion(ifstream& inStream, ofstream& outStream)
{
char next;
inStream.get(next);
while (!inStream.eof())
{
if (next == '<')
outStream << "<";
else if (next == '>')
outStream << ">";
else
outStream << next;
inStream.get(next);
}
}
fileToConvert.cpp:
#include <iostream>
using namespace std;
int main()
{
int x = 4;
if (x < 3) x++;
cout << x << endl;
return 0;
}
And then the output should look like the first block of code above as said in HTML format.
The only way I can get this to work is to place the main() method in fileToConvert.cpp inside of a namespace, like so:
#include <iostream>
using namespace std;
namespace secondMain{
int main()
{
int x = 4;
if (x<3) x++;
cout << x << endl;
return 0;
}
}
Problem obviously being, this will display the namespace secondMain{...} code inside of the .htm file and, which I do not want.
If I don't use this second namespace, obviously the program will not work since there are two main() methods defined.
What am I missing in this program? The only workaround I found was adding that second namespace, and I do have to use namespaces in this project, but cannot display that namespace definition in the html page.
Any information is appreciated,
Thanks!!

If I understand correctly, the problem might be that you are compiling fileToConvert.cpp along with Source.cpp.

Related

i was supposed to print 5,4,3,2,1 as per the problem statement,but my c++ code is showing some error

ERROR i'm getting
F:\coding\code blocks\hello.cpp|14|error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream}' and 'void')|
my code
#include <iostream>
using namespace std;
void fun(int n)
{
if (n==0) return;
cout<<n<<endl;
fun(n-1);
}
int main()
{
cout << fun(5)<< endl;
}
Your code has two issues:
fun´s return type is void. Thus, this:
cout << fun(5) << endl;
can´t work as expected. To use the value of a void returning function invokes undefined behavior.
The printing in main of n in general is redundant as you already print it in fun().
Rather do it like:
#include <iostream>
using namespace std;
int fun(int n)
{
if (n == 0)
return;
cout << n << endl;
fun(n - 1);
}
int main(void)
{
fun(5);
return 0;
}
int the main function .
instead of writing
cout<<fun(5)<<endl;
write like this.
fun(5);
Your function fun returns void, also there is no need to use cout twice, use it either in main or in the function body fun. Try this code to get the output "as per your problem statement":
#include <iostream>
using namespace std;
int fun(int n)
{
if (n==0)
return 0;
((n-1)==0)?(cout<<n<<endl):(cout<<n<<",");
fun(n-1);
}
int main()
{
fun(5);
return 0;
}

Maximum value of thrust device_vector

i am trying to find the maximum value and it's location of a thrust::device_vecotr.
the mechanism below can save the position of the maximum value, however, i couldn't find the max_val.
i have cout statements to track the running order and where it crashes. it seems to be it crash on this line
int max_val = *iter;
it shows this result:
terminate called after throwing an instance of 'thrust::system::system_error'
what(): invalid argument
1234567
here is the code
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust/reduce.h>
#include <thrust/extrema.h>
#include <iostream>
#include <iomanip>
template <typename Vector>
void print_vector(const std::string& name, const Vector& v)
{
typedef typename Vector::value_type T;
std::cout << " " << std::setw(20) << name << " ";
thrust::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, " "));
std::cout << std::endl;
}
int main()
{
std::cout<<"1";
thrust::host_vector<int>h_vec(5);
h_vec.push_back(10);
h_vec.push_back(11);
h_vec.push_back(12);
h_vec.push_back(13);
h_vec.push_back(14);
std::cout<<"2";
thrust::device_vector<int>d_vec(5);
std::cout<<"3";
thrust::copy_n(h_vec.begin(),5,d_vec.begin());
std::cout<<"4";
// print_vector("D_Vec",d_vec);
std::cout<<"5";
thrust::device_vector<int>::iterator iter=thrust::max(d_vec.begin(),d_vec.end());
std::cout<<"6";
unsigned int position = iter - d_vec.begin();
std::cout<<"7";
int max_val = *iter;
std::cout<<"8";
std::cout<<"Max Val= "<<14<<" #"<<position<< std::endl;
return 0;
}
Help .. please. also, if there is a better way to extract the maximum value and its position in device_vector using THRUST library it is more than appreciated.
You're not using vectors correctly. push_back() adds an element onto the end of an existing vector. It's clear that you want to replace existing elements.
Also, the thrust algorithm you want is thrust::max_element, not thrust::max
Here's a fully worked code with those issues fixed:
$ cat t1229.cu
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust/reduce.h>
#include <thrust/extrema.h>
#include <iostream>
#include <iomanip>
template <typename Vector>
void print_vector(const std::string& name, const Vector& v)
{
typedef typename Vector::value_type T;
std::cout << " " << std::setw(20) << name << " ";
thrust::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, " "));
std::cout << std::endl;
}
int main()
{
std::cout<<"1" <<std::endl;
thrust::host_vector<int>h_vec(5);
h_vec[0] = 10;
h_vec[1] = 11;
h_vec[2] = 12;
h_vec[3] = 13;
h_vec[4] = 14;
std::cout<<"2" << std::endl;
thrust::device_vector<int>d_vec(5);
std::cout<<"3" << std::endl;
thrust::copy_n(h_vec.begin(),5,d_vec.begin());
std::cout<<"4" << std::endl;
// print_vector("D_Vec",d_vec);
std::cout<<"5" << std::endl;
thrust::device_vector<int>::iterator iter=thrust::max_element(d_vec.begin(),d_vec.end());
std::cout<<"6" << std::endl;
unsigned int position = iter - d_vec.begin();
std::cout<<"7" << std::endl;
int max_val = d_vec[position];
std::cout<<"8" << std::endl;
std::cout<<"Max Val= "<<max_val<<" #"<<position<< std::endl;
return 0;
}
$ nvcc -o t1229 t1229.cu
$ ./t1229
1
2
3
4
5
6
7
8
Max Val= 14 #4
$

Qt 5.5 QSqlDatabase::open() always returns true?

The following code works with MySql:
#include <QCoreApplication>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlRecord>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("192.168.139.128");
db.setDatabaseName("qsql");
db.setUserName("user");
db.setPassword("pass");
if (!db.open()) {
qDebug() << "Error = " << db.lastError();
} else {
qDebug() << "Openned!" ;
}
QSqlQuery query("SELECT id, name FROM persons");
QSqlRecord record = query.record();
while (query.next()) {
QString id = query.value(record.indexOf("id")).toString();
QString name = query.value(record.indexOf("name")).toString();
qDebug() << query.at() << ":" << id << "," << name;
}
return a.exec();
}
The problem is db.open() always returns true, no matter how wrong the connection parameters are. I am aware that this might be a known bug in Qt 5.5 (which I am using), I wonder if there is a work around or a solution for it ?
Yes, it is QTBUG-47784 and related QTBUG-47452 bugs. Both was fixed in Qt 5.5.1. Unfortunately there is no solution without changing the source code of Qt or upgrading to 5.5.1.

getline() no instance of overloaded function c++

trying to code a cin.getline function and it throws me an error saying theres no instance of overloaded function? I'm sure im using the parameters correctly, not sure how to fix..
#include <iostream>
#include <string>
#include <ctime>
#include <string>
#include <cstring>
#include "NonPerishable.h"
#include "Item.h"
using namespace std;
namespace sict {
class NonPerishable : public Item{
fstream& save(std::fstream& file) const {
file << "N," << sku() << "," << name() << "," << cost() << "," << taxed() << "," << quantity() << endl;
return file;
}
fstream& load(std::fstream& file) {
char sku1[15];
cin.ignore(2);
cin.getline(sku1, 15, ',');
//edit:
double integer;
cin.getline(integer, 15, ',');
}
ostream& write(std::ostream& os, bool linear) const {
}
istream& read(std::istream& is) {
}
fstream& save(std::fstream& file){
}
};
}
any help would be appreciated !
Edit: i fixed it for retrieving chars, but doubles/int wont work?
The third argument is supposed to be a char, so try single quotes:
cin.getline(sku1, 15, ',');

Getting ifstream from istream and calling functions via overloaded >> operator

I've created an object, PDBParser, to extract information from a PDB file. Now I am trying to overload the >> and << operators so that I can use them from the main as so:
inFile >> MyPDBParser;
outfile << MyPDBParser;
I've got the << operator all set, but I can't seem to get the >> operator to work properly.
Here is the .h file for the PDBParser class to give you a better idea of what's going on:
#include <iostream>
#include <cstdlib>
#include "FloatArray.h"
#include "IntArray.h"
#include "Atom.h"
#include "AtomArray.h"
using namespace std;
class PDBParser
{
friend ostream& operator<<(ostream& _ostream, PDBParser &rhs);
friend istream& operator>>(istream& _istream, PDBParser &rhs);
public:
PDBParser();
PDBParser(string atom1, string atom2, int separation);
PDBParser(const PDBParser& orig);
virtual ~PDBParser();
void grabAtoms(ifstream &infile);
void findAtoms();
void setAtom1(string rhs);
void setAtom2(string rhs);
void setSeparation(int rhs);
string getAtom1();
string getAtom2();
int getSeparation();
private:
string atom1s;
string atom2s;
int separation;
AtomArray *atoms1;
AtomArray *atoms2;
AtomArray *matches1;
AtomArray *matches2;
FloatArray *x1;
FloatArray *y1;
FloatArray *z1;
FloatArray *x2;
FloatArray *y2;
FloatArray *z2;
IntArray *allsequence;
ifstream backupinfile;
void trim(string &rhs);
void incrementArrays(int newElements);
};
Essentially what I need the >> operator to do is get the infile from the istream and then call the grabAtoms(infile) and findAtoms() functions for this instance of the PDBParser object.
Here's what I have now, which doesn't work. Please forgive the commented lines, as they were things I was attempting to make work. I tried adding the backupinfile object to the PDBParser class just to make things work, so normally it didn't have this and doesn't use it.
istream & operator>>(istream & _istream, PDBParser &rhs)
{
// ifstream in;
// _istream >> rhs.grabAtoms(in) >> rhs.findAtoms();
_istream >> rhs.backupinfile;
rhs.grabAtoms(rhs.backupinfile);
rhs.findAtoms();
return _istream;
}
I've established that the issue here is that my function needs to receive an ifstream object and I can't figure out how to get that from the istream object.
Here's my working << overload just for the heck of it:
ostream & operator<<(ostream & _ostream, PDBParser &rhs)
{
for(int i=0; i < rhs.x1->getSize(); i++)
{
_ostream.precision(3);
_ostream << fixed;
_ostream << setprecision (3) << rhs.x1->get(i) << " ";
_ostream << setprecision (3) << rhs.y1->get(i) << " ";
_ostream << setprecision (3) << rhs.z1->get(i) << " ";
_ostream << setprecision (3) << rhs.x2->get(i) << " ";
_ostream << setprecision (3) << rhs.y2->get(i) << " ";
_ostream << setprecision (3) << rhs.z2->get(i) << endl;
}
return _ostream;
}
Thanks