Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.72 KB | None | 0 0
  1.  
  2. #include <iostream> // Needed for I/O.
  3.  
  4. using namespace std; // Needed in every program for cin and cout.
  5. int main ( void )
  6. {
  7. // State to the user what this program does.
  8. cout << "This a program recieves four values as input from the user. These four values "
  9. "are the Initial Voltage, the First Resistance Value, the Second Resistance value, "
  10. "and the Third resistance value. The program uses these values to calculate both the "
  11. "First and Second voltages, which are between the three resistors, and the Roundoff "
  12. "Error which might occur in the end of the program due to limited storage base. In "
  13. "addition to those values, the program also calculates the Total Resistance Value of "
  14. "all the resistors, the Current in amps, the First Voltage Drop, the Second Voltage "
  15. "Drop, and the Third Voltage Drop. "<< endl << endl;
  16.  
  17. // Declare the variables to be used.
  18. char answer ('y');
  19. double intVol (0.0), res1 (0.0), res2 (0.0), res3 (0.0), resTot = 0.0,
  20. current = 0.0, vd1 = 0.0, vd2 = 0.0, vd3 = 0.0, vol1 = 0.0, vol2 = 0.0, rounderr = 0.0;
  21. // Begin main do-while loop
  22. do{
  23. // Get first user input of Initial Voltage.
  24. cout<< "Please input the Initial Voltage in your curcuit that is greater than or equal to 0: ";
  25. cin >> intVol;
  26. cout<< endl << "You entered " << intVol << "." << endl;
  27. // Check to make sure the user entered a number equal to or greater than 0.
  28. while (intVol < 0)
  29. {
  30. cout<< "Please enter a number greater than or equal to 0: ";
  31. cin >> intVol;
  32. cout<< endl << "You entered " << intVol << "." << endl;
  33. if (intVol < 0)
  34. {
  35. cout<< "The number you have entered is less than 0." << endl;
  36. cout<< "Please re-enter the number." << endl;
  37. }
  38. }
  39. // Get second user input of First Resistance Value.
  40. cout<< endl << "Please input the First Resistance Value in your circuit that is greater than or equal to 0: ";
  41. cin >> res1;
  42. cout<< endl << "You entered " << res1 << "." << endl;
  43. // Check to make sure the user entered a number equal to or greater than 0.
  44. while (res1 < 0)
  45. {
  46. cout<< "Please enter a number greater than or equal to 0: ";
  47. cin >> res1;
  48. cout<< endl << "You entered " << res1 << "." << endl;
  49. if (res1 < 0)
  50. {
  51. cout<< "The number you have entered is less than 0." << endl;
  52. cout<< "Please re-enter the number." << endl;
  53. }
  54. }
  55. // Get third user input of Second Resistance Value.
  56. cout<< endl << "Please input the Second Resistance Value in your circuit that is greater than or equal to 0: ";
  57. cin >> res2;
  58. cout<< endl << "You entered " << res2 << "." << endl;
  59. // Check to make sure the user entered a number equal to or greater than 0.
  60. while (res2 < 0){
  61. cout<< "Please enter a number greater than or equal to 0: ";
  62. cin >> res2;
  63. cout<< endl << "You entered " << res2 << "." << endl;
  64. if (res2 < 0)
  65. {
  66. cout<< "The number you have entered is less than 0." << endl;
  67. cout<< "Please re-enter the number." << endl;
  68. }
  69. }
  70. // Get fourth user input of the Third Resistance Value.
  71. cout<< endl << "Please input the Third Resistance Value in your circuit that is greater than or equal to 0: ";
  72. cin >> res3;
  73. cout<< endl << "You entered " << res3 << "." << endl;
  74. // Check to make sure the user entered a number equal to or greater than 0.
  75. while (res3 < 0)
  76. {
  77. cout<< "Please enter a number greater than or equal to 0: ";
  78. cin >> res3;
  79. cout<< endl << "You entered " << res3 << "." << endl;
  80. if (res3 < 0)
  81. {
  82. cout<< "The number you have entered is less than 0." << endl;
  83. cout<< "Please re-enter the number." << endl;
  84. }
  85. }
  86. // Calculate the Total Resistance.
  87. resTot = res1 + res2 + res3;
  88. cout<< endl << "Your total Resistance value is " << resTot << "." << endl;
  89. // Check to make sure the user entered something other other
  90. // than the numbers 0,0,0,0 by making him input an additional
  91. // Resistor number, raising the total resistance if it ends up
  92. // beings 0 (because you can't divide by zero).
  93. while (resTot = 0)
  94. {
  95. cout<< "Please enter a resistance greater than 0: ";
  96. cin >> resTot;
  97. cout<< endl << "You entered " << resTot << "." << endl;
  98. if (resTot = 0)
  99. {
  100. cout<< "The number you have entered is less than 0." << endl;
  101. cout<< "Please re-enter the number." << endl;
  102. }
  103. }
  104. // Calculate the Current in Amps
  105. current = intVol / resTot;
  106. cout << endl << "Your current is " << current << " amps." << endl;
  107. // Calculate the First, Second, and Third Voltage Drops.
  108. vd1 = current * res1;
  109. vd2 = current * res2;
  110. vd3 = current * res3;
  111. cout<< endl << "Your first voltage drop is " << vd1 << endl;
  112. cout<< "Your second voltage drop is " << vd2 << endl;
  113. cout<< "Your third voltage drop is " << vd3 << endl;
  114. // Calculate the First and Second Voltage Values.
  115. vol1 = intVol - vd1;
  116. vol2 = intVol - vd1 - vd2;
  117. cout<< "Your first voltage value between Resistor 1 and Resistor 2 is " << vol1 << endl;
  118. cout<< "Your second voltage value between Resistor 2 and Resistor 3 is " << vol2 << endl;
  119. // Calculate the Roundoff Error.
  120. rounderr = intVol - vd1 - vd2 - vd3;
  121. cout<< "Your roundoff error at the end of the program is " << rounderr << endl << endl;
  122. // Ask the user if he or she wants to repeat the program,
  123. // if user types in "y" then the do-while loop repeats the
  124. // program without the initial cout explaining what the
  125. // program does.
  126. cout<< "Would you like to use the Program again? Answer y or n for Yes or No: ";
  127. cin >> answer;
  128. if (answer='n')
  129. {break;}
  130. }while (answer !='n');
  131. cout << endl << "Thank you for using this program, have a good day." << endl;
  132.  
  133. return 0;
  134. } // main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement