I took a C++ class in my high school, and we used the compiler"Borlan C++ Builder"
I think that's what it was called, anyways, I was doing good in it, and everything was working fine, but I wanted to learn some more after the class was done.
So I download Dev-C++ and the first thing a did was open up some of my programs I made in school, and none of them worked :(
Here's one of them
I wanna know why none of my programs work on this compiler
main()
{
int dice1 = 0;
int dice2 = 0;
int total = 0;
int rolls = 0;
int i;
int twos = 0;
int threes = 0;
int fours = 0;
int fives = 0;
int sixes = 0;
int sevens = 0;
int eights = 0;
int nines = 0;
int tens = 0;
int elevens = 0;
int twelves = 0;
cout << "How many times would you like the dice to roll? ";
cin >> rolls;
clrscr();
switch(total)
{
case 2:
twos++;
break;
case 3:
threes++;
break;
case 4:
fours++;
break;
case 5:
fives++;
break;
case 6:
sixes++;
break;
case 7:
sevens++;
break;
case 8:
eights++;
break;
case 9:
nines++;
break;
case 10:
tens++;
break;
case 11:
elevens++;
break;
case 12:
twelves++;
break;
}
}
What compiler errors are you receiving? At a quick glance at your code, I would point towards the clrscrn(); call. Try replacing that with a system("cls"); instead and see if that works for you. Also, main always returns a value, so it wouldn't hurt to add return 0; right before the last closing brace.
There's a few issues here and maingeek has pointed out a couple of them (although you can't just "return 0" without changing "main()" to "int main()"). I've fixed your code so it runs under Dev-Cpp. Comments are included. Let me know if it doesn't make sense.
You should just be able to cut and paste it into your compiler.
/* This uses the "code" tags so that it looks pretty, you should too! - try pressing
the "Insert Code" button */
/* it's worth knowing that C++ does not use exactly the same headers as C and
that failure to use the RIGHT header can lead to very subtle problems.
See web link on C vs C++ headers below */
// http://www.cplusplus.com/forum/general/127/
/* Generally the C header <header.h> is replaced in C++ by <header> so the
next line gets changed to;
*/
#include <iostream>
// the next two are okay, though...
#include <conio.h>
#include <stdlib.h>
/* you have to include this -- see weblink on "using namespace std;" for more */
// http://forums.devshed.com/c-programming-42/using-std-namespace-what-does-it-mean-45679.htmlusingnamespace std;
/* your use of "main()" is very non-standard. So, by the way, is the common
alternative "void main()". Standard C++ prefers the use of; */
// int main(int argc, char * argv[]);
/* BUT if you MUST use a shortened version, the below is acceptable -- it
does require the inclusion of a return statment at the end of the code.
I have used "return 0;" as returning a value of "0" is considered the
standard "everything worked okay" message. More advanced programs use
other approaches
*/
int main()
{
int dice1 = 0;
int dice2 = 0;
int total = 0;
int rolls = 0;
// int i; <-- relocated to if() statement (see below)
int twos = 0;
int threes = 0;
int fours = 0;
int fives = 0;
int sixes = 0;
int sevens = 0;
int eights = 0;
int nines = 0;
int tens = 0;
int elevens = 0;
int twelves = 0;
cout << "How many times would you like the dice to roll? ";
cin >> rolls;
/* the line below was "cls();" - a call to a borland specific command
which has no right to exist in this or any alternate universe.
ALWAYS use standard commands -- or else your code won't compile
on other compilers like, Oh say, Dev-Cpp :)
I changed it to a standard system call
*/
system("cls");
/* the next line changed from "for (i = 1; i <= rolls; i++)"
two issues here -- first it's considered good practice to define a
constant as close as possible to first use, so I deleted the
declaration (above) and moved it here
second, why is the for loop starting at i=1? Given that the test
condition is 1 LESS THAN the number of rolls requested you need
to start at i = 0 -- try it with 1 roll and you'll get the point
(you should ALWAYS try out your limit conditions as these are most
likely to give you problems)
*/
for(int i = 1; i <= rolls; i++)
{
/* both the below lines called the function "random(6)" which I can
only imagine is another Borland specific feature. I've changed it
to a standard rand() command - see weblink for more */
// http://www.daniweb.com/forums/thread1769.html
dice1 = rand()%6 + 1;
dice2 = rand()%6 + 1;
total = dice1 + dice2;
switch(total)
{
case 2:
twos++;
break;
case 3:
threes++;
break;
case 4:
fours++;
break;
case 5:
fives++;
break;
case 6:
sixes++;
break;
case 7:
sevens++;
break;
case 8:
eights++;
break;
case 9:
nines++;
break;
case 10:
tens++;
break;
case 11:
elevens++;
break;
case 12:
twelves++;
break;
}
}
cout << "#2's = " << twos;
cout << endl << "#3's = " << threes;
cout << endl << "#4's = " << fours;
cout << endl << "#5's = " << fives;
cout << endl << "#6's = " << sixes;
cout << endl << "#7's = " << sevens;
cout << endl << "#8's = " << eights;
cout << endl << "#9's = " << nines;
cout << endl << "#10's = " << tens;
cout << endl << "#11's = " << elevens;
cout << endl << "#12's = " << twelves;
/* The next two lines replace "getch();" -- the cout call simply
adds a couple of line returns to make it easy on the eyes.
"system("pause");" is a standard system command which puts a
"press any key to continue..." message to the screen.
The reason this is better than "getch()" is that it actually lets
the user know what it is you want them to do. You could simply
have included a "cout << "Press any key to continue...";" before
"getch();" though.
*/
cout << "\n\n";
system("pause");
/* finally, we provide a return value. Remember that we changed the
main function declaration to "int main()"? As such, main() now returns
a value to the calling procedure (i.e. the operating system) and this
return value can be used to determine error conditions -- if any exist.
You could, for instance, define an integer which you set to particular
values if certain errors occur and then return this integer value to
the system.
*/
return 0;
}
Why doesn't your school teach standard C++? Good question!
I guess if they use the Borland compiler as a teaching aid, then there's little reason to worry about using Borland specific code. Apart from that, I think a great deal can be put down to nothing more than bad habits. As long as things work, and as long as everybody else is doing it, there isn't very much impetus to change.
The problem is (as you've found out) that it gets terribly frustrating when something goes wrong -- especially when you've just been doing the same old (non-standard!) thing you've always done!
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#define MIN_DICE_ROLL 2
#define MAX_DICE_ROLL 12
//change dice sides here but you need to change your max too
#define SIDED_DICE 6
usingnamespace std;
int main()
{
int sumOfRolls[MAX_DICE_ROLL]; //index 0 and 1 are never used
for(int i = 0; i <= MAX_DICE_ROLL; i++)
{
sumOfRolls[i] = 0;
}
int dice1 = 0;
int dice2 = 0;
int rolls = 0;
while( rolls <= 0)
{
cout << "How many times would you like the dice to roll? ";
cin >> rolls;
system("cls");
}
for(int i = 1; i <= rolls; i++)
{
dice1 = rand()%SIDED_DICE + 1;
dice2 = rand()%SIDED_DICE + 1;
sumOfRolls[dice1 + dice2]++;
}
for( int i = MIN_DICE_ROLL; i <= MAX_DICE_ROLL; i++)
{
cout << "#" << i << "'s " << sumOfRolls[i] << endl;
}
system("pause");
return 0;
}