Sep 26, 2009 at 10:14am UTC
Hi everyone!
I have a question. I'm now making a program (in C++ ofcourse) which prints out some funny quotes on the screen, not very hard to make. But here comes the thing: I made the program so that it 'reads' a .txt file called 'quotes.txt'. In here are a few quotes I added so far. Works fine, but what I wanted to do next is that the program 'reads' the .txt file and the randomly chooses a quote (i.e. a random line in the .txt file since every quote starts on a new line). Unfortunatly I have absolutely no idea how to make something like that...
The code I have so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
// 'iQuote'
// '01 Main.cpp'
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
char retry;
int quote()
{
string line;
ifstream quotefile ("quotes.txt" );
do
{
if (quotefile.is_open())
{
while (! quotefile.eof())
{
getline (quotefile,line);
cout << line << endl;
}
quotefile.close();
}
else
{
cout << "ERROR #1: UNABLE TO OPEN FILE!" << endl;
cout << "" << endl;
cout << "Try again? Y = Retry, N = Quit" << endl;
cin >> retry;
cout << "" << endl;
}
}
while (retry == 'y' || retry == 'Y' );
return 0;
}
int main()
{
cout << "QQ QQ Q QQQ Q Q QQQ QQQ QQQ QQ QQ " << endl;
cout << "QQ QQ Q Q Q Q Q Q Q Q QQ QQ " << endl;
cout << " Q Q Q Q Q Q Q Q Q Q Q Q Q " << endl;
cout << "Q Q Q Q Q Q Q Q Q Q QQ Q Q " << endl;
cout << " Q QQQ Q Q Q Q Q Q " << endl;
cout << " Q QQ QQQ QQQ Q QQQ " << endl;
cout << "" << endl;
cout << "If you haven't got anything clever to say at the moment, use these!" << endl;
cout << "Just repeat the sentence and anyone will instantly agree with you!" << endl;
cout << "" << endl;
quote();
cout << "" << endl;
return 0;
}
All it does now is print the content (quotes) from the .txt file on the screen.
Any help would be very pleased!
Last edited on Sep 26, 2009 at 10:15am UTC
Sep 26, 2009 at 11:15am UTC
You can do this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <vector>
void quote()
{
char retry = ' ' ;
int lineCount = 0;
int randomQuote_int = 0;
std::string line = "" ;
std::vector<std::string>randomQuote_vect;
std::ifstream quoteFile ("quotes.txt" );
do
{
srand(time(0));
if (quoteFile.is_open())
{
while (!quoteFile.eof())
{
getline(quoteFile, line);
if (line != " " )
{
randomQuote_vect.push_back(line);
lineCount++;
}
}
randomQuote_int = rand() % lineCount;
std::cout << randomQuote_vect[randomQuote_int] << std::endl;
quoteFile.close();
}
else
{
std::cout << "Error. Unable to open file." << std::endl;
std::cout << "Try again? [Y]es / [N]o" << std::endl;
}
} while (retry == 'Y' || retry == 'y' );
}
int main()
{
std::cout << "QQ QQ Q QQQ Q Q QQQ QQQ QQQ QQ QQ " << std::endl;
std::cout << "QQ QQ Q Q Q Q Q Q Q Q QQ QQ " << std::endl;
std::cout << " Q Q Q Q Q Q Q Q Q Q Q Q Q " << std::endl;
std::cout << "Q Q Q Q Q Q Q Q Q Q QQ Q Q " << std::endl;
std::cout << " Q QQQ Q Q Q Q Q Q " << std::endl;
std::cout << " Q QQ QQQ QQQ Q QQQ " << std::endl;
std::cout << "If you haven't got anything clever to say at the moment, use these!" << std::endl;
std::cout << "Just repeat the sentence and anyone will instantly agree with you!" << std::endl;
quote();
return (0);
}
Put the lines into a vector then use a seeded random number generator to decide which quote to use.
Last edited on Sep 26, 2009 at 11:16am UTC
Sep 26, 2009 at 12:58pm UTC
Hi bluezor,
I bet it works, only it says that the function 'getline()' in line 24 is an undeclared identifier... Maybe it's just me (I still use the old Microsoft Visual C++ 6 which has (according to Vista) 'known compatability issues'. I'm installing the newer Visual C++ 2008 now.
Greets
Sep 26, 2009 at 1:05pm UTC
That's funny.. You don't really need Microsoft Visual C++ really yet imo since it's much more complicated with more eye candy. I'd suggest you get a more beginner-friendly one like Dev-C++ or Code::Blocks (one i'm using).
Sep 26, 2009 at 11:00pm UTC
Actually, he forgot an std::
in front of getline() (it is part of the std namespace as well)
Sep 27, 2009 at 1:47am UTC
Well it does in mine (VC++).
Sep 27, 2009 at 9:39am UTC
Thanks alot! Works perfectly! This is what I was looking for, thanks a million!
Greets, Splinter007