ผมต้องการแสดงค่าการเปลี่ยนหน่วยความยาวให้ออกมาเป็นเมตร โดยที่สามารถพิมพ์หน่วยอะไรก็ได้ระหว่าง in, cm, ft, m และความยาวที่คิดมาเป็นหน่วยเมตรนั้นจะบวกไปเรื่อยๆ ในขณะเดียวกันก็ต้องแสดงข้อความโดยมีการบันทึกและเปรียบเทียบว่าความยาวที่ออกมาเป็นเมตรแต่ละครั้งที่ป้อนนั้นตัวไหนยาวสุดและสั้นสุด เช่น
พิมพ์ 9 cm
ผลลัพธ์ 9 cm is 0.09 m
The sum of length is 0.09 m
0.09 m is the shortest
0.09 m is the longest
พิมพ์ 10 in
ผลลัพธ์ 10 in is 0.254 m
The sum of length is 0.344 m
0.254 m is the shortest
0.09 m is the longest...
---------------------------------------------------------------------------------------------
โค๊ดผมตามนี้ครับ
int main()
{
string unit = "cm"; "m"; "in"; "ft";
double sum = 0, longest = 0, shortest = 0, next = 0, meter = 0, length = 0;;
cout << "Enter a length value with a unit\n";
cin >> length >> unit;
shortest = length;
longest = length;
do{
if (unit != "cm"&& unit != "m" &&unit != "in"&&unit != "ft")
cout << "The unit given is not accepted\n"
<< "Enter a length value with a unit\n";
//__________illegal unit indicator___________________________________________
else if (unit == "cm")
{
cout << length << " cm is " << 0.01*length << " m\n"; //เปลี่ยน cm เป็น m
meter = 0.01*length; //เก็บค่าเป็นความยาว
sum += (0.01*length); //บวกความยาวตามลูป
cout << "The sum of length is " << sum << " m\n"; //แสดงผลบวก
}
//______________cm____________________________________________________________________
else if (unit == "in")
{
cout << length << " in is " << 0.0254*length << " m\n"; //เปลี่ยน in เป็น m
meter = 0.0254*length;
sum += (0.0254*length);
cout << "The sum of length is " << sum << " m\n";
}
//________________in____________________________________________________
else if (unit == "ft")
{
cout << length << " ft is " << 0.3048*length << " m\n"; //เปลี่ยน ft เป็น m
meter=0.3048*length;
sum += (0.3048*length);
cout << "The sum of length is " << sum << " m\n";
}
//_____________________________ft___________________________________________
else if (unit == "m")
{
cout << length << " m is " << length << " m\n"; //เมตรเหมือนเดิม
meter=length;
sum += length;
cout << "The sum of length is " << sum << " m\n";
}
if (meter<shortest)
{
shortest = meter;
}
else if (meter>longest)
{
longest = meter;
}
cout << shortest << " m is the shortest so far\n"
<< longest << " m is the longest so far\n";
cin >> next >> unit;
}while (length=next);
return 0;
}
ถ้าผมเริ่มค่าด้วย m ผลลัพธ์ไม่มีปัญหาครับ

แต่มีปัญหาเมื่อเริ่มค่าแรกด้วย in, ft, cm จึงอยากทราบว่าแก้ส่วนนี้ยังไง สังเกตว่าค่ายาวสุดเป็น 8 m เสมอ ผมเก็บค่าความยาวไว้ใน meter ครับ
อีกข้อคือทำอย่างไรให้ใส่ค่าเลขศูนย์ได้โดยที่ไม่เป็นการออกจากโปรแกรม ขอบคุณครับ
[C++] ช่วยแก้โค๊ดเปรียบเทียบค่าจัดเก็บหน่อยครับ
พิมพ์ 9 cm
ผลลัพธ์ 9 cm is 0.09 m
The sum of length is 0.09 m
0.09 m is the shortest
0.09 m is the longest
พิมพ์ 10 in
ผลลัพธ์ 10 in is 0.254 m
The sum of length is 0.344 m
0.254 m is the shortest
0.09 m is the longest...
---------------------------------------------------------------------------------------------
โค๊ดผมตามนี้ครับ
int main()
{
string unit = "cm"; "m"; "in"; "ft";
double sum = 0, longest = 0, shortest = 0, next = 0, meter = 0, length = 0;;
cout << "Enter a length value with a unit\n";
cin >> length >> unit;
shortest = length;
longest = length;
do{
cout << "The unit given is not accepted\n"
<< "Enter a length value with a unit\n";
//__________illegal unit indicator___________________________________________
else if (unit == "cm")
{
cout << length << " cm is " << 0.01*length << " m\n"; //เปลี่ยน cm เป็น m
meter = 0.01*length; //เก็บค่าเป็นความยาว
sum += (0.01*length); //บวกความยาวตามลูป
cout << "The sum of length is " << sum << " m\n"; //แสดงผลบวก
}
//______________cm____________________________________________________________________
else if (unit == "in")
{
cout << length << " in is " << 0.0254*length << " m\n"; //เปลี่ยน in เป็น m
meter = 0.0254*length;
sum += (0.0254*length);
cout << "The sum of length is " << sum << " m\n";
}
//________________in____________________________________________________
else if (unit == "ft")
{
cout << length << " ft is " << 0.3048*length << " m\n"; //เปลี่ยน ft เป็น m
meter=0.3048*length;
sum += (0.3048*length);
cout << "The sum of length is " << sum << " m\n";
}
//_____________________________ft___________________________________________
else if (unit == "m")
{
cout << length << " m is " << length << " m\n"; //เมตรเหมือนเดิม
meter=length;
sum += length;
cout << "The sum of length is " << sum << " m\n";
}
if (meter<shortest)
{
shortest = meter;
}
else if (meter>longest)
{
longest = meter;
}
cout << shortest << " m is the shortest so far\n"
<< longest << " m is the longest so far\n";
cin >> next >> unit;
}while (length=next);
return 0;
}
ถ้าผมเริ่มค่าด้วย m ผลลัพธ์ไม่มีปัญหาครับ
แต่มีปัญหาเมื่อเริ่มค่าแรกด้วย in, ft, cm จึงอยากทราบว่าแก้ส่วนนี้ยังไง สังเกตว่าค่ายาวสุดเป็น 8 m เสมอ ผมเก็บค่าความยาวไว้ใน meter ครับ
อีกข้อคือทำอย่างไรให้ใส่ค่าเลขศูนย์ได้โดยที่ไม่เป็นการออกจากโปรแกรม ขอบคุณครับ