Calculators Resistor Calculator - C - Complete n00b

AI Thread Summary
The discussion centers on a programming challenge faced by an electrical engineering student in a C programming course. The assignment involves creating a program that loops until the user presses either the up or down arrow key, which indicates whether they want a resistor value greater than or less than a specified input. The student is struggling with the loop continuously running without stopping, despite implementing a key detection mechanism using `kbhit()` and `getch()`. Suggestions include checking terminal settings to ensure individual character input is recognized without requiring a newline, and verifying the expected key codes for the arrow keys. The importance of debugging through extensive use of `printf()` statements to trace the program's execution and variable values is emphasized. The student also expresses ideas about processing the resistor value to determine the closest 10% series resistor based on user input.
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...
 
In my discussions elsewhere, I've noticed a lot of disagreement regarding AI. A question that comes up is, "Is AI hype?" Unfortunately, when this question is asked, the one asking, as far as I can tell, may mean one of three things which can lead to lots of confusion. I'll list them out now for clarity. 1. Can AI do everything a human can do and how close are we to that? 2. Are corporations and governments using the promise of AI to gain more power for themselves? 3. Are AI and transhumans...
Sorry if 'Profile Badge' is not the correct term. I have an MS 365 subscription and I've noticed on my Word documents the small circle with my initials in it is sometimes different in colour document to document (it's the circle at the top right of the doc, that, when you hover over it it tells you you're signed in; if you click on it you get a bit more info). Last night I had four docs with a red circle, one with blue. When I closed the blue and opened it again it was red. Today I have 3...

Similar threads

Replies
5
Views
3K
Replies
1
Views
3K
Replies
4
Views
2K
Replies
2
Views
3K
Replies
3
Views
2K
Replies
19
Views
3K
Back
Top