I'm trying to make a thermometer that can show when the temperature goes out of the range of 17 and 24 degrees,
when it goes below 17 degrees i want it to count how many times it goes below 17 degrees and how may times it goes above 24 degrees as well.
any tips to help improve my code are welcome, as when i run this code it constantly shows temperature below 17 and wont stop

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    
      float index;
      float random_temp = 0;
      float total = 0;
      srand (time (NULL));
      int lowest=12, highest=28;
      int range=(highest-lowest)+1;
      
      cout << " Temperature Vaules " << "\t" << " Sum of Temperature Values " << endl;
      
      for (index = 1; index <= 40; index++)
      {
          
          random_temp = lowest+int(range*rand()/(RAND_MAX + 1.0));
          
          while(random_temp != 17,18,19,20,21,22,23,24)
          {
                if (( random_temp >= 12 || random_temp < 17 ))
                {
                      while( random_temp >= 12 || random_temp < 17 )
                      {
                             cout << " Temperature below 17 Degrees Celsius " << endl;
                      }
                }
                else if (( random_temp > 24 || random_temp <= 28 ))
                {
                     while( random_temp > 24 || random_temp <= 28 )
                     {
                            cout << " Temperature above 24 Degrees Celsius " << endl;
                     }
                }
          }
          
          total = total + random_temp;
          cout << "\t" << random_temp << "\t" << "\t" << "\t" <<  total << endl;
      }
    
    
    
    
    
    
    system( "pause");
    return 0;
}
while(random_temp != 17,18,19,20,21,22,23,24)

That's just plain wrong. I suspect you meant:

while(random_temp != 17 &&
      random_temp != 18 &&
      random_temp != 19 &&
      random_temp != 20 &&
      random_temp != 21 &&
      random_temp != 22 &&
      random_temp != 23 &&
      random_temp != 24)

which can be more elegantly put

while(random_temp < 17 || random_temp > 24)

First of all you need to get rid of the while loops in your if and else if statement:

if (( random_temp >= 12 || random_temp < 17 ))
                {
                      while( random_temp >= 12 || random_temp < 17 )
                      {
                             cout << " Temperature below 17 Degrees Celsius " << endl;
                      }
                }
                else if (( random_temp > 24 || random_temp <= 28 ))
                {
                     while( random_temp > 24 || random_temp <= 28 )
                     {
                            cout << " Temperature above 24 Degrees Celsius " << endl;
                     }
                }

You don't want to loop the output to the console for ever.
Next thing, change your if and else if conditions to:

if (random_temp < 17)
{
...
}
else if (random_temp > 24)
{
...
}

You don't need to check wether the temperature generated is greater than 12 or lower than 28, you only care about when it is out of the range you've set. The OR operator that you used also made the programm always enter the first if end never go to else if because the temperature always was above or equal to 12.

Lastly, you want to put this statement:

random_temp = lowest+int(range*rand()/(RAND_MAX + 1.0));

inside the while loop (at the very start of it) so that the temperature is refreshed.
Put also a cin.gets(); at the end of the loop so that the programm waits for a key press before it starts the loop again.

while(random_temp != 17,18,19,20,21,22,23,24)

That's just plain wrong. I suspect you meant:

My compiler accepts that... I thought it was wrong too, but when I tested it I didn't got any errors :S

It's not incorrect syntax. It's just not at all what he meant to do.

The "," operator separates a group of expressions that are evaluated from left to right, so in this case

(random_temp != 17,18,19,20,21,22,23,24)

random_temp != 17 is evaluated, maybe true, maybe false, and then....

18 is evaluated. It's 18. The result of the previous expression has been discarded, and then...

19 is evaluated. It's 19. The result of the previous expression has been discarded, and then...

... all the way to ...

24 is evaluated. It's 24. So the whole thing is equivalent to

(24)

and the while statement is

while(24)

which will loop forever.

thanks for the reply guys but unfortunately its not runing the way i wanted it to, maybe i didnt explain fully what im tryig to accomplish, i only posted a part of my programs functio to see if it would work. i apologise if i have confused you with this.

first, i'm trying to get the thermometer to display how many times the degrees goes below 17,
then how many times it goes above 24,
then average of the temperature <== i have this part working so i dont need help with this
then i want to determine when the temperature goes outside the range of between 17 & 24 degrees how may minutes it will take to fall back into range

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
      float index;
      float random_temp = 0;
      float total = 0;
      srand (time (NULL));
      int lowest=12, highest=28;
      int range=(highest-lowest)+1;
      
      cout << " Temperature Vaules " << "\t" << " Sum of Temperature Values " << endl;
      
      for (index = 1; index <= 40; index++)
      {
          
          
          while(random_temp < 17 || random_temp > 24)
          {
                
                random_temp = lowest+int(range*rand()/(RAND_MAX + 1.0));
                
                if (( random_temp > 12 || random_temp < 17 ))
                {
                             cout << " Temperature below 17 Degrees Celsius " << endl;
                }
                else if (( random_temp > 24 || random_temp <= 28 ))
                {
                     cout << " Temperature above 24 Degrees Celsius " << endl;
                }
          }
          
          total = total + random_temp;
          cout << "\t" << random_temp << "\t" << "\t" << "\t" <<  total << endl;
      }
      return total;
      
}
//------------------------------------------------------------------------------
float calcAverage(float total)
{

      double average;
      double mean_of_temp = 40;
      
      average = total / mean_of_temp;
      
      return average;
}

//------------------------------------------------------------------------------
void displayResults(float average)
{
     cout  << "\n" << "The average temperature is " << average << endl;  
}

The code is faulty and badly structured. Here's an example of what you want to do:

for(index = 1; index <= 40; index++)
{
   random_temp = lowest+int(range*rand()/(RAND_MAX + 1.0));

   if(random_temp < 17)
      count_low++; //Declare count_low at the beginning as "int count_low = 0;"
   else if(random_temp > 24)
      count_high++; //Same as count_low

   total += rand_temp;
}

//Calculate average (total / 40)
//Print average
//Print count_low and count_high

Now about the minutes, I'm not sure of what you mean exactly.

Now about the minutes, I'm not sure of what you mean exactly.

thnks very much mikro, that has tidied up my code alot and works well,
however it shows the answer 3.21387e-039 every time i run it and i have declared the variable as = to 0 so that it wouldnt pick any umber stored in memory

i declared the variables above_temp and below_temp as 0
and the answer then is 0, im not sure where exactly im making the mistake
moschops, and that should resolve the floatig point number shouldnt it?

#include <iostream>
#include <cmath>
using namespace std;
{      
      float index;
      float random_temp = 0;
      float total = 0;
      float below_temp = 0;
      float above_temp = 0;

      srand (time (NULL));
      int lowest=12, highest=28;
      int range=(highest-lowest)+1;
      
      cout << " Temperature Values " << endl;
      for(index = 1; index <= 40; index++)
      {
           random_temp = lowest+int(range*rand()/(RAND_MAX + 1.0));

           if(random_temp < 17)
           {  
                float below_temp = 0;    
                below_temp++;
           }
           else if(random_temp > 24)
           {
                float above_temp = 0;
                above_temp++;
                  
           }
           
           total += random_temp;
           cout << "\t" << random_temp << endl;
      }
      cout << "Times Temperature went below 17 degrees = " << below_temp << endl;
      cout << "Times Temperature went above 24 degrees = " << above_temp << endl;

//Calculate average (total / 40)
//Print average
//Print count_low and count_high
      return total;
      
}
if(random_temp < 17)
           {  
                float below_temp = 0;   //What is this? 
                below_temp++;
           }
           else if(random_temp > 24)
           {
                float above_temp = 0;   //Again...?
                above_temp++;
 
           }

No! You don't want to initialise the variables again within the ifs! This way you will never get the correct value! And there is no point in using float type because a count of times is always an integer.

and that should resolve the floatig point number shouldnt it?

What should resolve the floating point number? Which floating point number? What do you mean "resolve"?

No! You don't want to initialise the variables again within the ifs! This way you will never get the correct value! And there is no point in using float type because a count of times is always an integer.

oh right, thats where i've been going wrong, my lecturer told me to always initialise the variables so i just made a habit of doing it all the time,

and i change it the way you said and now the values are

temp below = 2293496 ad temp above = 4247312

i meant moschops that by changing it shoul it not fix my code and make it run properly by initialising the variable, but obviously i was wrong because when i do so, it just gives me the answer of 0

i change it the way you said and now the values are

temp below = 2293496 ad temp above = 4247312

They shouldn't if you only initialised the variables at the beginning of main to zero. But only at the very beginning of main, n-o-w-h-e-r-e else.

They shouldn't if you only initialised the variables at the beginning of main to zero. But only at the very beginning of main, n-o-w-h-e-r-e else.

thats what i have done, only in int main i initialised everything, i've posted the entire code im using to show you what ive done

#include <iostream>
#include <cmath>
using namespace std;

//------------------------------------------------------------------------------
// Function Prototypes

float explainPrg();
float getTemp();
float calcAverage (float total);
/*float getRange(float random_temp);*/
void displayResults(float average, float total);

int main()
{
    
    float random_temp = 0;
    float operational_range = 0;
    float index;
    float total = 0;
    double average = 0;
    float time = 0;
    int below_temp = 0;
    int above_temp = 0;
    
    total = getTemp();
    average = calcAverage(total);
    /*operational_range = getRange(random_temp);*/
    displayResults(average, total);
    

    cout << endl;
    system( "pause");
    return 0;
}
//------------------------------------------------------------------------------
float getTemp()
{
      /*This function uses a random number generator
      to get different values for temperature between 12 and 28 degrees celsius
      and shows the sum of all temperature values*/
      
      float index;
      float random_temp;
      float total;
      int below_temp;
      int above_temp;

      srand (time (NULL));
      int lowest=12, highest=28;
      int range=(highest-lowest)+1;
      
      cout << " Temperature Values " << endl;
      for(index = 1; index <= 40; index++)
      {
           random_temp = lowest+int(range*rand()/(RAND_MAX + 1.0));

           if(random_temp < 17)
           {   
                int below_temp;    
                below_temp++;   
           }
           else if(random_temp > 24)
           {
                int above_temp;
                above_temp++;     
           }
           
           total += random_temp;
           cout << "\t" << random_temp << endl;
      }
      cout << "\n" << "Times Temperature went below 17 Degrees = " << below_temp << endl;
      cout << "\n" << "Times Temperature went above 24 Degrees = " << above_temp << endl;


      return total;
      
}
//------------------------------------------------------------------------------
float calcAverage(float total)
{

      double average;
      double mean_of_temp = 40;
      
      average = total / mean_of_temp;
      
      return average;
}

//------------------------------------------------------------------------------
void displayResults(float average, float total)
{
     cout  << "\n" << "The Total Temperature is " << total << endl;
     cout  << "\n" << "The Average Temperature is " << average << endl;  
}
//------------------------------------------------------------------------------

/*float getRange(int random_temp)
{
      // This function calculates the temperature when it goes outside of the operational range
      float operational_range;
      if ((random_temp >=17 && random_temp <=24))
      {
                         
              operational_range = random_temp;
              cout << operational_range << endl;
      }
      else
      {
          cout << operational_range << endl;
      }
      return operational_range;         
      
}*/

OK, things are getting a little bit mixed up.
Variables have scopes. To put it simple, variables declared in different functions can't "see" each other. They also live as long as the function is executed. You declare and initialise variables within the function they will have a meaning and purpose, thus are going to be used to do something.
Now, the way you had your code formatted, you're not using most of the variables you declare in main( ). Initialise below_temp and above_temp in getTemp() and erase them from main()

Edited 5 Years Ago by mikrosfoititis: n/a

ye im sorry about that, em im after doing that and the answer still shows up as 2293496 and 4247328

have you run this in your own compiler and its worked?

i got the code to work, my problem was that i needed to declare count_low outside of the for loop so by making count_low = count_low it worked when i used the cout function

cout << " Temperature Vaules " << endl;
      for(index = 1; index <= 40; index++)
      {
           random_temp = lowest+int(range*rand()/(RAND_MAX + 1.0));
           
           if(random_temp < 17)
           {
                count_low++;
                cout << "The temperature went below 17 degress " << endl;
           }
           else if(random_temp > 24)
           {
                count_high++;
                cout << "The temperature went above 24 degress " << endl;
           }
           
           count_low = count_low; // by making count_low = count_low this gets rid of floating point numbers and makes it visible outside of the for loop
           count_high = count_high; // similarly with count_high
           total += random_temp;
           cout << "\t" << random_temp << endl;
           
      }
      cout << "\n" << "The number of times the temperature went below 17 degress = " << count_low << endl;
      cout << "The number of times the temperature went above 24 degress = " << count_high << endl;

thanks very much to the both of you for all of your help, now to work on making it determine how many minutes it takes until the temperature to fall back into range using a clock :)

count_low = count_low; // by making count_low = count_low this gets rid of floating point numbers and makes it visible outside of the for loop
count_high = count_high; // similarly with count_high

Ok, can anybody see the point at this?

This article has been dead for over six months. Start a new discussion instead.