lundi 26 juin 2023

I am trying to create a program that outputs to screen and to file but when outputting to screen I keep getting a (-) number rather than decimal?

I am supposed to create two "functions" and one is to print the total resistance in parallel to the screen. I have tried looking up the difference between using integer division and float division but when dividing the number 1 over the sum of the resistors, it gives me the result of "inf". When changing one of the variables to an integer, the variable gives me a long - number. versus the result I am looking for. The answer I get on a calculator is 0.02316 but when switching the double for the integer I get -2147483648.

#include <iostream>
#include <fstream>
#include <iomanip>
~~~~~~~~~~~~~~~~~~~~~~~~~
using namespace std;
~~~~~~~~~~~~~~~~~~~~~~~
//create vector for resistor
//create variable for resistors
//create variable for equation in parallel
//output string with precision and results
//create variable for equation in series
//output string with precision and results
~~~~~~~~~~~~~~~~~
int main() {
    ifstream in_file;
    double resistor1, resistor2, resistor3, resistor4, resistor5, resistor6;
    double resistors_in_series;
    int resistors_in_parallel;
    
    in_file>> resistor1>>resistor2>>resistor3>>resistor4>>resistor5>>resistor6;
    resistors_in_parallel = 1.0/(resistor1+resistor2+resistor3+resistor4+resistor5+resistor6);
    cout << "The total equivalent resistance is " << fixed <<setprecision(1) << resistors_in_parallel << " ohms if resistors are connected in parallel." << endl;
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    ofstream out_file;
        
    in_file.open("resistors.txt");
    out_file.open("total_resistance.txt");
    
    out_file<<fixed<< setprecision(1);
    
    in_file>> resistor1>>resistor2>>resistor3>>resistor4>>resistor5>>resistor6;
    resistors_in_series = (resistor1+resistor2+resistor3+resistor4+resistor5+resistor6);
    out_file<< "The total equivalent resistance is "<< resistors_in_series << " ohms if resistors are connected in series.";
    
    in_file.close();
    out_file.close();
}
~~~~~~~~~~~~~~~~~~~~~~~

Aucun commentaire:

Enregistrer un commentaire