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
#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;
}
Comments
Leave a comment