Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am new to programming and am having an issue with my int function. The hw calls for us to print the total resistance for three resistors in Parallel using the function 1 / 1 / R1 + 1 / R2 + 1 / R3 and I noticed that my printf was not showing with the desired result. Using a breakpoint, I discovered my int for the total_resistance was reading as -858993460 instead of the desired 1 / R1, with whatever I chose R1 to be.

Heres my code:
printf("Enter the resistance for 1 resistor: ");
int R1 = 0;
scanf("%d%", &R1);
int total_resistance_denominator = 1 / R1;
int total_resistance = 1 / total_resistance_denominator;

printf("The total resistance of these parallel resistors is %d \n\n", total_resistance);

And above this code I have #define _CRT_SECURE_NO_WARNINGS, and #include <stdio.h>.

What I have tried:

I set up two integers, 1 being the denominator so its not one long equation, and the other being 1 / total_resistance_denominator, to split the equation into two separate functions. Then, I changed it so I am only inputting the resistance of 1 resistor. Regardless, the function that calculates 1 / R1 still results as -858993460, and the following print function does not appear.
Posted
Updated 31-Jan-21 23:34pm
Comments
Peter_in_2780 30-Jan-21 21:23pm    
First thought from the land of the bleeding obvious... Integers are not what you want for this kind of arithmetic. Doubles would be far more useful.
Ron Yehuda 1-Feb-21 2:37am    
I agree! Using int was a requirement for R1 from my teacher.

Quote:
C int function is returning -858993460 instead of desired result

Computer programming is the art of details. That is why reading documentation matters.

A division of integers is an integer division
C++
int total_resistance_denominator = 1 / R1; // Result is 0 unless R1 is 1
int total_resistance = 1 / total_resistance_denominator; // and the division is 1/0 the result is an overflow

As suggested in Peter's comment you need to use floating point datatype like double
[Update]
There is a tool of choice to troubleshoot your code, it is the debugger.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v3
Comments
Ron Yehuda 1-Feb-21 2:36am    
I noticed that it kept saying an error because it was computing 1/0 even when I had R1 as a value other than 1. Why is that the case though? If R1 is 2 why would it be 1/0 rather than 1/2?

In regards to using double over int, I completely agree and I would think to do so as well. Using int for R1 is a requirement for the assignment given by the teacher, I'm assuming for us to troubleshoot the problem I am currently having with my code.
Ron Yehuda 1-Feb-21 2:38am    
I'm realizing now, R1 (R2 and R3 which aren't in this code quite yet) need to be int but nothing is said about the total_resistance itself. Would it change anything if I set the that to be a double value?
Patrice T 1-Feb-21 2:54am    
Advice: Learn by yourself, try things, experiment.
Learning is a trial and error process.
Ron Yehuda 1-Feb-21 14:21pm    
I want to let you know that I figured it out. I set the total_resistance_denominator to be a double rather than an int but it still gave me a 0 for any number of R other than 1. Then, I cast R1 to be a double in the equation for total_resistance_denominator and I finally got it to print the correct number.

So my code now is:
double total_resistance_denominator = 1 / (double)R1
Ron Yehuda 1-Feb-21 14:13pm    
I appreciate that, thank you.
To add to what they others have said: you are a beginner - do not use
C++
#define _CRT_SECURE_NO_WARNINGS
as this tells the compiler to ignore things it thinks you are doing that might give you a problem.
In fact, you should use the appropriate compiler option to "treat all warnings as errors" so your code will not compile at all if the compiler is unsure you know what you are doing, and set the "warning level" to it's highest setting.
Trust me, as a beginner the compiler does know better than you - and you really shouldn't be telling it to ignore anything!
If nothing else, it should mean that your code will not run at all with either of these common beginner problems in it:
C++
if (a = 666)
   {
   ...

C++
if (a == 666);
   {
   ...
Both of which are pretty difficult to spot when in a large lump of code - because most people tend to read what they meant to write, rather than what they did!

But the big question is: why on Earth is a beginner learning C? It's a very, very old language now, and isn't used that much at all. I would expect new programmers to be learning much newer, more friendly languages like C# which don't have the many, many problems that C can frustratingly lead you into.
 
Share this answer
 
Comments
KarstenK 31-Jan-21 3:35am    
C is in common use when you are programming for hardware like embedded devices or chips for smart home or cars. As you see in the task of computing resistance.
OriginalGriff 31-Jan-21 3:54am    
Yes, it is - but that's specialist work, and you have to know exactly what you are doing (I did it for more decades than I want to think about).
It's not a good language for beginners, because pretty much they can't use it for several years until they get the experience they need!
Come to embedded from desktop and it's a fairly easy transition (though you will miss a load of "helper frameworks". Go straight to embedded and it's a world of pain!
Ron Yehuda 1-Feb-21 2:34am    
Honestly I agree with what your saying, C is the language I have to learn for the computer science class I need to take for my Mechanical Engineering major. Unfortunately using the #define _CRT_SECURE_NO_WARNINGS is something the teacher has told us to include in every code as its only been a few weeks of class thus far. He's teaching it as more of a, do what I show you and learn what it does later.
1) Don't panic excessively re: #define _CRT_SECURE_NO_WARNINGS
That's simply there to stop the compiler complaining about the use of old insecure CRT code (printf, scanf). It doesn't affect other warnings.

2) General principle here: Compile at warning level 4 with warnings as errors (/W4 and /WX) on the command line. This will help a lot.

3) Use a modernish compiler. Recent versions of VS (2015 and later) will point out some bugs to you by default:
Your call to scanf is wrong - it's using a malformed directive:
scanf("%d%".

%d% is not valid for scanf. It should be simply %d
VS 2015 and later will point this out to you automatically.
That's at least one bug you can find "for free", without even needing to run the code.

4) If you can, use a static analyser (again, recent versions of VS have it - look into Prefast / Code Analysis), this is like compiler warnings on overload, and is really helpful.

There's more to then do with actually debugging and single stepping your code - but the hints from other guys will help with this.
 
Share this answer
 
Comments
Ron Yehuda 1-Feb-21 14:13pm    
Thank you for the advice I didnt even notice the extra % after %d, it's honestly unfortunate that the newer VS points that out. I'm stuck using this version for the computer science class I am taking.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900