Resistor Calculator - C - Complete n00b

  • Calculators
  • Thread starter xortan
  • Start date
  • #1
78
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 thats 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 ive 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!
 

Answers and Replies

  • #2
595
0
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...
 

Related Threads on Resistor Calculator - C - Complete n00b

  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
2
Views
2K
  • Last Post
2
Replies
28
Views
11K
Replies
1
Views
1K
Replies
7
Views
9K
Replies
9
Views
2K
  • Last Post
Replies
4
Views
2K
Replies
1
Views
3K
Replies
15
Views
4K
Replies
39
Views
6K
Top