Im trying to make an average calculation when given 3 numbers

I am trying to make a program that when given three numbers calculates an average. The only problem is that it doesn't re ask correctly if a letter is put in.

Oh and I have noticed that the formatting makes it look all in line when i copied and pasted the code here

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
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

[code][code]int main()
   int number1;
    int number2;
    int number3;
    bool notarealnumber=false;

    cout<<"enter three numbers"<<endl;
    cout<<"enter number 1:";
    cin>>number1;
    do
    {


    if (cin.fail())
    {
        cout<<"pick a real number";
        cin.clear();
        cin.ignore (1000, '\n');
        (notarealnumber=true);
    }
    else
    {
        cin.ignore (1000, '\n');
        (notarealnumber=false);
    }
    }
    while (notarealnumber=false);

    cout<<"enter number 2:";
    cin>>number2;
    do
    {


    if (cin.fail())
    {
        cout<<"pick a real number";
        cin.clear();
        cin.ignore (1000, '\n');
        (notarealnumber=true);
    }
    else
    {
        cin.ignore (1000, '\n');
        (notarealnumber=false);
    }
    }
    while (notarealnumber=false);

    cout<<"enter number 3:";
    cin>>number3;
    do
    {


    if (cin.fail())
    {
        cout<<"pick a real number";
        cin.clear();
        cin.ignore (1000, '\n');
        (notarealnumber=true);
    }
    else
    {
        cin.ignore (1000, '\n');
        (notarealnumber=false);
    }
    }
    while (notarealnumber=false);

    cout<<"The average is "<<(number1+number2+number3)/3<<endl;




    return 0;
}
Last edited on
Hello dboyhade,



PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Try this after your "std::cin" statements.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cout << "enter three numbers" << endl;
cout << "enter number 1: ";
cin >> number1;

while (!std::cin)
{
	std::cout << "Error message." << std::endl;

	std::cin.clear();
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

	cout << "enter three numbers" << endl;
	cout << "enter number 1: ";
	cin >> number1;
}

Given a little time I can come up with something better for the input.

Hope that helps,

Andy
Hello dboyhade,

This should work better for you:
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
//  Im trying to make an average calculation

#include <iostream>
#include <iomanip>
#include <limits>

//using namespace std;  // <--- Best not to use.

int main()
{  // <--- You missed the opening brace.
	constexpr size_t MAXSIZE{ 3 };

	int numbers[MAXSIZE]{};

	std::cout << "\n  Enter three numbers" << std::endl;
	
	for (size_t lc = 0; lc < MAXSIZE; lc++)
	{
		std::cout << "\n enter number "<<lc+1<<": ";
		std::cin >> numbers[lc];

		while (!std::cin)
		{
			std::cout << "\n  pick a real number!\n";
			std::cin.clear();
			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

			std::cout << "\n enter number " << lc + 1 << ": ";
			std::cin >> numbers[lc];
		}

	}

	std::cout << std::fixed << std::showpoint << std::setprecision(2);

	std::cout << "\n The average is " << (numbers[0] + numbers[1] + numbers[2]) / 3.0 << std::endl;

	return 0;
}


Hope that helps,

Andy

Edit: Forgot to mention that there is nothing in your code the would require "cmath".
Last edited on
Thanks that should help
Topic archived. No new replies allowed.