Consol Closing Despite My Best Efforts

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.


#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main(int argc, char *argv[])
{
int tb;
const int TOTAL_DAYS = 12,
TOTAL_VERSE = 12;

string days[TOTAL_DAYS] = {"first", "second", "third", "fourth", "fifth", "sixth", "seventh",
"eighth", "ninth", "tenth", "eleventh", "twelfth"};

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;

for ( xverse = xday; xverse >= 0; --xverse )
{
cout << verse[xverse] << endl;
}

}



system("PAUSE");
return EXIT_SUCCESS;
}

I think it has something to do with my second for loop accessing the array, but i don't know.

Any help is apprieciated.

Thanks.
Last edited on
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;

Happy coding!
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.
closed account (zb0S216C)
bl4ckb3rry wrote:
First of all I don't see how you compiled it if you didn't include <string> header
<iostream> includes string( in VCE 2010 ).

Wazzak

Last edited on
thanks for the quick replys,

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.




Topic archived. No new replies allowed.