I was checking out a new book on C++ when I did one of the exercises in it. The program gets a length followed by its unit. The program converts the value into centimeters and then compares the value with previous inputs and gives an output saying which is the largest and smallest value entered so far. The problem is that, there seems to be a problem when using the 'feet' unit. Below is my main.cpp:
#include "std_lib_facilities.h"
int main()
{
double value{0};
double largest{0};
double smallest{0};
string unit;
constexpr double cm_per_in {2.54};
constexpr double cm_per_m {100};
constexpr double cm_per_ft {cm_per_in*12};
int sum;
while(cin >> value >> unit)
{
if(unit=="cm")
{
if(largest==0 && smallest == 0){
smallest = value;
largest = value;
cout << value << "cm is the only value entered till now.\n";
}
else if(value<smallest)
{
smallest = value;
cout << "\n\nThe smallest so far is "<<smallest<<"cm.\n";
cout << "\nThe largest so far is "<<largest<<"cm.\n";
}
else if(value>largest){
largest = value;
cout << "The smallest so far is "<<smallest<<"cm.\n";
cout << "\nThe largest so far is "<<largest<<"cm.\n";
}
sum += value;
}
else if(unit=="m")
{
if(largest==0 && smallest == 0){
smallest = value*cm_per_m;
largest = value*cm_per_m;
cout << value*cm_per_m << "cm is the only value entered till now.\n";
}
else if(value*cm_per_m<smallest)
{
smallest = value*cm_per_m;
cout << "The smallest so far is "<<smallest<<"cm.\n";
cout << "\nThe largest so far is "<<largest<<"cm.\n";
}
else if(value*cm_per_m>largest){
largest = value*cm_per_m;
cout << "\nThe smalles so far is "<<smallest<<"cm.\n";
cout << "The largest so far is "<<largest<<"cm.\n";
}
sum += value*cm_per_m;
}
else if(unit=="ft")
{
if(largest==0 && smallest == 0){
smallest = value*cm_per_ft;
largest = value*cm_per_ft;
cout << value*cm_per_ft << "cm is the only value entered till now.\n";
}
else if(value*cm_per_ft<smallest)
{
smallest = value*cm_per_ft;
cout << "The smallest so far is "<<smallest<<"cm.\n";
cout << "\nThe largest so far is "<<largest<<"cm.\n";
}
else if(value*cm_per_ft>largest){
largest = value*cm_per_ft;
cout << "\nThe smallest so far is "<<smallest<<"cm.\n";
cout << "The largest so far is "<<largest<<"cm.\n";
}
sum += value * cm_per_ft;
}
else if(unit=="in")
{
if(largest==0 && smallest == 0){
smallest = value*cm_per_in;
largest = value*cm_per_in;
cout << value*cm_per_in << "cm is the only value entered till now.\n";
}
else if(value*cm_per_in<smallest)
{
smallest = value*cm_per_in;
cout << "The smallest so far is "<<smallest<<"cm.\n";
cout << "The largest so far is "<<largest<<"cm.\n";
}
else if(value*cm_per_in>largest){
largest = value*cm_per_in;
cout << "The smallest so far is "<<smallest<<"cm.\n";
cout << "The largest so far is "<<largest<<"cm.\n";
}
sum += value * cm_per_in;
}
}
return 0;
}
Below is the input/output which happened with the program: D:\C++\testing\subtesting>program 24 cm 24cm is the only value entered till now.
24 in
The smallest so far is 24cm.
The largest so far is 60.96cm.
24 m
The smallest so far is 24cm.
The largest so far is 2400cm.
24 ft
As you can see in the above output, there is no output when the 'feet' unit is used. Another test's output:
D:\C++\testing\subtesting>program
24 ft
731.52cm is the only value entered till now.
24 ft
The smallest so far is 731.52cm.
The largest so far is 731.52cm.
200 ft
The smallest so far is 731.52cm.
The largest so far is 6096cm.
30 ft
There seems to be some sort of bug in the 'feet' code. Sometimes the code works and sometimes it doesn't. Can somebody help me?
Aucun commentaire:
Enregistrer un commentaire