나는이 코드를 실행하려고 애 쓰고 있습니다. 나는 내가 입력 한 float 값이 float와 동일한 데이터 유형인지 여부를 식별하려고합니다. 이것은 나의 코딩이다.데이터 형식 변환을 사용하여 플로팅 할 문자열
#include <iostream>
#include <string>
using namespace std;
int main()
{
float x1, y1, x2, y2, x3, y3, percentage;
string sym1, sym2;
int count;
cout<<"Enter the first fraction : ";
do{
cin>>x1>>sym1>>y1;
if(x1 != float(x1) || sym1 != string(sym1) || y1 != float(y1))
{
cout<<"Please enter the correct fraction : ";
}
else if(sym1 != "/")
{
cout<<"Sorry. This is not a fraction. Please enter again : ";
}
else
{
break;
}
}while(x1 == float(x1) || sym1 == string(sym1) || y1 == float(y1));
cout<<"Enter the second fraction : ";
do{
cin>>x2>>sym2>>y2;
if(x2 != float(x2) || sym2 != string(sym2) || y2 != float(y2))
{
cout<<"Please enter the correct fraction : ";
}
else if(sym2 != "/")
{
cout<<"Sorry. This is not a fraction. Please enter again : ";
}
else
{
break;
}
}while(x2 == float(x2) || sym2 == string(sym2) || y2 == float(y2));
x3 = x1 * x2;
y3 = y1 * y2;
percentage = (x3*100)/y3;
cout<<x1<<"/"<<y1<<" and "<<x2<<"/"<<y2<<" is "<<x3<<"/"<<y3<<"\n";
cout<<x3<<"/"<<y3<<" is "<<percentage<<"%";
return 0;
}
내가 변화하기 위해 노력하고있어 코드의 조각이
do{
cin>>x1>>sym1>>y1;
if(x1 != float(x1) || sym1 != string(sym1) || y1 != float(y1))
{
cout<<"Please enter the correct fraction : ";
}
else if(sym1 != "/")
{
cout<<"Sorry. This is not a fraction. Please enter again : ";
}
else
{
break;
}
}while(x1 == float(x1) || sym1 == string(sym1) || y1 == float(y1));
는 내가 4/6 또는 기타 관련 부분 형식을 입력 할 때, 제대로 읽는 것입니다. 동일하게 4 * 6으로 가면 예상 출력이 인쇄됩니다. 하지만/6 또는 6/a를 입력하면 논리 오류가 발생하며 무한 루프가됩니다. if 문과 while 문에서 데이터 변환의 어딘가 에서처럼 잘못되었습니다. 또는 사용 된 데이터 유형이 잘못 되었기 때문입니까? 나는 그 문제가 무엇인지 추적 할 수 없었다. 이 작업을 수행하는 방법에 대한 해결책이 있습니까? 도와주세요. 형제 자매들에게 미리 감사드립니다.
코드에 주석을 추가 할 수 있습니까? 무슨 일이 일어나고 있는지 전혀 모르겠다 : -/ – Swanand
'cin >> sym1'은 문자열이기 때문에 어떤 문자든지 받아 들여질 것이기 때문에 cin에 남아있는 모든 것을 펌프 처리한다. 당신이 들여다보고 싶은 것은 렉서이다. . –
Alexis Wilke 어때? 그것이 효과를 낼 것인가? –