-4

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.

Andrew
  • 5,212
  • 1
  • 22
  • 40
Manumit
  • 67
  • 1
  • 1
  • 7
  • Please reformat your code. – Christian Neverdal Sep 18 '15 at 13:54
  • What's wrong with it? – Manumit Sep 18 '15 at 13:54
  • You seem to be using `minTemp`, `maxTemp`, and `mmNedbor` without initializting them, and this is bad. – crashmstr Sep 18 '15 at 14:00
  • 2
    "I get some feedback on lack of initialization (I assume that means defining variables)." Three good terms to know, and not mix up, are: Declare, Define, and Initialize. You can google the difference or check out things like http://stackoverflow.com/questions/23345554/the-differences-between-initialize-define-declare-a-variable – RyanP Sep 18 '15 at 14:15
  • you would make the code a bit more readable if you created some functions e.g. `int getDaysInMonth()` it would also reduce the number of variables you need. – AndersK Sep 18 '15 at 17:54
  • @CyberSpock that is noted, ty. So far I have 1 month of c++ experience and don't know how yet. – Manumit Sep 18 '15 at 17:56
  • 1
    e.g `int getDaysInMonth() { int antDager = 0; 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); return antDager; }` – AndersK Sep 18 '15 at 17:56
  • @CyberSpock I want to implement this! but in my for loop I am using antD (second day variable) to make it function. How do I replace antD with your function? `for (antD = 1; antD < antDager; antD++)` – Manumit Sep 18 '15 at 19:04

5 Answers5

4

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).

Sorin
  • 11,863
  • 22
  • 26
  • By initializing you mean give them numerical values? My prof. asked us to keep "hardcorded" variables to a minimum. – Manumit Sep 18 '15 at 13:59
  • Yes, give them some initial value. It's not hardcoded if you have some way to change the value. In the end if you set all the values from input it doesn't matter. If somehow you skip one of them, instead of some random value or the one from the last run you will have the initial value you set. – Sorin Sep 18 '15 at 14:08
2

You are already having your variables reset for you thanks to C++'s value semantics. In your while loop you have

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;  

When you reach the end of your while loop (}) all of those variables will go out of scope. Then when you start the loop again they will be recreated and reset to their initial values again.

You are using mmNedbor, maxTemp and minTemp in your for loop before you initialize them which is undefined behavior so you should set them to some default initial value.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
2

What this means is that any variable that you use inside of a loop that depends on it being initialized to a certain value should be initialized at the start of the loop.

In your case, you declare all of your relevant variables inside of the loop, but you don't assign an initial value to all of them. If you try to read any of those uninitialized values before writing to them, they will contain unknown data. In particular, minTemp, maxTemp, and mmNedbor are read without initialization. So make sure all of those variables have some initial value.

dbush
  • 205,898
  • 23
  • 218
  • 273
1

Just set all the variables you use throughout your loop to 0 or something at the beginning of your loop, like you have done with your minTempAdd and mmNedborAdd variables.

As a side note, your code is hard to read, so I would recommend you to spend some time refactoring it.

Archie
  • 11
  • 1
-2
ex: my name program is program_name.cpp
how to reset all variabel : 
my version is :
char choice;
cout<< "do you want to reset all variabel and loop the program"
cin>>choice;
if (choice=="y")system("program_name.axe")
else(exit(0))

if you want to reset ALL VARIABEL please dont use goto;
REX
  • 1
  • 1