Answer to Question #175265 in C++ for Rose

Question #175265

Problem 1(Star patterns): Write a program using loops to display the following patterns :

 

(i)

********** 

 

(ii)

********************

 

(iii)

*

**

***

****

*****

******

*******

********

*********

**********

 

(iv)

*

**

***

****

*****

******

*******

********

*********

**********

***********

************

*************

Problem 2 (Save and Get Info) : Write a program that asks for the user's name, phone number, and address. The program then saves all information in a data file (each information in one line) named list.txt. Finally, the program reads the information from the file and displays it on the screen  in the following format:



Name:         User's Name  

Phone Number: User's Phone Number  

Address:       User's Street Address 

               User's City, State, and Zip Code 


1
Expert's answer
2021-03-24T20:16:56-0400
#include <iostream>
#include <fstream>


using namespace std;


int main()
{
    for(int i = 0; i < 10; i++)
        cout << '*';
    cout << endl << endl;


    for(int i = 0; i < 20; i++)
        cout << '*';
    cout << endl << endl;


    for(int i = 0; i < 10; i++)
    {
        for(int j = 0; j < i+1; j++)
            cout << '*';


        cout << endl;
    }


    cout << endl << endl;
    for(int i = 0; i < 13; i++)
    {
        for(int j = 0; j < i+1; j++)
            cout << '*';


        cout << endl;
    }


    string name, phoneNumber, address;
    cout << endl << endl;


    cout << "Enter name: ";
    getline(cin, name);


    cout << "Enter phone number: ";
    getline(cin, phoneNumber);


    cout << "Enter address: ";
    getline(cin, address);


    ofstream output;
    output.open("list.txt", ios_base::app);
    output.close();


    ifstream input("list.txt", ios::ate);
    bool isEmpty = input.tellg() == 0;
    input.close();


    output.open("list.txt", ios_base::app);
    if(!isEmpty)
        output << endl;


    output << name << endl;
    output << phoneNumber << endl;
    output << address;


    output.close();


    cout << endl << endl;


    input.open("list.txt");
    while(!input.eof())
    {
        getline(input, name);
        cout << "Name: " << name << endl;


        getline(input, phoneNumber);
        cout << "Phone number: " << phoneNumber << endl;


        getline(input, address);
        cout << "Address: " << address << endl << endl;
    }
    input.close();


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS