diode problem

write a program following that prompts and inputs from the user the temp in fahrenheit of the diode. output to the console a table with two columns: voltage (V) and current (A). Calculate and output the current for voltages ranging from -.2 to .5 volts in .05 volt increments. set the precision of the values appropriately. that is the mid term here is what i have so far

I am not getting it to output Voltage or Current, it outputs the headers but no values. where did I go wrong in this code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
//Define Variables
const double ILeak = .0000015;
double tempF = 0;
double volt = -.2;
const double q = (1.602 * pow(10,-19));
const double k = (1.32 * pow(10,-23));
double tempK = 0;
double I = 0;
int main(){
	cout << "Input Temperature in Degrees Fahrenheit \n";
	cin >> tempF;
	double tempK = (((5.0/9.0)*(tempF-32))+273.15);
	cout << "Voltage (V) Current(A) \n";
	if (!volt == .5){
		I = ILeak * ( exp( (q*volt)/(k*tempK)) - 1);
		cout << setprecision(2) << volt << " " << setprecision(7) << I << endl;
		volt= volt-.5; }
	else{
		system ("Pause");
		return 0; }
	
1) you have messed up operator prioritis in line 16. It works as
1
2
((!volt) == .5)
// ↑equals either to 1 or 0 depending on value of volt 

2) It is not important in your current code, but if your volt variable will be calculated, never compare floating point values with == or !=
Example of error: http://ideone.com/K1UFPD
this will not be calculated other than to say it goes down by .05 each time
I still can't get it to output the numbers for voltages which should be -.2 -.15 -.1 -.05 etc or the values for current
1) I posted that you have operator priorities here, but forgot to include correct way (excluding float comparsion): if ( !(volt == .5) ){
2) In the link I provided there is an example where I have added 0.1 to a value initializated with 0 ten times. And it does not compares equal to 1. You have a good chance that your condition will never fails.
Last edited on
<code>
//Cody Brown MidTerm Project Diode Current Flow
#include <iomanip>
#include <cmath>
#include <iostream>
using namespace std;
//Define Variables
const double ILeak = .0000015;
double tempF = 0;
double volt = -.2;
const double q = (1.602 * pow(10,-19));
const double k = (1.32 * pow(10,-23));
double tempK = 0;
double I = 0;
int main(){
cout << "Input Temperature in Degrees Fahrenheit \n";
cin >> tempF;
double tempK = (((5.0/9.0)*(tempF-32))+273.15);
cout << "Voltage(V) Current(A) \n";
if (!(volt == .5)){
I = ILeak * ( exp( (q*volt)/(k*tempK)) - 1);
cout << setprecision(2) << volt << " " << setprecision(7) << I << endl;
volt= volt-.5; }
else {
return 1;}
system ("Pause");
return 0;
}
<code>
so my new problem is it only outputs the first value but doesn't output the following values for voltage and current
Well, you didn't tell program to do so. You should wrap part which needs to be repeated in loop (probably replacing if).
I replaced if with for and now its giving me syntax error : missing ';' before ')'
How do you repaced it? It looks that your for loop are missing some of the 3 nessesary parts.
yes I just realized this, im trying to set my for loop conditions
sorry for double post

here is my for

for (volt=-.2;,!(volt == .5);,volt=volt+.05){

its giving me a couple errors having to do with the ;'s
remove commas after your ';'s
now I am getting an infinite loop


//Cody Brown MidTerm Project Diode Current Flow
#include <iomanip>
#include <cmath>
#include <iostream>
using namespace std;
//Define Variables
const double ILeak = .0000015;
double tempF = 0;
double volt = -.2;
const double q = (1.602 * pow(10.0,-19.0));
const double k = (1.32 * pow(10.0,-23.0));
double tempK = 0;
double I = 0;
int main(){
cout << "Input Temperature in Degrees Fahrenheit \n";
cin >> tempF;
double tempK = (((5.0/9.0)*(tempF-32))+273.15);
cout << "Voltage(V) Current(A) \n";
for (volt=-.2;!(volt == .5);volt=volt+.05){
I = ILeak * ( exp( (q*volt)/(k*tempK)) - 1);
cout << setprecision(2) << volt << " " << setprecision(7) << I << endl; }
system ("Pause");
return 0;
}
Well, congratulations, you have run into floating point comparsion problem I warned you about before.

Now look into mostly correct way to do comparsion:
1
2
3
4
5
6
7
8
9
10
11
12
#include <limits>
#include <cmath>

bool almost_equal(double x, double y, int ulp = 4)
{
    return std::abs(x-y) <=   std::numeric_limits<double>::epsilon()
                            * std::max(std::abs(x), std::abs(y))
                            * ulp;
}

//...
for (volt = -.2; !almost_equal(volt, .5); volt += .05){//... 
Last edited on
Topic archived. No new replies allowed.