Skip to content

Instantly share code, notes, and snippets.

@snorrewb
Created September 5, 2012 14:48
Show Gist options
  • Save snorrewb/3637745 to your computer and use it in GitHub Desktop.
Save snorrewb/3637745 to your computer and use it in GitHub Desktop.
IMT1031 Grunnleggende Programmering - Obligatorisk oppgave 2
#include <iostream>
using namespace std;
int main () {
//declare variables
const int maxTemp = 70; //maximum allowed temperature
const int minTemp = -70; //minimum allowed temperature
const int maxRain = 200; //maximum allowed downpour
const int minRain = 0; //minimum allowed downpour
char again = 'Y'; //variable for knowing when to shut down
while (again == 'Y') { // keeps going until we ask it to stop
//declare variables
int inInt; //our variable for temporarily storing numbers from input
int highTemp = 0; //variables for temporarily keeping track of temperature
int lowTemp = 0;
int totalHighTemp = 0; //variables for storing the total temperature for calculations
int totalLowTemp = 0;
float averageHighTemp = 0; //variables for storing the average temperature for calculations
float averageLowTemp = 0;
int rain = 0; //variable for keeping track of downpour
int totalRain = 0; //variable for storing total downpour
float averageRain = 0; //variable for storing average downpour
int day = 1; //variable for keeping track of progress
int monthLength = 0; //variable for storing the length of the month
while (monthLength == 0) { //sets the length of the month
cout << "\nHow many days are there in the month? (28-31) ";
cin >> inInt;
if (inInt >= 28 && inInt <= 31) { monthLength = inInt; }
else { cout << "\nIncorrect, please type number from 28-31"; }
}
while (day <= monthLength) { //this loop runs once for every day of the month
bool check = false; //boolean variable for getting our of completed while loops
while (check != true) { //here we get the lowest temperature of the day
cout << "\nWhat was the lowest temperature for day " << day << "? (" << minTemp << "-" << maxTemp << ") ";
cin >> inInt;
if (inInt >= minTemp && inInt <= maxTemp) { lowTemp = inInt; totalLowTemp += lowTemp; check = true; }
else { cout << "\nIncorrect, please type number from " << minTemp << "-" << maxTemp; }
}
check = false;
while (check != true) { //here we get the highest temperature of the day
cout << "\nWhat was the highest temperature for day " << day << "? (" << lowTemp << "-" << maxTemp << ") ";
cin >> inInt;
if (inInt >= lowTemp && inInt <= maxTemp) { highTemp = inInt; totalHighTemp += highTemp; check = true; }
else { cout << "\nIncorrect, please type number from " << lowTemp << "-" << maxTemp; }
}
check = false;
while (check != true) { //here we get the total downpour for the day
cout << "\nWhat was total downpour for day " << day << "? (" << minRain << "-" << maxRain << ") ";
cin >> inInt;
if (inInt >= minRain && inInt <= maxRain) { rain = inInt; totalRain += rain; check = true; }
else { cout << "\nIncorrect, please type number from " << minRain << "-" << maxRain; }
}
day++; //add a day before running the loop again
}
//calculations for averages
averageLowTemp = float (totalLowTemp)/float (monthLength);
averageHighTemp = float (totalHighTemp)/float (monthLength);
averageRain = float (totalRain)/float (monthLength);
//output of calculated values
cout << "\n\nThe average lowest temperature over the " << monthLength << " days was: " << averageLowTemp;
cout << "\nThe average highest temperature over the " << monthLength << " days was: " << averageHighTemp;
cout << "\n\nThe average downpour over the " << monthLength << " days was: " << averageRain;
cout << "\nThe total downpour over the " << monthLength << " days was: " << totalRain;
//ask user whether or not to re-run the program
cout << "\n\nDo you wish to calculate another month? (Y/N)";
cin >> again;
again = toupper(again);
}
cout << "\n\nThank you for you time, have a splendid day!";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment