PDA

View Full Version : C++ frasi random


onos3nd4i
01-04-2003, 01:53
ciao a tutti.
avete per caso sottomano un piccolo esempio in C++ di un generatore di frasi random che prende da un file?
mi spiego meglio, a me serve un sorgente in C++ che scriva una frase a caso presa da un file (contenente varie frasi).

mi aiutate??

grazie

cionci
01-04-2003, 13:19
int getNum()
{
char buf[1024];
//calcolo il numero di stringhe presente nel file
ifstream fi("frasi.txt");
int i = 0;
while(!fi.oef() && !fi.fail()) {
fi.getline(buf, 1024)
if(!fi.oef() && !fi.fail())
++i;
}
fi.close();
}

string extract(const int num)
{
char buf[1024];
ifstream fi("frasi.txt");
int i = srand()%num;
while(!fi.oef() && !fi.fail() && i-- > 0) {
fi.getline(buf, 1024)
}
fi.close();
return string(buf);
}

Questo dovrebbe andare bene...
Ah...non l'ho provato...

Hardware Upgrade Forum Database Error
Database Error Database error
The Hardware Upgrade Forum database has encountered a problem.

Please try the following:
  • Load the page again by clicking the Refresh button in your web browser.
  • Open the www.hwupgrade.it home page, then try to open another page.
  • Click the Back button to try another link.
The www.hwupgrade.it forum technical staff have been notified of the error, though you may contact them if the problem persists.
 
We apologise for any inconvenience.