Question

I am using C++ Start with the distance.cpp file on the website. Fill in the missing...

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. I also CANNOT change anything about int main, i cant add anything to it or do anything to change it. I also need to use function overload.

#include <iostream>

using namespace std;

double strToDouble(string s);

int main()
{
        Distance d1, d2;
        cout << "Enter two distances: \n";
        cin >> d1 >> d2;
        
        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;
        for(int i=0; i < s.length(); i++)
        {
                if(s[i] == '.')
                {
                        before= false;
                        continue;
                }
                
                if(!before)
                {
                        expo*=10;
                }
                current *= 10;
                current += s[i]-'0';
        }
        
        return current/expo;    
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer: Program: #include <iostream> #inc lude <string> из ing name 3p a ce sta; double strToDouble (string s); double converif (d1 < d2) cout <First distance is smaller << endl else if (d1 d2) cout Same distances << endl else cout < First distanc

Copyable Code:

#include <iostream>
#include <string>

using namespace std;

double strToDouble(string s);
double convert(string s);

int main()
{
string a, b;
double d1, d2;
  
cout << "Enter two distances: \n";
cin >> a >> b;
  


if(a.find("'") <10)
d1 = convert(a);
else
d1 = strToDouble(a);
  
if(b.find("'") <10)
d2 = convert(b);
else
d2 = strToDouble(b);
  

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;
for(int i=0; i < s.length(); i++)
{
if(s[i] == '.')
{
before= false;
continue;
}
  
if(!before)
{
expo*=10;
}
current *= 10;
current += s[i]-'0';
}
  
return current/expo;
}

double convert(string s)
{
string temp;
  
temp = s.replace(s.find("'"), 1, "");
  
double d = strToDouble(temp);
  
return (d * 0.305);
}

Add a comment
Know the answer?
Add Answer to:
I am using C++ Start with the distance.cpp file on the website. Fill in the missing...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ////****what am i doing wrong? im trying to have this program with constructors convert inches to...

    ////****what am i doing wrong? im trying to have this program with constructors convert inches to feet too I don't know what im doing wrong. (the program is suppose to take to sets of numbers feet and inches and compare them to see if they are equal , greater, less than, or not equal using operator overload #include <stdio.h> #include <string.h> #include <iostream> #include <iomanip> #include <cmath> using namespace std; //class declaration class FeetInches { private:    int feet;   ...

  • #include <iostream> #include <string> #include <stdio.h> using namespace std; /** The FeetInches class holds distances measured...

    #include <iostream> #include <string> #include <stdio.h> using namespace std; /** The FeetInches class holds distances measured in feet and inches. */ class FeetInches { private: int feet; // The number of feet int inches; // The number of inches /** The simplify method adjusts the values in feet and inches to conform to a standard measurement. */ void simplify() { if (inches > 11) { feet = feet + (inches / 12); inches = inches % 12; } } /**...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • i am getting the warning warning: unknown escape sequence: '\s' and warning: unknown escape sequence: '\C'...

    i am getting the warning warning: unknown escape sequence: '\s' and warning: unknown escape sequence: '\C' here is my code #include <iostream> #include <fstream> using namespace std; int main() { ifstream infile; infile.open("C:\\college\\C++\\scores.txt"); if(!infile) {   cout<<"could not open"; return 0; else string p; string t; int s; string player[6]; string team[6]; int score[6]; while(infile>>p>>t>>s) { for(int i=0;i<6;i++) {    p=player[i];    t=team[i];    s=score[i];    } } int scoreb=0,scorew=0; for(int i=0;i<6;i++) { if(team[i]=="Blue")    scoreb=scoreb+score[i]; else if(team[i]=="White")    scorew=scorew+score[i]; }...

  • I need to add something to this C++ program.Additionally I want it to remove 10 words...

    I need to add something to this C++ program.Additionally I want it to remove 10 words from the printing list (Ancient,Europe,Asia,America,North,South,West ,East,Arctica,Greenland) #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int> words);    int main() { // Declaring variables std::map<std::string,int> words;       //defines an input stream for the data file ifstream dataIn;    string infile; cout<<"Please enter a File Name :"; cin>>infile; readFile(infile,words);...

  • Am I getting this error because i declared 'n' as an int, and then asking it...

    Am I getting this error because i declared 'n' as an int, and then asking it to make it a double? This is the coude: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <vector> using namespace std; void sort(double grades[], int size); char calGrade(double); int main() {    int n;    double avg, sum = 0;;    string in_file, out_file;    cout << "Please enter the name of the input file: ";    cin >> in_file;   ...

  • I need to make a few changes to this C++ program,first of all it should read...

    I need to make a few changes to this C++ program,first of all it should read the file from the computer without asking the user for the name of it.The name of the file is MichaelJordan.dat, second of all it should print ,3 highest frequencies are: 3 words that occure the most.everything else is good. #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int>...

  • I am having trouble understanding how this code is able to use the contents of the...

    I am having trouble understanding how this code is able to use the contents of the header file. Can someone please provide comments in the main code to describe what is happening? (especially on the bool isNumber) THE MAIN CODE: #include<bits/stdc++.h> #include "MyCartesianPoint.h" #include <math.h> #include <iostream> using namespace std; bool isNumber(string s) {    if(!isdigit (s[0]))    {        if(s[0] != '-')        return false;               else if(s.length() == 1)        return false;...

  • You are given a Q1.h file with overloaded function prototypes for isOrdered. Implement this overloaded function...

    You are given a Q1.h file with overloaded function prototypes for isOrdered. Implement this overloaded function into a file named Q1.cpp. Q1.cpp should only include your function implementation, the necessary #include directives if needed, and should not contain anything else such as the main function or global variable declarations. Test your code using a separate main.cpp file where you implement a sufficient number of test cases. You should use Q1.h as a header file and Q1main.cpp as a main function...

  • Hey, so i am trying to have my program read a text file using a structure...

    Hey, so i am trying to have my program read a text file using a structure but i also want to be able to modify the results(I kinda have this part but it could be better). I cant seem to get it to read the file(all the values come up as 0 and i'm not sure why because in my other program where it wrote to the txt file the values are on the txt file) i copied and pasted...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT