The weird while and If

S

Scott

Hi,

Now this one does not make any sense to me at all. I have a while
loop and it is not working anything like it is suppose to. I have
placed it below.

-------------------------
Count = 1;

while(count < 4)
{
if(count == 1) rating2 = security;
if(count == 2) rating2 = health;
if(count == 3) rating2 = security;

bonus = (rating2 / 10) - 5;
bonus2 = 10 * (rating2 - ((bonus + 5) * 10));
if (bonus2 < 0)
{
bonus--;
bonus2 = bonus2 + 100;
}
srand((unsigned) time(NULL));
roll = rand()/(int)(((unsigned)RAND_MAX + 1) / 100);
if (bonus2 >= roll) bonus++;

if (count = 1) fhealth = fhealth + bonus;
if (count = 2) feconomy = feconomy + bonus;
if (count = 3) feconomy = feconomy + bonus;
loop = loop - 1;
printf("feconomy: %i\n", feconomy);
printf("fhealth: %i\n", fhealth);

if (count >= 2)
{
bonus = (rating2 / 10) - 5;
bonus2 = 10 * (rating2 - ((bonus + 5) * 10));
if (bonus2 < 0)
{
bonus--;
bonus2 = bonus2 + 100;
}
srand((unsigned) time(NULL));
roll = rand()/(int)(((unsigned)RAND_MAX + 1) / 100);
if (bonus2 >= roll) bonus++;
if (count = 1) fhealth = fhealth + bonus;
if (count = 2) feconomy = feconomy + bonus;
if (count = 3) feconomy = feconomy + bonus;
}
count = count + 1;
}
-----------------------------

First, the while loop exits before reaching 3 even though I have the
while going until count is less than 4. Then, if I try and put less
than 5, something even more weird happens, it somehow goes into a
endless loop even though I have one being added in each loop.

Second, the program seems to go along as if the if with brackets does
not exsist. It simply does the code in the brackets every time, even
if the count is equal to 1.

I have spent a few hours now trying to fix this and I can't see what I
am doing wrong. I have used several ifs and whiles in my program
without any trouble.

--
Your friend,
Scott

Sent to you from a 100% Linux computer using Kubuntu Version 7.04
(Feisty Fawn)
 
G

Guest

Hi,

Now this one does not make any sense to me at all. I have a while
loop and it is not working anything like it is suppose to. I have
placed it below.

-------------------------
Count = 1;

while(count < 4)
{
if(count == 1) rating2 = security;
if(count == 2) rating2 = health;
if(count == 3) rating2 = security; [...]
if (count = 1) fhealth = fhealth + bonus;
if (count = 2) feconomy = feconomy + bonus;
if (count = 3) feconomy = feconomy + bonus; [...]
if (count = 1) fhealth = fhealth + bonus;
if (count = 2) feconomy = feconomy + bonus;
if (count = 3) feconomy = feconomy + bonus;

Do you see anything different in the first group of if conditions,
when you compare them to the second and third? :)
 
S

Spiros Bousbouras

Hi,

Now this one does not make any sense to me at all. I have a while
loop and it is not working anything like it is suppose to. I have
placed it below.

-------------------------
Count = 1;

while(count < 4)
{
if(count == 1) rating2 = security;
if(count == 2) rating2 = health;
if(count == 3) rating2 = security;

bonus = (rating2 / 10) - 5;
bonus2 = 10 * (rating2 - ((bonus + 5) * 10));
if (bonus2 < 0)
{
bonus--;
bonus2 = bonus2 + 100;
}
srand((unsigned) time(NULL));
roll = rand()/(int)(((unsigned)RAND_MAX + 1) / 100);
if (bonus2 >= roll) bonus++;

if (count = 1) fhealth = fhealth + bonus;
if (count = 2) feconomy = feconomy + bonus;
if (count = 3) feconomy = feconomy + bonus;

In the 3 lines above you have assignment instead of comparison.
I suggest you start using a code checking tool like spint ; it will
pick up silly mistakes like that.
 
A

Army1987

Scott said:
bonus = (rating2 / 10) - 5;
bonus2 = 10 * (rating2 - ((bonus + 5) * 10));
if (bonus2 < 0)
{
bonus--;
bonus2 = bonus2 + 100;
}
srand((unsigned) time(NULL));
roll = rand()/(int)(((unsigned)RAND_MAX + 1) / 100);
if (bonus2 >= roll) bonus++;
if (count = 1) fhealth = fhealth + bonus;
if (count = 2) feconomy = feconomy + bonus;
if (count = 3) feconomy = feconomy + bonus;

As you rewrite this identical code twice, how 'bout splitting it into
another function?
 
S

Spiros Bousbouras

In the 3 lines above you have assignment instead of comparison.
I suggest you start using a code checking tool like spint ; it will
pick up silly mistakes like that.

The name of the tool is splint not spint.
 
G

gw7rib

Hi,

Now this one does not make any sense to me at all. I have a while
loop and it is not working anything like it is suppose to. I have
placed it below.

Are you sure about this? You call it "count" not "Count" everywhere
else.
while(count < 4)
{
if(count == 1) rating2 = security;
if(count == 2) rating2 = health;
if(count == 3) rating2 = security;

bonus = (rating2 / 10) - 5;
bonus2 = 10 * (rating2 - ((bonus + 5) * 10));
if (bonus2 < 0)
{
bonus--;
bonus2 = bonus2 + 100;
}
srand((unsigned) time(NULL));
roll = rand()/(int)(((unsigned)RAND_MAX + 1) / 100);
if (bonus2 >= roll) bonus++;

if (count = 1) fhealth = fhealth + bonus;
if (count = 2) feconomy = feconomy + bonus;
if (count = 3) feconomy = feconomy + bonus;

Ah! You presumably mean == not = , like you did the first time.
loop = loop - 1;
printf("feconomy: %i\n", feconomy);
printf("fhealth: %i\n", fhealth);

if (count >= 2)
{
bonus = (rating2 / 10) - 5;
bonus2 = 10 * (rating2 - ((bonus + 5) * 10));
if (bonus2 < 0)
{
bonus--;
bonus2 = bonus2 + 100;
}
srand((unsigned) time(NULL));
roll = rand()/(int)(((unsigned)RAND_MAX + 1) / 100);
if (bonus2 >= roll) bonus++;
if (count = 1) fhealth = fhealth + bonus;
if (count = 2) feconomy = feconomy + bonus;
if (count = 3) feconomy = feconomy + bonus;
}
count = count + 1;
}
-----------------------------

I've only glanced at the code, so there may be other problems, but try
fixing those ones first.

Paul.
 
S

Scott

Now this one does not make any sense to me at all. I have a while loop
and it is not working anything like it is suppose to. I have placed it
below.
Sorry for bugging you, I'm a moron. I did a = when I should have done
a ==. Grrrrrrrrrr, in Basic everything was always = :)
 
S

Scott

As you rewrite this identical code twice, how 'bout splitting it into
another function?
I was thinking about doing that. I will probably need this line of
code in another function. Will I be able to access it from both
functions and return using the return; command?
 
S

Scott

The name of the tool is splint not spint.

Thank you for the suggestion, it would have saved me a lot of
time :). I have installed splint and will check it out.
 
A

army1987

I was thinking about doing that. I will probably need this line of
code in another function. Will I be able to access it from both
functions and return using the return; command?

Yes. But I propose to put all these variables in a struct, so you
won't need to pass umpteen parameters to the function.
 
C

CBFalconer

Spiros said:
In the 3 lines above you have assignment instead of comparison.
I suggest you start using a code checking tool like spint ; it
will pick up silly mistakes like that.

No, simply get in the habit of writing the constant first, as in:

if (3 == count) ...

and the compiler will object if you have a typo in the '=='. Now
prepare for the onslaught of the "It's ugly" "It's unnatural" etc.
crowd.
 
K

Keith Thompson

CBFalconer said:
Spiros Bousbouras wrote: [...]
In the 3 lines above you have assignment instead of comparison.
I suggest you start using a code checking tool like spint ; it
will pick up silly mistakes like that.

No, simply get in the habit of writing the constant first, as in:

if (3 == count) ...

and the compiler will object if you have a typo in the '=='. Now
prepare for the onslaught of the "It's ugly" "It's unnatural" etc.
crowd.

Glad to oblige. It's ugly. But if you find it sufficiently useful,
feel free to use it.
 
R

Richard Heathfield

Keith Thompson said:
CBFalconer said:
Spiros Bousbouras wrote: [...]
In the 3 lines above you have assignment instead of comparison.
I suggest you start using a code checking tool like spint ; it
will pick up silly mistakes like that.

No, simply get in the habit of writing the constant first, as in:

if (3 == count) ...

and the compiler will object if you have a typo in the '=='. Now
prepare for the onslaught of the "It's ugly" "It's unnatural" etc.
crowd.

Glad to oblige. It's ugly. But if you find it sufficiently useful,
feel free to use it.

And remember that ugly is in the eye of the beholder. I can see no
aesthetic difference whatsoever between if(x == y) and if(y == x).
 
S

santosh

Scott said:
I was thinking about doing that. I will probably need this line of
code in another function. Will I be able to access it from both
functions and return using the return; command?

Yes. As Army1987 says you might want to "encapsulate" related objects
into one or more structures and pass these, (or pointers to them),
around. A thing to note:

In C, function arguments are always passed by value, i.e., a new copy
of that object is passed to each function, not the original object
that exists in the calling function. Example:

void f1(void) {
int a =0, b = 1;
f2(a, b);
}

void f2(int x, int y) {
int m = x, n = y;
f3(m, n);
}

Here when function f2 is called in f1, _copies_ of f1's a and b are
passed to f2, not f1's a and b. Similarly copies of m and n are passed
to f3, not f2's m and n. This means that any modifications that f2 and
f3 might do to their arguments do not in any way affect their caller's
locals.

If you want a function to directly manipulate it's caller's objects,
you need to pass pointers to those objects. Note that though the
pointers themselves are passed by copying them, since the address they
hold is used, the called function can affect the caller's objects.

void f1(void) {
float x = 1.414, y = 18.83;
float *p = &y;

f2(&x, p);
}

Here f2 will recieve pointer values to f1's x and y, and hence, can
directly modify them.
 
C

Chris Dollin

Scott said:
Now this one does not make any sense to me at all. I have a while
loop and it is not working anything like it is suppose to. I have
placed it below.

-------------------------
Count = 1;

while(count < 4)
{
if(count == 1) rating2 = security;
if(count == 2) rating2 = health;
(fx:snip)

if (count = 1) fhealth = fhealth + bonus;
if (count = 2) feconomy = feconomy + bonus;

I expect you didn't mean to /assign/ to `count` in those conditions.

--
Is it a bird? It is a plane? No, it's: http://hpl.hp.com/conferences/juc2007/
"He could not weigh up which was worse and so tried not to think about either."
/The Spellgrinder's Apprentice/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
 
D

Dave Hansen

No, simply get in the habit of writing the constant first, as in:

if (3 == count) ...

and the compiler will object if you have a typo in the '=='. Now
prepare for the onslaught of the "It's ugly" "It's unnatural" etc.
crowd.

Well, it _is_ unnatural, and it harms readability, but that's not my
worst objection.

1) There's no enforcement. If you remember to do it, you'll probably
do it right anyway, and if you forget, well, you forgot.

2) It doesn't help you at all in a large fraction of cases, e.g., when
comparing two variables.

And suggesting that it's _any_ sort of substitute for lint is
malpractice, pure and simple. Shame.

Regards,

-=Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top