Search code examples
c++reset

How to reset all variables in C++?


My code seems to be functioning without any problems,
but our professor warned us that this task might require
resetting of variables in order to rerun cleanly after first run.

Problem is I have no idea how to reset variables and
I assume it becomes essential with more complex code. Help?

This weather task is basically "ask the user which month it is,
then ask for min.temperature, max.temp. & rainfall for each day of this month,
then print monthly averages and total rainfall.

// Note to Stackoverflowers: Dager = days & Nedbor = rainfall.
// Other then that everything below is translated to english just for this
// question
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
  char rerun = 'j';  // rerun program variable
  while (rerun == 'j' || rerun == 'J')  // rerun program
  {
    int antDager, antD;  // number of days
    int minTemp;
    float minTempAdd = 0;  // min.temp.
    int maxTemp;
    float maxTempAdd = minTempAdd;  // max.temp
    int mmNedbor;
    float mmNedborAdd = 0;  // rain/downfall

    do {
      cout << "\n\tHow many days in this month? (28 til 31)\n";  //#days?
      cin >> antDager;
      if (31 < antDager || 28 > antDager)  // must be 28-31
      {
        cout << "\n\tInvalid value\n";
      }
    } while (31 < antDager || 28 > antDager);  // loop if invalid value

    for (antD = 1; antD < antDager;
         antD++,  // for loop the month
         minTempAdd += minTemp, maxTempAdd += maxTemp,
        mmNedborAdd +=
         mmNedbor) {  // the above increments temperatures and rain with itself
      do {
        cout << "\n\tminTemp? (-70 til 70), max.temp? (min til 70),   
            rain in mm
            ? (0 til 200)\n ";      //sentence break for this website
                  cin >>
                  minTemp >> maxTemp >>
                  mmNedbor;  // temperatures and rain input
        if ((-70) > minTemp || 70 < minTemp) {
          cout << "\n\tInvalid min-value\n";
        }
        if (minTemp > maxTemp || 70 < maxTemp) {
          cout << "\n\tInvalid max-value\n";
        }
        if (0 > mmNedbor || 200 < mmNedbor) {
          cout << "\n\tInvalid Nedbor-value\n";
        }
      } while ((((-70) > minTemp || 70 < minTemp) ||
                (minTemp > maxTemp || 70 < maxTemp)) ||
               (0 > mmNedbor || 200 < mmNedbor));
    }

    cout << "\n\tGjennomsnittlig minTemp: " << minTempAdd / antDager;
    cout << "\n\tGjennomsnittlig minTemp: " << maxTempAdd / antDager;
    cout << "\n\tGjennomsnittlig nedbor: " << mmNedborAdd / antDager;
    cout << "\n\tTotal nedbor: " << mmNedborAdd << endl;

    cout << "\n\tØnsker du å kjøre programmet igjen? j/n";
    cin >> rep;
    if (rerun == 'n' || rerun == 'N') {
      cout << "\n\tExiting program\n";
    }
  }

  return 0;
}

EDIT:
I get some feedback on lack of initialization (I assume that means defining variables). I've tried not to do that. My prof. asked us to "use const as much as possible and "hardcoded" variables minimally.
I couldn't find any use of const here so I compensated with minimum "hardcording", I hope he was talking about initialization.


Solution

  • Your code is almost correct. The variables will be removed and reallocated because they are inside the inner scope.

    What you got wrong is that you don't initialize them. When you declare a primitive type like int you just get the next 4 bytes on stack so it will contain whatever is there.

    This is generally bad because sometimes you assume that it's going to be 0. So to fix your code just make sure you initialize all the variables explicitly (assign 0 to them).