PDA

View Full Version : why the command endl; dont work in my code ?



joovoo
26th June 2017, 18:13
Good morninng every body! I dont understand why the command "endl;" in the code below dont make the cursor of the file "out.txt" go to the next line when writing in it. My APPICLATION consist of ask names of students and their notes to write all of that in the file "out.txt by clicking on the button "genere notes ". The command "endl;" is implemeted in the slot "genereNotes" as in the code below:
This is the header:

#ifndef TEST2_H
#define TEST2_H
#include <QObject>
#include <QApplication>
#include <QWidget>
#include <QBoxLayout>
#include <QGridLayout>
#include <QDoubleSpinBox>
#include <QLabel>
#include <QInputDialog>
#include <QString>
#include <QIcon>
#include <QTextEdit>
#include <QPushButton>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QFormLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>

class MaFenetre : public QWidget
{
Q_OBJECT
public:
MaFenetre();
~MaFenetre();
public slots:
void genereNotes();
private:
QString liste1;
QString liste2;
QString liste3;
QString liste4;
QString liste5;
QString liste6;
QDoubleSpinBox* note1;
QDoubleSpinBox* note2;
QDoubleSpinBox* note3;
QDoubleSpinBox* note4;
QDoubleSpinBox* note5;
QDoubleSpinBox* note6;
QFormLayout* layout1;
QFormLayout* layout2;
QFormLayout* layout3;
QFormLayout* layout4;
QFormLayout* layout5;
QFormLayout* layout6;


};


#endif // TEST2_H


This the cpp where the slot is implemented:

#include "test2.h"
MaFenetre::MaFenetre():QWidget(),liste1("nom"),liste2("nom"),liste3("nom"),liste4("nom"),liste5("nom"),liste6("nom")
{
note1=new QDoubleSpinBox ;
note2=new QDoubleSpinBox ;
note3=new QDoubleSpinBox ;
note4=new QDoubleSpinBox ;
note5=new QDoubleSpinBox ;
note6=new QDoubleSpinBox ;
layout1=new QFormLayout;
layout2=new QFormLayout;
layout3=new QFormLayout;
layout4=new QFormLayout;
layout5=new QFormLayout;
layout6=new QFormLayout;
QVBoxLayout *vlayout=new QVBoxLayout;
QPushButton *button=new QPushButton("generer note",this);
QString eleve;
bool ok(false);
do{eleve=QInputDialog::getText(this,
"Question","Saisissez le nom de l'eleve",QLineEdit::Normal,QString(),&ok);liste1=eleve;}
while(!ok||eleve.isEmpty());
do{eleve=QInputDialog::getText(this,
"Question","Saisissez le nom de l'eleve",QLineEdit::Normal,QString(),&ok);liste2=eleve;}
while(!ok||eleve.isEmpty());
do{eleve=QInputDialog::getText(this,
"Question","Saisissez le nom de l'eleve",QLineEdit::Normal,QString(),&ok);liste3=eleve;}
while(!ok||eleve.isEmpty());
do{eleve=QInputDialog::getText(this,
"Question","Saisissez le nom de l'eleve",QLineEdit::Normal,QString(),&ok);liste4=eleve;}
while(!ok||eleve.isEmpty());
do{eleve=QInputDialog::getText(this,
"Question","Saisissez le nom de l'eleve",QLineEdit::Normal,QString(),&ok);liste5=eleve;}
while(!ok||eleve.isEmpty());
do{eleve=QInputDialog::getText(this,
"Question","Saisissez le nom de l'eleve",QLineEdit::Normal,QString(),&ok);liste6=eleve;}
while(!ok||eleve.isEmpty());


layout1->addRow(liste1,note1);
layout2->addRow(liste2,note2);
layout3->addRow(liste3,note3);
layout4->addRow(liste4,note4);
layout5->addRow(liste5,note5);
layout6->addRow(liste6,note6);

vlayout->addLayout(layout1);
vlayout->addLayout(layout2);
vlayout->addLayout(layout3);
vlayout->addLayout(layout4);
vlayout->addLayout(layout5);
vlayout->addLayout(layout6);
vlayout->addWidget(button);
this->setLayout(vlayout);
QObject::connect(button,SIGNAL(clicked()),this,SLO T(genereNotes()));

}
MaFenetre::~MaFenetre()
{

}

void MaFenetre::genereNotes()
{ int i(0);
QFile file("out.txt");
if(file.exists()&&file.open(QIODevice::WriteOnly)){QMessageBox::info rmation(this,"A propos du bulletin","Ouverture réussie de "+file.fileName());
QTextStream out(&file);
out<<liste1;for(i=0;i<10-liste1.size();i++)
{
out<<" ";
}
out<<note1->value()<<"\n";out<<liste2;for(i=0;i<10-liste2.size();i++)
{
out<<" ";
}
out<<note2->value()<< endl;out<<liste3;for(i=0;i<10-liste3.size();i++)
{
out<<" ";

}out<<note3->value()<< endl;out<<liste4;for(i=0;i<10-liste4.size();i++)
{
out<<" ";
}
out<<note4->value()<< endl;out<<liste5;for(i=0;i<10-liste5.size();i++)
{
out<<" ";
}
out<<note5->value()<< endl;out<<liste6;for(i=0;i<10-liste6.size();i++)
{
out<<" ";
}
out<<note6->value()<< endl;
}

}


And there is the main.cpp

#include "test2.h"


int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MaFenetre fenetre;
fenetre.show();
return app.exec();
}


Please help me!!!!!!!!!!!!

Syrian Lucianos
29th June 2017, 11:36
you should include the iostream library:


#include <iostream>

then you can use endl,but beware to use the standard namespace,just like this:


std::endl;

or you can use "\n" instead of endl

Lesiok
29th June 2017, 12:14
What does it mean "don't work" ? Can You attach sample file ?

joovoo
29th June 2017, 22:24
OK! THans Lesiok! This is the sample file...12506
For ur idea Syrian Lucianos your solution dont work...im sorry!!

Lesiok
30th June 2017, 08:04
File is OK (that means it contains exactly what you are writing). The problem is with Windows pseudostandards.
Read what QTextStream::endl does.

joovoo
30th June 2017, 17:11
Ok Lesiok....i read it as u said! But i didn't get what syntax error i've done...! Please can u explain me more? The file contain exactly what im writting...but where i try to go on another line of the file by the '\n' or 'endl',it doesn't do anything....!!!!!

Lesiok
30th June 2017, 18:21
Because under Windows the end of the line is CR LF. In your file you have only LF which is good for Linux. So as doc says : Note: On Windows, all '\n' characters are written as '\r\n' if QTextStream's device or string is opened using the QIODevice::Text flag. You must open QTextStream like this :
QTextStream out(&file,QIODevice::ReadWrite | QIODevice::Text);

joovoo
30th June 2017, 19:53
Thanks you very much Lesiok...Now "endl;" work well !!!! But when i put as u said the compiler show the error that " no match fonction QTextStream (QFile*,QIODevice::OpenModeFlags),no known conversion from QFile* to FILE*.". So i then decide to open the QFile in QIODevice::Text instead of open the QTextStream...Thank a lot...!!!!