PDA

Visualizza la versione completa : [C++] Caratteri aggiuntivi indesiderati all'interno del file di testo


alo
22-09-2011, 16:42
salve,
ho un problema con un programma che sto srcivendo. Quando il programma genera i file member.txt e access_data.txt alla fine mi inserisce due caratteri in piu... chi mi puo spiegare perchè??
Questo è il codice:



#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <sstream>
using namespace std;

#define space ' '
#define newline '\n'
#define tab '\t'

//inizializing function
void main_page();
void reg_page();
void reg_write();
void access_page();
void access_write();
void pause();
void cls();

struct member {
char name[50];
char surname[50];
char year[4];
};

//global variable
bool memb;

//main function
int main() {
string syear;
member a;
member * p;
p = &a;
do {
//call the main page function
main_page();
//if you aren't a member...
if (memb!=1) {
//start registration step
reg_page();
reg_write();
memb=true;
//end registration step
}
//if you are a member...
access_page();
access_write();
memb=true;
}while (memb=false, memb=true);
}

//main page function
void main_page() {
cout << newline << newline << newline;
cout << tab << tab << "WELCOME TO LIBRARY";
cout << newline << newline << newline;
cout << tab << "ARE YOU A MEMBER? (1=si, 0=no) --> ";
cin >> memb;
}

//access page function
void access_page() {
cout << newline << newline << newline;
cout << "Remember that the access data will be save, so if you aren't a member is better don't continue";
cout << newline << newline;
cout << tab << tab << "ACCESS:";
cout << newline << newline;
}

//write access_data.txt
void access_write() {
member a;
member * p;
p = &a;
fstream f2;
f2.open ("access_data.txt", ios::out);
cout << tab << tab << "NAME: ";
cin >> p->name;
f2 << p->name << tab;
cout << tab << tab << "SURNAME: ";
cin >> p->surname;
f2 << p->surname << tab;
cout << tab << tab << "BORN DATE: ";
cin >> p->year;
(stringstream) p->year;
f2 << p->year;
f2 << newline;
f2.close();
}

//registration page function
void reg_page() {
cout << newline << newline << newline;
cout << tab << tab << "REGISTRATION:";
cout << newline << newline;
}

//write member.txt
void reg_write() {
member a;
member * p;
p = &a;
fstream f1;
f1.open("members.txt", ios::out);
cout << tab << tab << "NAME: ";
cin >> p->name;
f1 << p->name << tab;
cout << tab << tab << "SURNAME: ";
cin >> p->surname;
f1 << p->surname << tab;
cout << tab << tab << "BORN DATE: ";
cin >> p->year;
(stringstream) p->year;
f1 << p->year;
f1 << newline;
cout << newline << newline;
cout << "REGISTRATION COMPLETE";
f1.close();
}

void pause() {
system("PAUSE");
}

void cls() {
system("CLS");
}



grazie in anticipo :)

oregon
22-09-2011, 17:11
Che vuoi dire? Quali caratteri? E come fai a vedere che sono 2?

Saranno i newline che inserisci?

P.S. Il linguaggio va nel titolo ... vedi il regolamento del forum ..

alo
22-09-2011, 17:14
quando apro i file member e access_data dopo l'anno ci sono dei caratteri...

oregon
22-09-2011, 17:40
Beh, per cominciare, per l'elemento

char year[4];

della struttura, devi prevedere 5 caratteri e non 4 per il terminatore ...

E poi ti consiglio di dare un'occhiata a questa linea

while (memb=false, memb=true);

perché non mi pare proprio corretta ...

alka
22-09-2011, 17:46
Originariamente inviato da oregon
P.S. Il linguaggio va nel titolo ... vedi il regolamento del forum ..

Quoto, e aggiungo che il titolo dovrebbe anche essere più significativo, come da Regolamento (http://forum.html.it/forum/showthread.php?s=&threadid=973887).

alo
22-09-2011, 17:53
si giusto :)
grazie a tutti :)

alo
22-09-2011, 21:38
ho modificato il codice in questo modo:


#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <sstream>
using namespace std;

#define space ' '
#define newline '\n'
#define tab '\t'

//inizializing function
void main_page();
void reg_page();
void reg_write();
void access_page();
void access_write();
void pause();
void cls();

struct books {
char title[50];
char author[50];
char editor[50];
int pages[10000];
};

//global variable
bool memb;

//main function
int main() {
do {
//call the main page function
main_page();
//if you aren't a member...
if (memb!=1) {
//start registration step
reg_page();
reg_write();
memb=true;
//end registration step
}
//if you are a member...
access_page();
access_write();
memb=true;
}while (memb=false);
}

//main page function
void main_page() {
cout << newline << newline << newline;
cout << tab << tab << "WELCOME TO LIBRARY";
cout << newline << newline << newline;
cout << tab << "ARE YOU A MEMBER? (1=si, 0=no) --> ";
cin >> memb;
}

//access page function
void access_page() {
cout << newline << newline << newline;
cout << "Remember that the access data will be save, so if you aren't a member is better don't continue";
cout << newline << newline;
cout << tab << tab << "ACCESS:";
cout << newline << newline;
}

//write access_data.txt
void access_write() {
char name[50];
char surname[50];
char year[5];

ofstream access("access_data.txt", ios::app);

cout << tab << tab << "NAME: ";
cin >> name;
cout << tab << tab << "SURNAME: ";
cin >> surname;
cout << tab << tab << "BORN DATE: ";
cin >> year;
(stringstream) year;
access << newline;
access << "----------------------" << endl;
access << "NAME: " << name << endl;
access << "SURNAME: " << surname << endl;
access << "BORN DATE: " << year << endl;
access << "----------------------" << endl;
access << newline;
}

//registration page function
void reg_page() {
cout << newline << newline << newline;
cout << tab << tab << "REGISTRATION:";
cout << newline << newline;
}

//write member.txt
void reg_write() {
char name[50];
char surname[50];
char year[5];

ofstream member("members.txt", ios::app);

cout << tab << tab << "NAME: ";
cin >> name;
cout << tab << tab << "SURNAME: ";
cin >> surname;
cout << tab << tab << "BORN DATE: ";
cin >> year;
(stringstream) year;
member << newline;
member << "----------------------" << endl;
member << "NAME: " << name << endl;
member << "SURNAME: " << surname << endl;
member << "BORN DATE: " << year << endl;
member << "----------------------" << endl;
member << newline;
}

void pause() {
system("PAUSE");
}

void clear() {
system("CLS");
}

funziona alla grande :)

Loading