Question

12. Comparing Estimates This is a C++ question This exercise will test your knowledge of passing...

12. Comparing Estimates

This is a C++ question

This exercise will test your knowledge of passing structures to functions. Create a structure that stores information about various plumbers that you are getting repair estimates from. As you can see from the output, the structure must store the name, phone number, and estimate. Since you are only getting estimates from two companies, only two structure variables are needed.

Functions:

1) Get data from the user. This function must be called TWICE.

2) Print the summary you see in the last line of output. The formula is simple: Check which company has the lower estimate and output their name and the difference in price. If they gave the same estimate, then output a message stating so.

Output:

This program will compare estimates from two different plumbers.

Enter the company/plumber name: [user types: ABC Corp]

Enter the phone number: [user types: 305-555-1111]

Enter the estimate received: [user types: 90]

Enter the company/plumber name: [user types: XYZ Corp]

Enter the phone number: [user types: 305-555-2222]

Enter the estimate received: [user types: 99.50]

ABC Corp is cheaper by $9.50

(Could also have been this: "Both plumbers gave the same estimate. Go with either.")

Notes and Hints:

1) Since the company / plumber name can have a space in it, you must use GETLINE for this piece of input. Important: Remember to use std::cin.ignore() near the end of the function. Otherwise, the next time the function is called, getline will read the leftover newline from the previous input.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The logic is described by comments in the code at every required line.

The complete code:
#include <iostream>
#include <string>

using namespace std;

// define structure to store plumber/company information
struct PlumberInfo
{
string name; // plumber or company name
string phoneNumber; // phone number
double estimate; // estimate given
};

// function to get data for a company or plumber
void GetPlumberInfo(PlumberInfo &pi)
{
// prompt user to enter company or plumber name
cout << "Enter the company/plumber name: ";
// using getline as name can have spaces in between
getline(cin, pi.name);
  
// prompt user to enter company or plumber phone number
cout << "Enter the phone number: ";
cin >> pi.phoneNumber;
  
// prompt user to enter the estimate
cout << "Enter the estimate received: ";
cin >> pi.estimate;
  
// to ignore or flush the leftover newline character from the previous input
cin.ignore();
}

// function compares two estimates and prints summary based on the estimates
void compareEstimates(PlumberInfo &plumber1, PlumberInfo &plumber2)
{
// plumber estimates are equal
if(plumber1.estimate == plumber2.estimate)
{
cout << "Both plumbers gave the same estimate. Go with either.";
}
// first plumber has cheaper estimate
else if(plumber1.estimate < plumber2.estimate)
{
cout << plumber1.name << " is cheaper by " << (plumber2.estimate - plumber1.estimate);
}
// second plumber has cheaper estimate
else
{
cout << plumber2.name << " is cheaper by " << (plumber1.estimate - plumber2.estimate);
}
}

int main()
{
// define 2 variables for 2 plumber or company
PlumberInfo plumber1;
PlumberInfo plumber2;
  
// get first plumber/company information from user
GetPlumberInfo(plumber1);
  
// get second plumber/company information from user
GetPlumberInfo(plumber2);
  
// comapare the estimates and print the summary
compareEstimates(plumber1, plumber2);

return 0;
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
12. Comparing Estimates This is a C++ question This exercise will test your knowledge of passing...
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
  • Using C++ programming. This exercise will test your knowledge of passing structures to functions. Create a...

    Using C++ programming. This exercise will test your knowledge of passing structures to functions. Create a structure that stores information about various plumbers that you are getting repair estimates from. As you can see from the output, the structure must store the name, phone number, and estimate. Since you are only getting estimates from two companies, only two structure variables are needed. Functions: 1) Get data from the user. This function must be called TWICE. 2) Print the summary you...

  • in C++ Description The purpose of this challenge is to manually manipulate character arrays. This challenge...

    in C++ Description The purpose of this challenge is to manually manipulate character arrays. This challenge converts text in phone numbers to their equivalent values on a phone keypad. Requirements Write a program to decode phone numbers with letters in them. Create a void function decodetext(char after[], char before[]). This function should be case-insensitive. Do NOT cout in this function. Declare a char array to hold a maximum of 50 characters. Ask the user to enter a string. Use cin.getline(char_variable,...

  • (In C) 8.12 LAB: Warm up: Contacts You will be building a linked list. Make sure...

    (In C) 8.12 LAB: Warm up: Contacts You will be building a linked list. Make sure to keep track of both the head and tail nodes. (1) Create three files to submit. ContactNode.h - Struct definition, including the data members and related function declarations ContactNode.c - Related function definitions main.c - main() function (2) Build the ContactNode struct per the following specifications: Data members char contactName[50] char contactPhoneNum[50] struct ContactNode* nextNodePtr Related functions CreateContactNode() (2 pt) InsertContactAfter() (2 pts) Insert...

  • Hello I need some help with my CC+ project. My current project erasing the first two...

    Hello I need some help with my CC+ project. My current project erasing the first two phone numbers and only printing the 3rd phone number out in the list. Any idea on how to correct. Here is the error. Contacts.h #ifndef Contact_H #define Contact_H #include<string> using namespace std; class ContactNode{ public: ContactNode(); ContactNode(string name, string phone); void InsertAfter(ContactNode*); string GetName(); string GetPhoneNumber(); ContactNode* GetNext(); void PrintContactNode(); private: string contactName; string contactPhoneNum; ContactNode* nextNodePtr; }; #endif Contacts.cpp #include <iostream> #include "Contacts.h"...

  • Using C++ programming Write a program that compares a student’s answers in a 5-question quiz to...

    Using C++ programming Write a program that compares a student’s answers in a 5-question quiz to the teacher’s answer key. The answer key is: {'C', 'D', 'B', 'B', 'A'} You MUST use TWO functions: One for input and the other for comparing/output. The output is simply the # of questions answered correctly. This program will test your ability to handle parallel arrays. Output: Look at the questions and choices on page 999 of your textbook. Enter the UPPERCASE letter of...

  • This is a C++ question we do not use namespace std at the beginning of the...

    This is a C++ question we do not use namespace std at the beginning of the program 9. Arrays: Functions and Reading From File Although this code is a little long, the strategy is straightforward and it will really help you practice functions. I have provided a large amount of detail to help you out. In a nutshell, you're going to read some numbers from a file (into an array) and then ask the user for a number that will...

  • This C++ Programs should be written in Visual studio 2017 PROGRAM 1: Comparing Using Arrays to...

    This C++ Programs should be written in Visual studio 2017 PROGRAM 1: Comparing Using Arrays to Using Individual Variables One of the main advantages of using arrays instead of individual variables to store values is that the program code becomes much smaller. Write two versions of a program, one using arrays to hold the input values, and one using individual variables to hold the input values. The programs should ask the user to enter 10 integer values, and then it...

  • Write this in a C program please. Structures on Disk The Problem Your task is to...

    Write this in a C program please. Structures on Disk The Problem Your task is to write a program that stores and retrieves structures in a file on disk. The file of structures must remain on the disk once your program ends. You should be able to store structures to the file and retrieve from the file after restarting the program. The record that you will be writing to file has the following structure: struct contact {unsigned long phone_number; long...

  • This is a c++ question we are not using namespace std at the top Subtraction +...

    This is a c++ question we are not using namespace std at the top Subtraction + Decision and Loop This is a twist on the previous exercise that will help you review loops and decision structures. You will again ask the user to enter two numbers. However, you will ALWAYS subtract the smaller number from the larger number to ensure that you never get a negative number for an answer. You do this by checking the numbers and switching them...

  • 8. (15 marks) Write a complete C program, which uses an array of structures and two...

    8. (15 marks) Write a complete C program, which uses an array of structures and two user defined functions. The program deals with a library database. The program will ask the user to input required information about each book and store it in a structure in a user defined function called get info. The second user defined function display_info will display database information on the screen. The output should look as follows: Book Title Book ld 123456 C Programming 10...

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