Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream> // Needed for I/O.
- using namespace std; // Needed in every program for cin and cout.
- int main ( void )
- {
- // State to the user what this program does.
- cout << "This a program recieves four values as input from the user. These four values "
- "are the Initial Voltage, the First Resistance Value, the Second Resistance value, "
- "and the Third resistance value. The program uses these values to calculate both the "
- "First and Second voltages, which are between the three resistors, and the Roundoff "
- "Error which might occur in the end of the program due to limited storage base. In "
- "addition to those values, the program also calculates the Total Resistance Value of "
- "all the resistors, the Current in amps, the First Voltage Drop, the Second Voltage "
- "Drop, and the Third Voltage Drop. "<< endl << endl;
- // Declare the variables to be used.
- char answer ('y');
- double intVol (0.0), res1 (0.0), res2 (0.0), res3 (0.0), resTot = 0.0,
- current = 0.0, vd1 = 0.0, vd2 = 0.0, vd3 = 0.0, vol1 = 0.0, vol2 = 0.0, rounderr = 0.0;
- // Begin main do-while loop
- do{
- // Get first user input of Initial Voltage.
- cout<< "Please input the Initial Voltage in your curcuit that is greater than or equal to 0: ";
- cin >> intVol;
- cout<< endl << "You entered " << intVol << "." << endl;
- // Check to make sure the user entered a number equal to or greater than 0.
- while (intVol < 0)
- {
- cout<< "Please enter a number greater than or equal to 0: ";
- cin >> intVol;
- cout<< endl << "You entered " << intVol << "." << endl;
- if (intVol < 0)
- {
- cout<< "The number you have entered is less than 0." << endl;
- cout<< "Please re-enter the number." << endl;
- }
- }
- // Get second user input of First Resistance Value.
- cout<< endl << "Please input the First Resistance Value in your circuit that is greater than or equal to 0: ";
- cin >> res1;
- cout<< endl << "You entered " << res1 << "." << endl;
- // Check to make sure the user entered a number equal to or greater than 0.
- while (res1 < 0)
- {
- cout<< "Please enter a number greater than or equal to 0: ";
- cin >> res1;
- cout<< endl << "You entered " << res1 << "." << endl;
- if (res1 < 0)
- {
- cout<< "The number you have entered is less than 0." << endl;
- cout<< "Please re-enter the number." << endl;
- }
- }
- // Get third user input of Second Resistance Value.
- cout<< endl << "Please input the Second Resistance Value in your circuit that is greater than or equal to 0: ";
- cin >> res2;
- cout<< endl << "You entered " << res2 << "." << endl;
- // Check to make sure the user entered a number equal to or greater than 0.
- while (res2 < 0){
- cout<< "Please enter a number greater than or equal to 0: ";
- cin >> res2;
- cout<< endl << "You entered " << res2 << "." << endl;
- if (res2 < 0)
- {
- cout<< "The number you have entered is less than 0." << endl;
- cout<< "Please re-enter the number." << endl;
- }
- }
- // Get fourth user input of the Third Resistance Value.
- cout<< endl << "Please input the Third Resistance Value in your circuit that is greater than or equal to 0: ";
- cin >> res3;
- cout<< endl << "You entered " << res3 << "." << endl;
- // Check to make sure the user entered a number equal to or greater than 0.
- while (res3 < 0)
- {
- cout<< "Please enter a number greater than or equal to 0: ";
- cin >> res3;
- cout<< endl << "You entered " << res3 << "." << endl;
- if (res3 < 0)
- {
- cout<< "The number you have entered is less than 0." << endl;
- cout<< "Please re-enter the number." << endl;
- }
- }
- // Calculate the Total Resistance.
- resTot = res1 + res2 + res3;
- cout<< endl << "Your total Resistance value is " << resTot << "." << endl;
- // Check to make sure the user entered something other other
- // than the numbers 0,0,0,0 by making him input an additional
- // Resistor number, raising the total resistance if it ends up
- // beings 0 (because you can't divide by zero).
- while (resTot = 0)
- {
- cout<< "Please enter a resistance greater than 0: ";
- cin >> resTot;
- cout<< endl << "You entered " << resTot << "." << endl;
- if (resTot = 0)
- {
- cout<< "The number you have entered is less than 0." << endl;
- cout<< "Please re-enter the number." << endl;
- }
- }
- // Calculate the Current in Amps
- current = intVol / resTot;
- cout << endl << "Your current is " << current << " amps." << endl;
- // Calculate the First, Second, and Third Voltage Drops.
- vd1 = current * res1;
- vd2 = current * res2;
- vd3 = current * res3;
- cout<< endl << "Your first voltage drop is " << vd1 << endl;
- cout<< "Your second voltage drop is " << vd2 << endl;
- cout<< "Your third voltage drop is " << vd3 << endl;
- // Calculate the First and Second Voltage Values.
- vol1 = intVol - vd1;
- vol2 = intVol - vd1 - vd2;
- cout<< "Your first voltage value between Resistor 1 and Resistor 2 is " << vol1 << endl;
- cout<< "Your second voltage value between Resistor 2 and Resistor 3 is " << vol2 << endl;
- // Calculate the Roundoff Error.
- rounderr = intVol - vd1 - vd2 - vd3;
- cout<< "Your roundoff error at the end of the program is " << rounderr << endl << endl;
- // Ask the user if he or she wants to repeat the program,
- // if user types in "y" then the do-while loop repeats the
- // program without the initial cout explaining what the
- // program does.
- cout<< "Would you like to use the Program again? Answer y or n for Yes or No: ";
- cin >> answer;
- if (answer='n')
- {break;}
- }while (answer !='n');
- cout << endl << "Thank you for using this program, have a good day." << endl;
- return 0;
- } // main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement