I am using C++ Start with the distance.cpp file on the website. Fill in the miss
ID: 3940745 • Letter: I
Question
I am using C++
Start with the distance.cpp file on the website. Fill in the missing functions to enable the program to compile and correctly find determine which entered distance is larger. The user can either enter it in meters labeled with an “m”, or in feet and inches labeled with and ' and “. You may not change main(), we will check to ensure it is exactly the same. The meters and inches can be doubles, but the feet portion will always be in integers. Use the conversion that 1 foot = 0.305 meters. this is distance.cpp
Explanation / Answer
You need call the conversion function and take input in main ..so I changed main file without changing the code which was there..complete working program is as below. I changed strToDouble with existing code to convert feet and inches to meter.
------------------------------------------------------------
#include <iostream>
#include<string>
#include<iomanip>
using namespace std;
//define constant values
const double foot_meter = 0.305;
const double inches_meter = 39.37;
double strToDouble(string s);
typedef double Distance;
int main()
{
Distance d1, d2;
//take input distance as string and then convert distance to double using function strToDouble
string s1, s2;
cout<<"This program checks which entered distance is larger "<<endl;
cout<<"The user can either enter distances in meters labeled with an “m”, or in feet and inches labeled with and ' and “."<<endl;
//take input for firest distancce
cout<<"Enter d1 = ";
cin>>s1;
//take input for second distancce
cout<<"Enter d2 = ";
cin>>s2;
//call strToDouble to convert to double
d1 = strToDouble(s1);
d2 = strToDouble(s2);
//output d1 and d2
cout<<"Distance d1 = "<<d1<<endl;
cout<<"Distance d2 = "<<d2<<endl;
if(d1 < d2)
{
cout << "First distance is smaller" << endl;
}
else if(d1 == d2)
{
cout << "Same distances" << endl;
}
else
{
cout << "First distance is larger" << endl;
}
}
double strToDouble(string s)
{
double expo = 1;
double current = 0;
bool before = true;
bool foot = false ;
int feet , inch;
for(int i=0; i < s.length(); i++)
{
if( s[i] == 'm')
{
continue;
}
if(s[i] == '.')
{
before= false;
continue;
}
else
{
if( s[i] == ''' )
{
foot = true;
feet = current;
continue;
}
if( s[i] == '"')
{
foot = true;
inch = (int)current%(int)expo;
continue;
}
if(!before || foot)
{
expo*=10;
}
current *= 10;
current += s[i]-'0';
}
}
if ( foot )
{
current = feet * foot_meter + inch /inches_meter ;
return current;
}
return current/expo;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.