In: Computer Science
in c++
QUESTION 4:
Write the code to do the following:
-read input from the keyboard by displaying the message on the screen to ask and read the following information:
* customer ID (string)
* customer name (string)
* balance (float)
-open output file customer .txt to write
-Write to the output file customer.txt the following information:
Customer ID – customer name – balance
For example:
1561175753 - James Smith – 1255.25
-close file
QUESTION 5:
-create one notepad file named as data.txt and type the following lines:
1112243433 - Mary Lane – 1250.366
2123312344 – John Smith – 2134.25
1561175753 - James Smith – 1255.25
-provide the C++ code to open above file data.txt
-Read each line of the file then display on screen
-continue reading and displaying on the screen all the lines until end of the file
-Write: “End of the file data.txt” on the screen
-close data.txt file
Given code is in C++:
Q4.)
#include <iostream>
#include <stdio.h>
#include <string>
#include <fstream> //this header is used whenever we want to work with files.
using namespace std;
int main()
{
string id, name, surname; //declaring id, name and surname strings.
float balance; //declaring the balance variable.
cout<<"Enter the customer ID: "; //taking inputs for the id, name, surname and balance.
cin>>id;
cout << "Enter the customer name: ";
cin >> name >>surname;
cout << "Enter the balance: ";
cin >> balance;
ofstream myfile("customer.txt"); //create and open the customer.txt file
myfile<<id<<"-"<<name<<" "<<surname<<"-"<<balance; //writing to the file.
myfile.close(); //closing the file after writing to it.
return 0; //terminating the program.
}
Output:
customer.txt:
Q5.)
#include <iostream>
#include <fstream> //this header is used whenever we want to work with files.
#include <stdio.h>
using namespace std;
int main()
{
string text; //declaring a text string.
ifstream readfile("data.txt"); //opening the file data.txt into readfile.
while(getline(readfile, text)) //reading each line of the file into text string.
{
cout<<text<<endl; //printing out the text string.
}
cout<<"End of the file data.txt";
readfile.close(); //closing the file after reading it.
return 0; //terminating the program.
}
Output:
Kindly like if this helps :)