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
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace 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;
}
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.