Guest User

Untitled

a guest
Mar 8th, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1.  
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <ctype.h>
  5. #include <cmath>
  6. #include <string>
  7. #include <iomanip>
  8.  
  9. using namespace std;
  10.  
  11. double GetValidDouble(string prompt = "",const double MIN = -90, const double MAX = 60);
  12. double TempConvert(double input);
  13. void Output(double INPUT[],double CONVERTED_INPUT[],const string WEEKDAYS[]);
  14.  
  15. int main ()
  16. {
  17. const string WEEKDAYS[] = { "Monday", "Tuesday", "Wednesday", "Thursday",
  18. "Friday", "Saturday", "Sunday"};
  19. double CONVERTED_INPUT[6];
  20. double INPUT[6];
  21. double validInput = 0.0;
  22.  
  23. cout << "TEMPERATURE REPORTER"<< endl <<"=================="<< endl;
  24. for(int counter = 0;counter <7;counter ++)
  25. {
  26. validInput = GetValidDouble("Please Enter Temperature for " + WEEKDAYS[counter]+":");
  27. INPUT[counter] = validInput;
  28. CONVERTED_INPUT[counter] = TempConvert(INPUT[counter]);
  29. }
  30. system("cls");
  31. Output(INPUT,CONVERTED_INPUT,WEEKDAYS);
  32. system("pause");
  33. }
  34.  
  35. void Output(double INPUT[],double CONVERTED_INPUT[],const string WEEKDAYS[])
  36. {
  37. cout << "THIS WEEK'S TEMPERATURE REPORT" << endl << "==============================" << endl << endl;
  38. for(int counter = 0; counter <7;counter++)
  39. {
  40. cout << right << setw(10) << WEEKDAYS[counter] << " " << right << setw(10) << CONVERTED_INPUT[counter] <<"\370F" << " " << right << setw(10) << INPUT[counter]<< "\370C"<<endl;
  41. }
  42. }
  43.  
  44. double TempConvert(double input)
  45. {
  46. double convertedInput = 0;
  47. convertedInput = input * 9 / 5 + 32;
  48. return convertedInput;
  49. }
  50. double GetValidDouble(string prompt,const double MIN, const double MAX)
  51. {
  52. double validNumber = 0.0; // holds the user input
  53. string rubbish; // holds garbage input.
  54.  
  55. cout << endl <<prompt << " ";
  56. cin >> validNumber; // try to get input
  57. if(cin.fail()) // if user input fails...
  58. {
  59. cin.clear(); // reset the cin object
  60. cin >> rubbish; // cleans garbage from cin.
  61.  
  62. // report the problem to the user.
  63. cerr << "\nInvalid input. Please try again and enter a numeric value.\n";
  64. // Try again by calling the function again (recursion)
  65. validNumber = GetValidDouble(prompt);
  66. }
  67. else if(validNumber < MIN || validNumber > MAX)// if value is outside range...
  68. {
  69. // report the problem to the user.
  70. cerr << "\nInvalid input. Please try again and enter a value between "
  71. << MIN << " and " << MAX << ".\n";
  72. // Try again by call the function again (recursion)
  73. validNumber = GetValidDouble(prompt);
  74. }
  75. return validNumber; // returns a valid value to the calling function.
  76. }
Advertisement
Add Comment
Please, Sign In to add comment