Thread: [Dev-C++] compiler psychiatrics
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Carlos d. M. <cg...@wo...> - 2002-08-07 18:48:34
|
Here is an old experiment for programming, when I still did not know
C++, (only a piece of the program, so I don't get you buried under a ton
of code). You run fine?
There are two lines, in red, that invert their order, the scanf is done
before its printf.
printf("and then choose the weight\n");//THESE TWO LINES CHANGE THE
ORDERING!!!!!
scanf("%d\n", &weight);
You see (adding two lines before)
___________________________________
| Title _0X |
|-----------------------------------------------|
| [...] |
| Choose the height. |
| 180 |
| _ |
|___________________________________|
The program asks for the height, you enter the height you want, 180, and
then you wait the next prompt for the weight, but you find there is the
cursor one line below, waiting your input...
___________________________________
| Title _0X |
|-----------------------------------------------|
| Choose the height. |
| 180 |
| 80 |
| And then choose the weight |
|___________________________________|
And you enter any number, then the prompt appears, and from now on all
inputs are before its prompt. When the moment arrives when the program
prints your character's statistics you see what went where. It continues
doing so till the end.
Copying the scanf after the printf and having two scanf only makes the
error repeat in the next pair printf/scanf
The best is that it happens when I compile it with borland 5.5.
/*a text-based fighting game*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int genre, level, answ, result, end, weight, tall, radius, prop, pula;
int vic, mvic, round, mov, mmov, posa, posb, posc, posd, pose, monster,
hit;
int agil, magil;
int a, u;
int difab, difup, difpta, difptb;
int strenght;
int abwer, upwer, piwer, mawer, cabwer, arwer;
int mabwer, mupwer, mpuiwer, mmawer, mcabwer, marwer;
int atpts, dfpts, agpts, witpts, velpts, dampts, sufpts, respts;
int matpts, mdfpts, magpts, mwitpts, mvelpts, mdampts, msufpts, mrespts;
int oat, odf, oag, owit, ovel, odam, ores;
char name[20];
char wear[16];
char clothab[16];
char clothup[16];
char abdo[] = "skirt";
char abcer[] = "skirt";
char abon[] = "pants";
char abtre[] = "leather pants";
char abfor[] = "leather skirt";
char abcin[] = "magic pants";
char abses[] = "magic skirt";
char absep[] = "armor pants";
char upcer[] = "body";
char upon[] = "shirt";
char updo[] = "jacket";
char uptre[] = "leather jacket";
char upfor[] = "armor plate";
char upcin[] = "leather armor";
char upsis[] = "armor";
char upsep[] = "magic armor";
srand(time(NULL));
strenght = 0;
while(strenght == 0){ /*decides if there will be more fights
###first###*/
printf("Choose the character you want to control, first the genre, 0 for
female, 1 for male.\n");
scanf("%d", &genre);
printf(" And the name, no more of 19 letters.\n");
scanf("%s", name);
printf("Now choose the height,\n");
scanf("%d\n", &tall);
printf("and then choose the weight\n");//THESE TWO LINES CHANGE THE
ORDERING!!!!!
scanf("%d\n", &weight);
printf("choose the belt radius\n");
scanf("%d\n", &radius);
printf("Choose muscle/bone proportion\n");
scanf("%d\n", &prop);
printf ("your character is a ");
if (genre == 0)
printf("female ");
else
printf ("male ");
/*if((prop >= 33) || (prop < 15)){ /*stablish a bone proportion if
the set by the player is unacceptable*/
prop = rand() % 5;
if(prop == 0)
prop = 20;
else
if(prop == 1)
prop = 23;
else
if(prop == 2)
prop = 25;
else
if(prop == 3)
prop = 28;
else
prop = 30;
} */ /*closes the stablishment of a
correct bone proportion*/
printf("called %s, of %d centimeters and %d kilogrames, with a
muscle-bone proprotion of %d, and belt radius of %d. So, your character
is ", name, tall, weight, prop, radius);
if ((prop <= 32) && (prop > 29))
printf("very strong ");
else
if ((prop <= 29) && (prop > 27))
printf("strong ");
else
if((prop <=27) && (prop > 22))
printf("normal in strenght ");
else
if((prop <= 22) && (prop > 14))
printf("weak ");
if(weight > (tall - 85)){
if(genre == 0)
printf("his/her size is only appearance ");
else
printf("his/her size is only appearance ");
}
else
if(prop <= 10){
printf("very weak ");
if(weight > (tall - 85))
printf("the mass is evidently uncompetent");
}
agil = (tall - weight + 100) - (radius / 2) + 70;
if(agil > 250)
printf("and very agile\n");
else
if((agil <= 250) && (agil > 235))
printf("and agile\n");
else
if((agil <= 235) && (agil > 215))
printf("and normal in agility\n");
else
if((agil <= 215) && (agil > 200))
printf("and slow\n");
else
if(agil <= 200)
printf("and completely lack of reflex\n");
printf("You can choose the next wearings:\n");
printf("Feet wearing: shoes, 1; boots, 2; long boots, 3; armor boots,
4.\n");
scanf("%d\n", &piwer);
printf("Legs wearing: pants, 1; skirt, 2; leather pants, 3; leather
skirt, 4; magic pants, 5; magic skirt, 6; armour pants, 7.\n");
scanf("%d\n", &abwer);
switch(abwer){
case 0:
strcpy(clothab, abcer);
break;
case 1:
strcpy(clothab, abon);
break;
case 2:
strcpy(clothab, abdo);
break;
case 3:
strcpy(clothab, abtre);
break;
case 4:
strcpy(clothab, abfor);
break;
case 5:
strcpy(clothab, abcin);
break;
case 6:
strcpy(clothab, abses);
break;
case 7:
strcpy(clothab, absep);
break;
}
printf("Body wearing: shirt, 1; jacket, 2; leather jacket, 3; armor
plate, 4; leather armor, 5; armor, 6; magic armor, 7.\n");
scanf("%d\n", &upwer);
switch(upwer){
case 0:
strcpy(clothup, upcer);
break;
case 1:
strcpy(clothup, upon);
break;
case 2:
strcpy(clothup, updo);
break;
case 3:
strcpy(clothup, uptre);
break;
case 4:
strcpy(clothup, upfor);
break;
case 5:
strcpy(clothup, upcin);
break;
case 6:
strcpy(clothup, upsis);
break;
case 7:
strcpy(clothup, upsep);
break;
}
printf("Head wearing: nothing, 1; hat, 2; helmet, 3;whole helm, 4.\n");
scanf("%d\n", &cabwer);
printf("Hand wearing: nothing, 1; glovers, 2; armor glovers, 3; armed
glovers, 4.\n");
scanf("%d\n", &mawer);
printf("weapon: none, 1; knife, 2; bar, 3; long bar,4; articulate bars,
5;bow, 6; long bow, 7; little axe, 8; big axe, 9; axe and net, 10; short
sword, 11; long sword, 12; sword and shield, 13;.\n");
scanf("%d", &arwer);
atpts = ((arwer + 1) * 30) + ((prop -15) * 40) + ((piwer + 1) * 50);
dfpts = ((agil) * 2) + ((abwer +1) * 15) + ((piwer +1) * 5) + ((cabwer
+1) * 150) + ((upwer +1) * 300) - (radius - 80);
if((arwer == 9) || (arwer == 10) || (arwer >= 12))
dfpts = dfpts + (arwer * 8);
else
dfpts = dfpts + (arwer * 3);
witpts = (rand() % 50) + 80;
velpts = (agil) * tall / weight;
dampts = (atpts / 2) + ((atpts * prop) / (agil));
respts = (((dfpts * velpts) / weight) + (atpts / witpts) + (agil * 10))
/ (weight / 4);
sufpts = (rand() % 10) + 2;
printf("attack points = %d\n defense points = %d\n wits points = %d\n
velocity points = %d\n damage points = %d\n resistence points = %d\n
suffering points = %d\n", atpts, dfpts, witpts, velpts, dampts, respts,
sufpts);
round = 0;
ores = respts;
ovel = velpts;
odam = dampts;
owit = witpts;
oat = atpts;
odf = dfpts;
a = abwer;
u = upwer;
difab = 0;
difup = 0;
difpta = (abwer * 15) / dfpts;
difptb = (upwer * 300) / dfpts;
printf("press 0 if you want to play again\n");
scanf("%d", &strenght);
} /*##first##*/
return 0;
}
P.S.- Sorry for my firsts shaming posts at this list, a year ago, more
or less, I regret having written so big stupidities, as I wrote those
couple of messages. Forgive me and forget them. (Yeah, I know, this line
should be in an older message, when the referenced messages were
remembered).
Thank you all
|
|
From: Ioannis V. <no...@ho...> - 2002-08-08 08:15:49
|
I assume you coded in C.=20
=20
=20
=20
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of Carlos
Garc=EDa del Monte
Sent: Wednesday, August 07, 2002 9:44 PM
To: dev-cpp-users-list
Subject: [Dev-C++] compiler psychiatrics
Here is an old experiment for programming, when I still did not know
C++, (only a piece of the program, so I don't get you buried under a ton
of code). You run fine?=20
There are two lines, in red, that invert their order, the scanf is done
before its printf.=20
printf("and then choose the weight\n");//THESE TWO LINES CHANGE THE
ORDERING!!!!!=20
scanf("%d\n", &weight);=20
You see (adding two lines before)=20
___________________________________=20
| Title _0X |=20
|-----------------------------------------------|=20
| [...] |
| Choose the height. |=20
| 180 |=20
| _ |=20
|___________________________________|=20
The program asks for the height, you enter the height you want, 180, and
then you wait the next prompt for the weight, but you find there is the
cursor one line below, waiting your input...=20
___________________________________=20
| Title _0X |=20
|-----------------------------------------------|=20
| Choose the height. |=20
| 180 |=20
| 80 |=20
| And then choose the weight |=20
|___________________________________|=20
And you enter any number, then the prompt appears, and from now on all
inputs are before its prompt. When the moment arrives when the program
prints your character's statistics you see what went where. It continues
doing so till the end.=20
Copying the scanf after the printf and having two scanf only makes the
error repeat in the next pair printf/scanf=20
The best is that it happens when I compile it with borland 5.5.=20
/*a text-based fighting game*/=20
#include <stdio.h>=20
#include <stdlib.h>=20
#include <time.h>=20
int main()=20
{=20
int genre, level, answ, result, end, weight, tall, radius, prop, pula;=20
int vic, mvic, round, mov, mmov, posa, posb, posc, posd, pose, monster,
hit;=20
int agil, magil;=20
int a, u;=20
int difab, difup, difpta, difptb;=20
int strenght;=20
int abwer, upwer, piwer, mawer, cabwer, arwer;=20
int mabwer, mupwer, mpuiwer, mmawer, mcabwer, marwer;=20
int atpts, dfpts, agpts, witpts, velpts, dampts, sufpts, respts;=20
int matpts, mdfpts, magpts, mwitpts, mvelpts, mdampts, msufpts, mrespts;
int oat, odf, oag, owit, ovel, odam, ores;=20
char name[20];=20
char wear[16];=20
char clothab[16];=20
char clothup[16];=20
char abdo[] =3D "skirt";=20
char abcer[] =3D "skirt";=20
char abon[] =3D "pants";=20
char abtre[] =3D "leather pants";=20
char abfor[] =3D "leather skirt";=20
char abcin[] =3D "magic pants";=20
char abses[] =3D "magic skirt";=20
char absep[] =3D "armor pants";=20
char upcer[] =3D "body";=20
char upon[] =3D "shirt";=20
char updo[] =3D "jacket";=20
char uptre[] =3D "leather jacket";=20
char upfor[] =3D "armor plate";=20
char upcin[] =3D "leather armor";=20
char upsis[] =3D "armor";=20
char upsep[] =3D "magic armor";=20
srand(time(NULL));=20
strenght =3D 0;=20
while(strenght =3D=3D 0){ /*decides if there will be more fights
###first###*/=20
printf("Choose the character you want to control, first the genre, 0 for
female, 1 for male.\n");=20
scanf("%d", &genre);=20
printf(" And the name, no more of 19 letters.\n");=20
scanf("%s", name);=20
printf("Now choose the height,\n");=20
scanf("%d\n", &tall);=20
printf("and then choose the weight\n");//THESE TWO LINES CHANGE THE
ORDERING!!!!! =20
=20
The above style of comment is not ANSI C90 compliant.
=20
=20
=20
scanf("%d\n", &weight);=20
printf("choose the belt radius\n");=20
scanf("%d\n", &radius);=20
printf("Choose muscle/bone proportion\n");=20
scanf("%d\n", &prop);=20
printf ("your character is a ");=20
if (genre =3D=3D 0)=20
printf("female ");=20
else=20
printf ("male ");=20
/*if((prop >=3D 33) || (prop < 15)){ /*stablish a bone proportion if
the set by the player is unacceptable*/=20
=20
=20
Above you begin a comment and inside it another comment which ends at
the same line. The */ terminator terminates the first /*. So you think
you have commented all the block below while you did not!=20
=20
=20
prop =3D rand() % 5;=20
if(prop =3D=3D 0)=20
prop =3D 20;=20
else=20
if(prop =3D=3D 1)=20
prop =3D 23;=20
else=20
if(prop =3D=3D 2)=20
prop =3D 25;=20
else=20
if(prop =3D=3D 3)=20
prop =3D 28;=20
else=20
prop =3D 30;=20
} */ =20
=20
Nope, the above */ closes nothing and is a syntax error.
=20
=20
So here are the bugs i discovered witg a quick pass. Bad use of
comments, and REMOVE those \n and commas from scanf(). After you do the
above, resend the fixed code.
=20
=20
=20
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.cjb.net
<http://www.noicys.cjb.net/>=20
* Alternative URL: http://run.to/noicys
|
|
From: Carlos d. M. <cg...@wo...> - 2002-08-08 11:30:10
|
The two bad comments were not in the original, I added them in the message. The // was an explanation for you about where the fail was, and since I write almost everything in C++ now I use //always. If so the compiler would have say errors errors errors!!! and bad comment. The other that interrupts is either added in the message (sorry, I had to be more carefully, although being in a message, it was for be compiled, so I had to notice they would give you errors if you try). Yeah! Those damned \n. I forgot that this experiment was the last before abandoning that custom of \n in scanfs (it was my second experiment while learning programming) and that I deleted the correction and left the original with \n (I did not reached to try the correction which works fine). And so I was not observant of that error, when I saw a \n I thought "printf"... and went on. Without it it works fine. All the other strange behavior (or possible strange, of course) are corrected and I don't have any of them at hand. |
|
From: Ioannis V. <no...@ho...> - 2002-08-08 14:38:21
|
> -----Original Message----- > From: dev...@li...=20 > [mailto:dev...@li...] On Behalf=20 > Of Carlos Garc=EDa del Monte > Sent: Thursday, August 08, 2002 2:25 PM > To: dev-cpp-users-list > Subject: Re: [Dev-C++] compiler psychiatrics >=20 >=20 > The two bad comments were not in the original, I added them in the > message. The // was an explanation for you about where the=20 > fail was, and > since I write almost everything in C++ now I use //always. If so the > compiler would have say errors errors errors!!! and bad comment. The > other that interrupts is either added in the message (sorry,=20 > I had to be > more carefully, although being in a message, it was for be=20 > compiled, so > I had to notice they would give you errors if you try). > Yeah! Those damned \n. I forgot that this experiment was the=20 > last before > abandoning that custom of \n in scanfs (it was my second experiment > while learning programming) and that I deleted the correction and left > the original with \n (I did not reached to try the correction which > works fine). And so I was not observant of that error, when I=20 > saw a \n I > thought "printf"... and went on. > Without it it works fine. > All the other strange behavior (or possible strange, of course) are > corrected and I don't have any of them at hand. Great! As you see it was not a compiler's issue. Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.cjb.net * Alternative URL: http://run.to/noicys |
|
From: Carlos d. M. <cg...@wo...> - 2002-08-08 18:35:50
|
> Great! As you see it was not a compiler's issue. Great! Yes! Now I've found and reviewed one piece of code which has the for(int i) I said it didn't work. It is an author error (author of some code example I used). The ANSI about declaring for is something like "you can declare inside a for statement in the way (int i = 0[...]), but you can't use it later outside it, if you want you have to declare it just before the for: int i; for(i=0[...]) or redeclare it". I read first C/C++, an old edition and centered in C, so it said nothing about possibility of declaring in the for. Later I bought the "C++ how to program" (which couple of inexactitudes I found, some misleading things about bitfields, I suspect are due to the translator (I bought it in spanish) who knows bad spanish (!!!)), but I knew for, and I only noticed "can be declared in", but not read "out of it it is out of scope". And later I got some code examples from some sites (I don't remember where) where they were used later, and that misled me, thinking that the error wasn't only outside, but inside, too. It's a bad thing that the last for/while rare thing is deleted. I would see a possibly frequent error. (or you a compiler strange behavior, hehehehe) and I would learn to see a error now I am unable to see (Feel I need a phrase about the sea being free). Greets. |
|
From: Ioannis V. <no...@ho...> - 2002-08-08 20:35:20
|
> -----Original Message-----
> From: dev...@li...=20
> [mailto:dev...@li...] On Behalf=20
> Of Carlos Garc=EDa del Monte
> Sent: Thursday, August 08, 2002 9:31 PM
> To: dev-cpp-users-list
> Subject: Re: [Dev-C++] compiler psychiatrics
>=20
>=20
> > Great! As you see it was not a compiler's issue.
>=20
> Great! Yes!
> Now I've found and reviewed one piece of code which has the=20
> for(int i) I
> said it didn't work. It is an author error (author of some=20
> code example I
> used). The ANSI about declaring for is something like "you can declare
> inside a for statement in the way (int i =3D 0[...]), but you=20
> can't use it
> later outside it, if you want you have to declare it just=20
> before the for:
> int i; for(i=3D0[...]) or redeclare it".
>=20
> I read first C/C++, an old edition and centered in C, so it=20
> said nothing
> about possibility of declaring in the for.
> Later I bought the "C++ how to program" (which couple of=20
> inexactitudes I
> found, some misleading things about bitfields, I suspect are=20
> due to the
> translator (I bought it in spanish) who knows bad spanish=20
> (!!!)), but I knew
> for, and I only noticed "can be declared in", but not read=20
> "out of it it is
> out of scope". And later I got some code examples from some=20
> sites (I don't
> remember where) where they were used later, and that misled=20
> me, thinking
> that the error wasn't only outside, but inside, too.
>=20
> It's a bad thing that the last for/while rare thing is=20
> deleted. I would see
> a possibly frequent error. (or you a compiler strange=20
> behavior, hehehehe)
> and I would learn to see a error now I am unable to see (Feel I need a
> phrase about the sea being free).
> Greets.
Lets clarify things a bit here. A variable has scope inside the block it
was defined. For example:
void f()
{
int i=3D0;
i++;
// local scope
{
int j=3D0;
j+=3D2;
} // end of j's scope. j is destroyed.
// another local scope
{
// This is a different i
int i=3D7;
=20
i*=3D2;
// The local i has become 14, the first i remains 1
} // end of local i's scope. i dies.
// Here we see the i with value 1
}// end of function and i's scope. i is destroyed
This applies both in C & C++. The difference between C & C++ is that in
C90 you have to define the variables in the beginning of a scope, while
in C++98 you can define it in the first place you will need it in a
scope. I 'll not discuss about C99 so as to not confuse things.
Now in C90 when we wanted to define a local variable for a for-loop we
did this trick:
void f(void)
{
{
int i;
for(i=3D0; i<10; i++)
/* stuff */
}
/* i dies */
=20
/* ... */
}
=20
Now in C++98=20
we can do
void f()
{
for(int i=3D0; i<10; i++)
{
// stuff
=20
} // i dies here
}
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.cjb.net
* Alternative URL: http://run.to/noicys
=20
|