comparing values - c program

macuni

Registered
Hello

After asking a question previous, ive spent hours trying to complete my program. Im having problems trying to check a value against several values stored in two arrays. Ive managed to load two .txt files into arrays. I ask the user for a voltage input followed by four resistor values and their tolerance. The resistor values and their tolerance needs to be confirmed against realistic values within the .txt files.

Ive been working around while(value!=RVALS[idx]) but also tried many other ways, no matter what I enter it never manages to match 'value' with any of 'RVALS' elements.

Below is the working code(I do get 'inf' sometimes for the 'ans' result but havnt looked at that just yet) without the value comparing,Ive replaced it with *********as it was a bit of a mess.

Any help is much appreciated.


#include <stdio.h>
#include <string.h>
#include <iostream>
#define resistors 4
#define real 4

float voltage,ans;
int idx,i;
char input [4];
void circuittest();

int main ()

{
memset( input, '\0', 4 );
do{
circuittest();
printf("would you like to retest yes or no?\n");
scanf("%s",&input);
}while ( strcmp( input, "yes" ) == 0 );
fflush(stdin);
return 0;
}




void circuittest()
{
int RVALS[real],R[resistors],i,usertol[resistors];
float tol[9];


FILE *fp;
fp=fopen("//Users//nealbee//Documents//UNI//resistors.txt", "r");

for(idx=0; idx<real; idx++)
{
fscanf(fp,"%d", &RVALS[idx]) ;
}

FILE *fp2;
fp2=fopen("//Users//nealbee//Documents//UNI//tolerances.txt","r");




for(idx=0; idx<9; idx++)
{
fscanf(fp2,"%d", &tol[idx]);
}


printf("Please enter a voltage");
scanf("%f", &voltage);


for(i = 0; i<resistors; i++)

{ float value;
float tolvalue;


printf("Enter resistor %d value and its tolerance ",i+1);
scanf("%f %f", &value, &tolvalue);
R=value;
usertol=tolvalue;

******************

}

ans = voltage/(((R[0]*R[1])/(R[0]+R[1]))/((R[2]*R[3])/(R[2]+R[3])));
printf("Output voltage=%f\n",ans);

}
 
Your RVALS array is an array of ints, specified here:
Code:
int RVALS[real],R[resistors],i,usertol[resistors];

If you want to match something in that array to your "value" variable, then your value variable must also be of type int (or must be cast to an int). Is it? It doesn't look like it, because you declare the variable "value" here as a float (!):

Code:
[b]float value;[/b]
float tolvalue;


printf("Enter resistor %d value and its tolerance ",i+1);
scanf("%f %f", &value, &tolvalue);
R[i]=value;
usertol[i]=tolvalue;

You then go on to do this:
Code:
R[i]=value;
Your "R" array is an array of integers, and your "value" variable is a float. You're sticking a float value into an int container... bad, bad, bad.

Comparing a float to an int will never (er, rarely) evaluate to "true."
 
Back
Top