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?
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;
@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
for(;;) //Creates an infinite loop
{
cin >> force1;
if(cin.fail())
{
cout << "Invalid entry" << endl;
cin.clear();
}
elsebreak; //If it is entered correctly it will break from the loop
}
...
for(;;) //Creates an infinite loop
{
cin.get();
cin >> force1;
if(cin.fail())
{
cout << "Invalid entry" << endl;
cin.clear();
}
elsebreak; //If it is entered correctly it will break from the loop
}