Having some problems with different C++ compliers

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

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>


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();

for(i = 1; i <= rolls; i++)
{
dice1 = random(6) + 1;
dice2 = random(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;
getch();
}
Last edited on
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.
Last edited on
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.

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* 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.html
using namespace 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;
}



And that's it from me...
muzhogg
Last edited on
thanks a lot muzhogg, the comments are really helpful ^_^

I'll try to apply this to the rest of my programs, I hope I can get them to work

So that all leads to the question... why wouldn't my school teach standard C++?

I'm definitely coming back here if I have anymore problems, I got the answer I was looking for very quick
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!

You could condense your code if you keep your dice sum in an array like 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
#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   


using namespace 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;
}

Topic archived. No new replies allowed.