Resistor Calculator - C - Complete n00b

Click For Summary
SUMMARY

The discussion centers on a C programming assignment involving a resistor calculator that requires user input for resistance values and the selection of series resistors based on key presses. The user is struggling with implementing a loop that responds to up and down arrow key presses to exit the loop. Key functions discussed include kbhit() and getch(), which are essential for detecting key presses. The user is advised to check terminal settings and instrument their code with printf() statements for debugging.

PREREQUISITES
  • Understanding of C programming, specifically loops and conditionals
  • Familiarity with keyboard input handling in C using kbhit() and getch()
  • Basic knowledge of resistor values and series resistor calculations
  • Experience with debugging techniques in C, such as using printf() for tracing
NEXT STEPS
  • Research the use of kbhit() and getch() for keyboard input in C programming
  • Learn about handling special key codes in C, particularly for arrow keys
  • Explore debugging techniques in C, focusing on effective use of printf() for tracing program execution
  • Study resistor value calculations and how to implement them programmatically
USEFUL FOR

Electrical engineering students, C programmers, and anyone developing console applications that require real-time user input handling.

xortan
Messages
76
Reaction score
1
Hi all,

I am studying to be an electrical engineer and I have a C programming course that all of a sudden took a massive jump..last week we were writing single For statements and things of that manner and now he assigned a program that's just got my completely stumped, I thought I was getting this stuff until now.

My problem is that the first part of the flow chart he gave out wants to keep looping until an up arrow key is pressed or a down. My loop just seems to keep going tho. Oh and the problem assignment is that the user enters in a resistor value and chooses if they want the closest 10% series resistor greater than or less than the value they entered.

Here is what I've got so far.

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

int main(void)
{
int iFirstKeyCode,iSecondKeyCode;
int iFlag = 1;
int iVal;

printf("This program determines the closest 10% PV to a resistance value entered by the user\n");

printf("Enter an integer value for resistance in ohms");
scanf("%d",&iVal);

printf("Press Up Arrow key to indicate GREATER THAN or Down Arrow key to indicate LESS THAN:\n");


while(iFlag ==1)
{
printf("PV should be greater than or less than %d?",iVal);
if(kbhit())
{

iFirstKeyCode = getch();
if(iFirstKeyCode == 224)
{
iSecondKeyCode = getch();
if (iSecondKeyCode == 72 || iSecondKeyCode == 80)
{
iFlag = 0;
}
}
}// end if
} // end while



system("PAUSE");
return 0;
} // end main

After I know what key is pressed I have a few ideas of how to reduce the value entered to its to most sig figs while keeping track of the multiplier and from that determining the desired value...any insight would be much appreicated!
 
Computer science news on Phys.org
ohhhh...it's been a while here...but a couple possibilities come to mind.

First your "terminal" may not be set to return individual chars without a newline being typed. Look at the man page for getch() on whatever system you are using to see if that's the case, or you could just try typing <ENTER> after your arrow key.

Second you may not be getting the values you think. I don't know what code to expect from an arrow key so I can't check...but would be a bit surprised to get two chars rather than just one with the top bit set.

In either case, instrumenting your code is the way to proceed. printf() printf() printf() everywhere to trace operation and the values you get.

Good luck with that C thing...
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
6
Views
6K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
7K
  • · Replies 14 ·
Replies
14
Views
4K
Replies
1
Views
4K
  • · Replies 19 ·
Replies
19
Views
3K