Simple output problem

R

RadiationX

I have to solve the following problem:Write a program that accepts two
integers, and determines if the second is a factor (is evenly divisible
into) the first. Here is the code i have so far.


#include <stdio.h>
#include <stdlib.h>

int main()
{
int a; //dividend
int b; //divisor
int c;
c==0;

printf("<------The Factoring Machine------->\n");
printf("\nPlease enter an integer followed by\n");
printf("a space and then the possible factor integer.\n");
printf("For exampe: [a b] is the correct format.\n");
scanf("%d%d", &a, &b);
c == a/b;


if(a%b==0)
{
printf("\n %d is a factor of %d\n", b, a);
printf("\nThe other factor is %d\n", c);

}

else
{
printf("\n%d is not a factor of %d\n",b,a);
}
system("PAUSE");
return 0;
}

why is the %d in the second printf not outputting the value of variable
c?
This should be the other factor.
 
X

xyombie

The problem is in the line that reads "c == a/b;". Using two equals is
a comparison (boolean condition). It's asking "is c equal to a/b?",
returning either a zero or one for true or false, but doesn't save this
value anywhere.

What you probably meant to do is "c = (int) a/b;" which will take the
value of a/b and place it into the variable c. Also, because a/b is
returning a double, it's probably a good idea to tell the compiler to
cast it back to an integer because c is an integer. Without the (int),
you might get a compiler warning depending on the compiler.

Good luck.
 
X

xyombie

I also just reallized, you have the same problem in the line "c==0; ".
There is no need to initialize c, but if you wanted to, you would use
"c=0;".

As a reminder, double equal "==" is for comparison, while single "=" is
for setting a variable equal to another variable or equation. When you
mix this up, it can be a difficult bug to find because the compiler
isn't going to complain because it is valid C syntax.
 
V

Vladimir S. Oka

xyombie said:
RadiationX said:
I have to solve the following problem:Write a program that accepts
two integers, and determines if the second is a factor (is evenly
divisible into) the first. Here is the code i have so far.


#include <stdio.h>
#include <stdlib.h>

int main()
{
int a; //dividend
int b; //divisor
int c;
c==0;

printf("<------The Factoring Machine------->\n");
printf("\nPlease enter an integer followed by\n");
printf("a space and then the possible factor integer.\n");
printf("For exampe: [a b] is the correct format.\n");
scanf("%d%d", &a, &b);
c == a/b;


if(a%b==0)
{
printf("\n %d is a factor of %d\n", b, a);
printf("\nThe other factor is %d\n", c);

}

else
{
printf("\n%d is not a factor of %d\n",b,a);
}
system("PAUSE");
return 0;
}

why is the %d in the second printf not outputting the value of
variable c?
This should be the other factor.

Please don't top post. I corrected it this time...
The problem is in the line that reads "c == a/b;". Using two equals
is a comparison (boolean condition). It's asking "is c equal to
a/b?", returning either a zero or one for true or false, but doesn't
save this value anywhere.

You probably meant the correct thing, but your expression of it may be
confusing (see the order of true and false vis order of zero and one in
your sentence). A logical operation in C yields 1 if condition is true,
and 0 if it is false -- not the other way around, as your post implies.
What you probably meant to do is "c = (int) a/b;" which will take the
value of a/b and place it into the variable c. Also, because a/b is
returning a double, it's probably a good idea to tell the compiler to

`a/b` is _not_ returning a double. It returns an int, after
performing /integer/ division (any fractional part is discarded).
cast it back to an integer because c is an integer. Without the
(int), you might get a compiler warning depending on the compiler.

There's no need for a cast here. All variables are of the type int, as
is the result of the division. What _is_ advisable though is to check
for division by zero (obviously before it is attempted), and do
something sensible if it's detected, e.g.:

if (0 == b)
{
fprintf(stderr,"Division by zero attempted!\n");
return EXIT_FAILURE;
}

Or ask user to try and input a legal value for b.

Cheers

Vladimir
 
A

Arndt Jonasson

xyombie said:
I also just reallized, you have the same problem in the line "c==0; ".
There is no need to initialize c, but if you wanted to, you would use
"c=0;".

As a reminder, double equal "==" is for comparison, while single "=" is
for setting a variable equal to another variable or equation. When you
mix this up, it can be a difficult bug to find because the compiler
isn't going to complain because it is valid C syntax.

If you tell it to, it will:

% cat divv.c
int main()
{
int a,b,c;
a = b = c = 1;
c == a/b;
return 0;
}
% gcc -W divv.c
divv.c: In function `main':
divv.c:5: warning: statement with no effect
 
M

Mike Wahler

RadiationX said:
I have to solve the following problem:Write a program that accepts two
integers, and determines if the second is a factor (is evenly divisible
into) the first. Here is the code i have so far.


#include <stdio.h>
#include <stdlib.h>

int main()
{
int a; //dividend
int b; //divisor
int c;
c==0;

c = 0;

or simply replace above two lines with:

int c = 0;

Look up the difference between = and == (they're
two distinct operators with different meanings.

printf("<------The Factoring Machine------->\n");
printf("\nPlease enter an integer followed by\n");
printf("a space and then the possible factor integer.\n");
printf("For exampe: [a b] is the correct format.\n");
scanf("%d%d", &a, &b);

Be very careful with scanf(). See the FAQ for details.
c == a/b;

Again, wrong operator.

c = a / b;
if(a%b==0)
{
printf("\n %d is a factor of %d\n", b, a);
printf("\nThe other factor is %d\n", c);

}

else
{
printf("\n%d is not a factor of %d\n",b,a);
}
system("PAUSE");
return 0;
}

why is the %d in the second printf not outputting the value of variable
c?

Actually it was. As your code is written, the only possible values
for 'c' were zero and one.
This should be the other factor.

-Mike
 
F

Flash Gordon

Make that isn't required to. Some will.
If you tell it to, it will:

<snip>

No, it's not that it will, only that some do. There is no requirement to
produce a diagnostic since as xyombie says it is perfectly valid, and
I'm sure there are compilers that won't warn you.
 
N

Nelu

xyombie said:
The problem is in the line that reads "c == a/b;". Using two equals is
a comparison (boolean condition). It's asking "is c equal to a/b?",
returning either a zero or one for true or false, but doesn't save this
value anywhere.

What you probably meant to do is "c = (int) a/b;" which will take the
value of a/b and place it into the variable c. Also, because a/b is
returning a double, it's probably a good idea to tell the compiler to
cast it back to an integer because c is an integer. Without the (int),
you might get a compiler warning depending on the compiler.
Please, don't top post.
 
N

Nelu

xyombie said:
I also just reallized, you have the same problem in the line "c==0; ".
There is no need to initialize c, but if you wanted to, you would use
"c=0;".

As a reminder, double equal "==" is for comparison, while single "=" is
for setting a variable equal to another variable or equation. When you
mix this up, it can be a difficult bug to find because the compiler
isn't going to complain because it is valid C syntax.
(Don't top post from the previous message)
Please quote some context, even if it's your post.
 
N

Nelu

RadiationX said:
that fixed it. thanks..
That fixed what? Thanks for what?
I'm glad it got fixed and that you are thanking someone.
Please let us know what you're talking about.
QUOTE SOME CONTEXT.
 
A

Arndt Jonasson

Flash Gordon said:
<snip>

No, it's not that it will, only that some do. There is no requirement
to produce a diagnostic since as xyombie says it is perfectly valid,
and I'm sure there are compilers that won't warn you.

To be completely clear, I ought to have written, "if there is a way to
tell the compiler, then if you tell it to, it will." - I suppose I
made the impression of saying that every compiler works like 'gcc'.
 
D

Dave Thompson

xyombie wrote:

`a/b` is _not_ returning a double. It returns an int, after
performing /integer/ division (any fractional part is discarded).
Right. And even if a and b were floating-point, (int) a / b would
convert a to int (if in range, else Undefined Behavior), then convert
back to floating-point because of the usual arithmetic conversions for
computational operators, do the division in floating-point giving a
floating-point result, then implicitly convert to int (again if in
range and assuming c is int).

To explicitly force an integer result from flt-pt; c = (int) (a/b);

To explicitly do integer division: c = (int)a / (int)b;

(Or similarly for long, long long, unsigned, etc.)

- David.Thompson1 at worldnet.att.net
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top