Saturday, 28 January 2017

Object-Oriented Programming in C++ Fourth Edition By Robert Lafore Chapter-4 C++ Structures -- Questions 1

Leave a Comment

Question:

A phone number, such as (212) 767-8900, can be thought of as having three parts: the area code (212), the exchange (767), and the number (8900). Write a program that uses a structure to store these three parts of a phone number separately. Call the structure phone. Create two structure variables of type phone. Initialize one, and have the user input a number for the other one. Then display both numbers. The interchange might look like this:

Enter your area code, exchange, and number: 415 555 1212
My number is (212) 767-8900
Your number is (415) 555-1212

Explanation:

We strongly recommend you to minimize your browser and try this yourself first.


Below mention code is compiled in Visual Studio 2015 and Code Blocks 13.12,output snap is attached.. If any problem you feel and you want some explanation feel free to contact us.



Code:

/**************************************************|
/*************C++ Programs And Projects************|
***************************************************/
// uses structure to store phone number
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
struct phone
{
       int area;         //area code (3 digits)
       int exchange;     //exchange (3 digits)
       int number;       //number (4 digits)
};
////////////////////////////////////////////////////////////////
int main()
{
       phone ph1 = { 212, 767, 8900 };  //initialize phone number
       phone ph2;                       //define phone number
                                                               // get phone no from user
       cout << "\nEnter your area code, exchange, and number";
       cout << "\n(Don’t use leading zeros) : ";
       cin >> ph2.area >> ph2.exchange >> ph2.number;
       cout << "\nMy number is "        //display numbers
              << '(' << ph1.area << ") "
              << ph1.exchange << ' - ' << ph1.number;
       cout << "\nYour number is "
              << '(' << ph2.area << ") "
              << ph2.exchange << ' - ' << ph2.number << endl;
       return 0;
}

Output:
Object-Oriented Programming in C++ Fourth Edition By Robert Lafore Chapter-4 C++ Structures  -- Questions 1



Related Articles:


If You Enjoyed This, Take 5 Seconds To Share It