Question

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 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

In this C++ Program,

- We have defined a structure named Company which contains member variables:
1. string name;
2. string phone_number;
3. double estimate;

- We have defined a function named getDetails to accept input from the user to initials the structure variables.

- In main, we have declared 2 structure objects called c1 and c2 and calling the getDetails() to initialize those structure objects.

- Then we are comparing the estimate of both the structure objects and printing the respective message to the console.

(I believe that the logic which I used is simple and easy, If you still have any query, Feel free to drop me a comment below)

Program:

#include <iostream>
using namespace std;

//Company structure declaration
struct Company
{
string name;
string phone_number;
double estimate;
};

//Function prototype
Company getDetails();

//Function getDetails() definition
Company getDetails()
{
Company c;//delared structure object
  
//Prompting the user to enter details
cout<<"Enter the company/plumber name: ";
std::getline(cin,c.name);
cout<<"Enter the phone number: ";
cin>>c.phone_number;
cout<<"Enter the estimate received: ";
cin>>c.estimate;
std::cin.ignore();
//Returning the structure object
return c;
}
int main()
{
//Created 2 structure objects of Company
Company c1,c2;
  
//Calling the getDetails() to get the data from the user
c1=getDetails();
c2=getDetails();
  
//Company1's estimate is less
if(c1.estimate<c2.estimate)
{
cout<<c1.name<<" is cheaper by $"<<(c2.estimate)-(c1.estimate);
}
//Company2's estimate is less
else if(c1.estimate>c2.estimate)
{
cout<<c2.name<<" is cheaper by $"<<(c1.estimate)-(c2.estimate);
}
//Both estiamtes are equal
else
{
cout<<"Both plumbers gave the same estimate. Go with either.";
}
return 0;
}
main.cpp #include <iostream> 2 using namespace std; 4 //Company structure declaration struct Company string name; string phon

//Company2s estimate is less else if(c1.estimate c2.estimate) cout<<c2.name<< is cheaper by $<<(c1.estimate)-(c2.estimate)

Output:

Enter the company/plumber name: ABC Corp Enter the phone number: 305-555-1111 Enter the estimate received: 90 Enter the compa

Hope this Helps!!!

Please do upvote as well if you got the answer ?

If not please comment, I will Help you with that..

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

  • 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,...

  • 20.6 Lab: 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-Class declaration ContactNode.cpp- Class...

    20.6 Lab: 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-Class declaration ContactNode.cpp- Class definition main.cpp-main0 function (2) Build the ContactNode class per the following specifications Parameterized constructor. Parameters are name followed by phone number Public member functions InsertAfter0 (2 pts) GetName0 -Accessor(1 pt) GetPhoneNumber- Accessor (1 pt) GetNext0-Accessor (1 pt) PrintContactNode Private data members string contactName string contactPhoneNum ContactNode* nextNodePtr Ex....

  • 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...

  • (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...

  • In this program, you will be using C++ programming constructs, such as overloaded functions. Write a...

    In this program, you will be using C++ programming constructs, such as overloaded functions. Write a program that requests 3 integers from the user and displays the largest one entered. Your program will then request 3 characters from the user and display the largest character entered. If the user enters a non-alphabetic character, your program will display an error message. You must fill in the body of main() to prompt the user for input, and call the overloaded function showBiggest()...

  • 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"...

  • In this program, you will be using C++ programming constructs, such as functions. main.cpp Write a...

    In this program, you will be using C++ programming constructs, such as functions. main.cpp Write a program that asks the user to enter an integer value. Your program will then display that integer back to the user. Your program should include a function called getInteger that requests an integer value from the user and returns that value back to the caller. Your main () function will call the function getInteger and will then display the value returned by that function....

  • 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...

  • Programming Assignment 1 Structures, arrays of structures, functions, header files, multiple code files Program description: Read...

    Programming Assignment 1 Structures, arrays of structures, functions, header files, multiple code files Program description: Read and process a file containing customer purchase data for books. The books available for purchase will be read from a separate data file. Process the customer sales and produce a report of the sales and the remaining book inventory. You are to read a data file (customerList.txt, provided) containing customer book purchasing data. Create a structure to contain the information. The structure will contain...

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