Help with displaying to the user their input is not valid.

Hey, guys. I need help with the following program - I have done half of it, but I do not know how to proceed from there.

These are the instructions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Write a program takes temperature readings, but fails if the temperature value 
goes out of range three times. Output the average so we know what a stable
temperature is for these reagents.

You should repeatedly ask the user for input until the temperature value
goes outside the following range [22,230]. If the temperature values goes outside 
the range, output a warning the first two times and an error the last time.

Here are the functions you should write: 

calcAvg - this should calculate the average value of the readings, not including
the three out of range values.

displayWarning - this should display a warning, 
use a global variable to tell the user if it's the first or last warning.

displayError - this should display an error, it should only be called when
exiting the program if we got three bad readings.

GetReading - this should return a boolean value, and 
store the temperature entered by the user in the input parameter. 
It should return true if there was 
a successful reading or false if the reading was out of range. 

If at any time the user wants to stop entering temperature readings, 
they should type -123456.

Finally, you should output the decimal average with exactly 2 decimal places. 


And this is my code thus far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>

void SetupConsole(void);
double CalcAvg(int iTemp, int TempTotal, double nTemp);

using namespace std;

int main()
{
int iTemp;
int TempTotal = 0;
int nTemp = 0;

cout << "Enter the temperature value or -123456 to quit: " << endl;
cin >> iTemp;

SetupConsole();
CalcAvg(iTemp, TempTotal, nTemp);


return 0;
}

void SetupConsole(void)
    {
        cout.setf(ios::fixed);
        cout.setf(ios::showpoint);
        cout.precision(2);
    }

double CalcAvg(int iTemp, int TempTotal, double nTemp)
    {
        while (iTemp != -123456 && iTemp > 21 && iTemp < 231)
            {
                TempTotal = TempTotal + iTemp;
                nTemp++;

                cout << "Enter the temperature value or -123456 to quit: " << endl;
                cin >> iTemp;
            }
        cout << "Average temperature: " << TempTotal / nTemp;
    }


Help, please?
Maybe you can have an int, and initialize it to 0 or 1. Then anytime the user enters a value out of range, increase the int by one(1).
Check for the value of the int. If it is less than three(3), cout a warning but if it is greater or equal to three(3), then cout an error.

HTH,
Aceix.
I understand what you're trying to tell me, but I don't know how to translate that into code. :\
Quick eg--not tested:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int i=0;  //the int I was referring to in my last post to do the checking
int iInput;   //for user input purposes

do
{
     cout<<"Type the integer one(1): ";
     cin>>iInput;

     if(iInput!=1)
     {
          i++;   //increases if user did not follow my autocratic rule
          if(i<3)  //the warning. this condition is met twice if you analyze
          {
               cout<<"\nJust obey! I said the number one!";
          }
          else if(i>=3)    //finally, the error. this condition is met after two warnings
          {
               cout<<"\nYou are toooooo disobedient.\nI'm exiting!!!";
               exit(0);
          }
     }
}while(iInput!=1);


Hope it helps,
Aceix.
I completely understand that. And if I were to have to do it as a stand-alone program, I'd be able to do it perfectly fine.

What I'm having trouble with is tying it all together. I guess I should have expressed that in my previous post.
I would start by paying a bit better attention to what the assignment requires.

CalcAvg should calculate the average, not get input (as also might be suggested by the name.) Input should be obtained in GetReading. Begin by implementing GetReading.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>

void SetupConsole(void);
bool GetReading(int InputTemp);
double CalcAvg(int AvgInput, int InputTemp);

using namespace std;

int main()
{
int InputTemp;
int AvgInput = 0;

GetReading(InputTemp);
SetupConsole();
InputTemp = CalcAvg(AvgInput, InputTemp);

return 0;
}

void SetupConsole(void)
    {
        cout.setf(ios::fixed);
        cout.setf(ios::showpoint);
        cout.precision(2);
    }

bool GetReading(int InputTemp)
    {

        cout << "Temperature: " << endl;
        cin >> InputTemp;

        if (InputTemp > 21 && InputTemp < 231)
            {
                return true;
            }
        else if (InputTemp < 22 && InputTemp > 230)
            {
                return false;
            }
    }

double CalcAvg(int AvgInput, int InputTemp)
    {
        AvgInput = InputTemp + InputTemp;
        AvgInput++;
    }


Is what I've got so far. What's making this assignment so confusing is the fact that usually we'll be told what the console looks like, or at least what the functions look like. This time around it seems as though my professor decided to take off the training wheels, and thus why I'm having so much trouble with this.

At this point, I don't know where to input the loop so that the program keeps asking the user for the input if it's between the aforementioned parameters.
GetReading needs the modification of the parameter InputTemp to be propagated to the calling code, so the parameter should be of type reference to int.

Since GetReading returns a boolean value indicating whether or not a successful reading was taken (and, thus whether the program should continue getting further readings) I imagine you'll want to loop on the return value of GetReading in the code that calls it.
Like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool GetReading(int InputTemp)
    {
        do
        {
            cout << "Temperature: " << endl;
            cin >> InputTemp;
        }

        while (InputTemp != -123456);

            if (InputTemp > 21 || InputTemp < 231)
                {
                    return true;
                }
            else if (InputTemp < 22 || InputTemp > 230)
                {
                    return false;
                }
    }
1
2
3
4
5
6
7
      do
        {
            cout << "Temperature: " << endl;
            cin >> InputTemp;
        }

        while (InputTemp != -123456);


This loops until the user inputs -123456.

you'll want to loop on the return value of GetReading in the code that calls it.


the parameter should be of type reference to int.

So I should loop it in main?

I really don't understand what you're trying to say.. :\
That wasn't really meant to address the internals of GetReading.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool GetReading( int& reading )
{
    for ( ;; )
    {
        get user input for reading.
        if ( user input is the sentinel to stop getting input )
            return false
        else if ( user input is outside the proscribed ranges )
        {
            if ( this is not the third invalid input )
                display a warning
            else
            {
                display an error
                return false 
            }
        }
        else
            return true
    }
}


Sometimes it helps to consider the logic before you start writing code.
Alright well I ended up doing the same exact program without using functions at all.

Only problem is that I don't know how to exclude the numbers not between [22, 230] from being averaged.

I also need help translating this code to using functions, heh:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <cstdlib>

void SetupConsole(void);

using namespace std;

int main()
{
    int InputTemp;
    double TempTotal = 0;
    int NumTemps = 0;
    double AvgInput = 0;
    int ErrorCounter = 0;

    cout << "Enter the temperature: " << endl;
    cin >> InputTemp;

    if (InputTemp > 230 || InputTemp < 22 && InputTemp != -1)
        {
            ErrorCounter++;

            if (ErrorCounter == 1)
                {
                    cout << "Error 1, please stay within the parameters." << endl;
                }
        }

while (InputTemp != -1)
    {
        TempTotal = TempTotal + InputTemp;
        NumTemps++;

        cout << "Enter the temperature: " << endl;
        cin >> InputTemp;

        AvgInput = TempTotal / NumTemps;

    if (InputTemp > 230 || InputTemp < 22 && InputTemp != -1)
        {
            ErrorCounter++;

            if (ErrorCounter == 1)
            {
                cout << "Error 1, please stay within the parameters." << endl;
            }
            else if (ErrorCounter == 2)
            {
                cout << "Error 2, one more error and the program will terminate." << endl;
            }
            else if (ErrorCounter == 3)
            {
                cout << "Error 3. Program terminated." << endl;
                exit(1);
            }
        }
    }
    if (InputTemp == -1)
        {
            SetupConsole();

            cout << "Number of temperatures entered: " << NumTemps << endl;
            cout << "Average temperature: " << AvgInput << endl;
        }

return 0;
}

void SetupConsole(void)
    {
        cout.setf(ios::fixed);
        cout.setf(ios::showpoint);
        cout.precision(2);
    }
Topic archived. No new replies allowed.