fork download
  1. //Xinnuo Wang CS1A chapter 4, p.224 #19
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * Caculate meters the sound traveled
  6.  * _____________________________________________________________________________
  7.  * This program accepts the seconds of the sound traveled and caculate the
  8.  * meters.
  9.  *
  10.  * Computation is based on the formula:
  11.  * meters = speed * seconds
  12.  * _____________________________________________________________________________
  13.  * INPUT
  14.  * Choice : The choice you made to decide a type of gase.
  15.  * Seconds : The seconds the sound traveled
  16.  * OUTPUT
  17.  * Meters : The meters the sound traveled
  18.  *
  19.  ******************************************************************************/
  20. #include <iostream>
  21. using namespace std;
  22.  
  23. int main()
  24. {
  25. int choice; // INPUT - The choice you made to decide a type of gase.
  26. float seconds; //INPUT - The seconds the sound traveled
  27. float meters; //OUTPUT - The meters the sound traveled
  28. cout <<"===== MENU ====="<< endl;
  29. cout <<"1.Carbon Dioxide"<< endl;
  30. cout <<"2.Air"<< endl;
  31. cout <<"3.Helium"<< endl;
  32. cout <<"4.Hydrogen"<< endl;
  33. //
  34. //Data Input
  35. cout<<"Enter a number"<< endl;
  36. cin >> choice;
  37. cout << "Enter the seconds"<<endl;
  38. cin>> seconds;
  39. //
  40. //Check
  41. if (seconds<0 || seconds > 30)
  42. {
  43. cout << "Invalid time input" << endl;
  44. return 0;
  45. }
  46. //Output
  47. switch(choice)
  48. {
  49. case 1:
  50. {
  51. meters= seconds * 258;
  52. cout<< "The sound traveled " << meters <<" meters."<<endl;break;
  53. }
  54. case 2:
  55. {
  56. meters= seconds * 331.5;
  57. cout<< "The sound traveled " << meters <<" meters."<<endl;break;
  58. }
  59. case 3:
  60. {
  61. meters= seconds * 972.0;
  62. cout<< "The sound traveled " << meters <<" meters."<<endl;break;
  63. }
  64. case 4:
  65. {
  66. meters= seconds * 1270.0;
  67. cout<< "The sound traveled " << meters <<" meters."<<endl;break;
  68. }
  69. default:
  70. cout <<"Invalid choice"<< endl;
  71. }
  72. return 0;
  73. }
Success #stdin #stdout 0.01s 5320KB
stdin
2 34
stdout
===== MENU =====
1.Carbon Dioxide
2.Air
3.Helium
4.Hydrogen
Enter a number
Enter the seconds
Invalid time input