When I enter a wrong input like a character it makes an error. How will I get rid of the error in my cin?

I'am beginner in C++.I have a problem with my program can somebody tell what will I do to stop the the error in the cin. When I input a wrong key like a letter it begins to make an error. Am I correct that the cin in my program only recognize numbers? What will I do to stop the error?

#include<iostream>
#include<conio.h>
#include<math.h>

using namespace std;

int main()
{

double force1=0, force2=0, force3=0, force4=0;
double angle1=0, angle2=0, angle3=0, angle4=0, resultant=0, angle=0;
double sfx=0, sfy=0, pi=3.14159265;
char answer='y';




do
{
cout<<"\t\t\t VECTOR ADDTION"<<endl;
cout<<" Finding the Resultant force and its angle using COMPONENT METHOD\n"<<endl;



getch();

cout<<"Enter Force1: ";
cin>>force1;
cout<<"Enter the angle of Force1: ";
cin>>angle1;
cout<<"Enter Force2: ";
cin>>force2;
cout<<"Enter the angle of Force2: ";
cin>>angle2;
cout<<"Enter Force3: ";
cin>>force3;
cout<<"Enter the angle of Force3: ";
cin>>angle3;
cout<<"Enter Force4: ";
cin>>force4;
cout<<"Enter the angle of Force4: ";
cin>>angle4;


sfx=force1*cos (angle1*pi/180)+force2*cos (angle2*pi/180)+force3*cos (angle3*pi/180)+force4*cos (angle4*pi/180);
sfy=force1*sin (angle1*pi/180)+force2*sin (angle2*pi/180)+force3*sin (angle3*pi/180)+force4*sin (angle4*pi/180);
resultant=sqrt (pow (sfx, 2)+pow (sfy, 2));
angle=atan (sfy/sfx)*180/pi;

if(angle>0&&angle<90)
{
cout<<" The resultant force is "<<resultant<<" at an angle of "<<angle<<" degrees at quadrant 1 \n"<<endl;
}
else if(angle>90&&angle<180)
{
cout<<" The resultant force is "<<resultant<<" at an angle of "<<angle<<" degrees at quadrant 2 \n"<<endl;
}
else if(angle>180&&angle<270)
{
cout<<" The resultant force is "<<resultant<<" at an angle of "<<angle<<" degrees at quadrant 3 \n"<<endl;
}
else if(angle>270&&angle<360)
{
cout<<" The resultant force is "<<resultant<<" at an angle of "<<angle<<" degrees at quadrant 4 \n"<<endl;
}
else if(angle==90&&angle==270)
{
cout<<" The resultant force is "<<resultant<<" at an angle of "<<angle<<" degrees at y-axis \n"<<endl;
}
else if(angle==0&&angle==180)
{
cout<<" The resultant force is "<<resultant<<" at an angle of "<<angle<<" degrees at x-axis \n"<<endl;
}
else if(angle<0&&angle>-90)
{
cout<<" The resultant force is "<<resultant<<" at an angle of "<<angle<<" degrees at quadrant 4 \n"<<endl;
}
else if(angle<-90&&angle>-180)
{
cout<<" The resultant force is "<<resultant<<" at an angle of "<<angle<<" degrees at quadrant 3 \n"<<endl;
}
else if(angle<-180&&angle<-270)
{
cout<<" The resultant force is "<<resultant<<" at an angle of "<<angle<<" degrees at quadrant 2 \n"<<endl;
}
else if(angle<-270&&angle<-360)
{
cout<<" The resultant force is "<<resultant<<" at an angle of "<<angle<<" degrees at quadrant 1 \n"<<endl;
}
else if(angle==-180)
{
cout<<" The resultant force is "<<resultant<<" at an angle of "<<angle<<" degrees at x-axis \n"<<endl;
}
else if(angle==-90&&angle==-270)
{
cout<<" The resultant force is "<<resultant<<" at an angle of "<<angle<<" degrees at y-axis \n"<<endl;
}

getch();

cout<<" Do you want to try again? y or n ? \n"<<endl;
cin>>answer;

} while(answer=='y');
{
cout<<"THANK YOU! HAVE A NICE DAY"<<endl;

}
getch();



return 0;
}
Last edited on
closed account (jwkNwA7f)
It cin will get whatever was inputted, so if the user inputs a char when your getting an int it will still get it.
You can use cin.fail()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
cin >> force1;
if(cin.fail())
{
    cout << "Invalid entry" << endl;
    cin.clear();
}
cin >> force2;
if(cin.fail())
{
    cout << "Invalid entry" << endl;
    cin.clear();
}

...


Also, next time can you use code tags.
Hope this helped!
Or you can use fflush(stdin) whenever you get char and them you want to get an int or vice versa
@cppprogrammer297 (216) thank you for the solution! but still there is an error.
This is when I run the program.

VECTOR ADDTION
Finding the Resultant force and its angle using COMPONENT METHOD

Enter Force1: g
Invalid entry
Enter the angle of Force1: Enter Force2: Enter the angle of Force2: Enter Force3
: Enter the angle of Force3: Enter Force4: Enter the angle of Force4: The resul
tant force is 0 at an angle of -1.#IND degrees at quadrant 2



how am I gonna get rid of this and comeback in entering an input?
Enter the angle of Force1: Enter Force2: Enter the angle of Force2: Enter Force3
: Enter the angle of Force3: Enter Force4: Enter the angle of Force4: The resul
tant force is 0 at an angle of -1.#IND degrees at quadrant 2
Last edited on
closed account (jwkNwA7f)
I think this is what you meant:
1
2
3
4
5
6
7
8
9
10
11
12
13
for(;;) //Creates an infinite loop
{
   cin >> force1;
   if(cin.fail())
   {
       cout << "Invalid entry" << endl;
       cin.clear();
   }
   else
      break; //If it is entered correctly it will break from the loop
}

...
still it doesnt break in the infinite cycle when I enter a character instead of a number.
closed account (jwkNwA7f)
I tested this and it worked:
1
2
3
4
5
6
7
8
9
10
11
12
for(;;) //Creates an infinite loop
{
   cin.get();
   cin >> force1;
   if(cin.fail())
   {
       cout << "Invalid entry" << endl;
       cin.clear();
   }
   else
      break; //If it is entered correctly it will break from the loop
}
I get it. Thank you for the idea!
closed account (jwkNwA7f)
NP, glad to help.
Topic archived. No new replies allowed.