I'm learning C++, and I thought that I knew what I was doing until I wrote the following program to display the song, "The Twelve Days of Christmas" using strings and arrays.
Everytime I exectute the program, it runs, shows the output for a second, then terminates.
I've tried the standard pauss, and the cin tactic, but it does the same thing no matter what.
I'm using Dev-C++ compiler and I haven't had any problems with any of the other programs that I've written.
string verse[TOTAL_VERSE] = {"a partridge in a pear tree!", "Two turtle doves,\nAnd", "Three French hens,",
"Four calling birds,", "Five golden rings,", "Six geese a-laying,",
"Seven swans a-swimming,", "Eight maids a-milking,","Nine ladies dancing,","Eleven pipers piping,", "Ten lords a-leaping,", "Twelve drummers drumming,"};
int xday = 0;
for ( xday = 0; xday <= TOTAL_DAYS; xday++ )
{
int xverse = xday;
cout << endl <<"On the " << days[xday] << " day of Christmas, my true love gave to me: " << endl;
First of all I don't see how you compiled it if you didn't include <string> header. Second of all, your first for loop: for ( xday = 0; xday <= TOTAL_DAYS; xday++ ) does one more repetition than needed. On final repetition, xday's value is 12, and when you type cout << endl <<"On the " << days[xday] << " day of Christmas, my true love gave to me: " << endl; it tries to access element days[12] which doesn't exist. So just type xday < TOTAL_DAYS;
DevC++ is not a compiler, it is an IDE, and a bad one at that. Sorry to burst your bubble, but it's the harsh reality ;) http://cplusplus.com/articles/36vU7k9E/
As for stopping the console close, there is a long thread about it: http://cplusplus.com/forum/beginner/1988/
If you start using the Code::Blocks IDE, it will keep the console window open for you automatically.
to: xander314
First, I know that I need to use another compiler, I have MS Visual 2010, but it's a slow trial-and-error learning process with it. I can't even figure out how to execute it to pop up in the terminal.
Second, I used the close console that you threaded to:
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
return 0;
}
and it's still closing.
But...
to: bl4ckb3rry:
the #include <string> is there, I just missed it on my copy and paste.
And that stupid relational operator was the reason, THANK YOU! I've been beating my head against the computer for the last four hours trying to figure that out.
everything runs and stops now. Thanks again.
Also, Xander, thanks for the console close link, even though I'm new, I want to learn everything the right, and compatible, way first, as too not develop any bad habits.