Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that demonstrates the various ways in which C++ can get data int

ID: 3623389 • Letter: W

Question

Write a program that demonstrates the various ways in which C++ can get data into and out of programs according to the criteria described below. Declare the following variables, as described:

- Two int variables called i1 and i2.
- Three char variables called c1, c2, and c3.
- One double variable called d1;
- Six string variables called s1, s2, s3, s4, s5, and s6.
- One input file stream variable called inFile;
- One output file stream variable called outFile.

You may need other variables, declare them as required.

Your program should perform the following operations in the order given below (for all output statements, put an endl at the end):

Screen I/O

- Using the extraction operator, read the following sequence of characters from the keyboard: 497 x y 9756.321 CS110-001. Store the values read in i1, c1, c2, d1, and, s1, respectively.
- Using the insertion operator, write the values in i1, c1, c2, d1, and, s1 to the screen, all values on the same line, with adjacent values separated by a single space.
- Using cin and the stream member function get, read the following sequence of characters from the keyboard: abc. Store the values read in c1, c2, and c3, respectively.
- Using the insertion operator, write the values in c1, c2, and c3 to the screen, each value on a new line.
- Using the extraction operator, read the following sequence of characters from the keyboard: CS 11 0 Science. Store the values read in s1, i1, i2, and s2, respectively.
- Using the insertion operator, write the values in s1, i1, i2, and s2 to the screen, all values on the same line, with no spaces between adjacent values.
- Using the string member function length, calculate the length of s1 and store it in a variable of the appropriate data type.
- Using the string member function size, calculate the length of s2 and store it in a variable of the appropriate data type.
- Using the insertion operator, write the sum of the lengths of s1 and s2 to the screen.
- Using the cin and the getline function, read the following sequence of characters from the keyboard: CS 11 0 Science. Store the value read in s1.
- Using the insertion operator, write the value of s1 to the screen.
- Using the string member function find, determine the position of the substring enc in s1 and store it in a variable of the appropriate type.
- Using the insertion operator, write the position of the substring enc to the screen.
- Using the string member function find, determine the position of the substring nec in s1 and store it in a variable of the appropriate type.
- Using the insertion operator, write the position of the substring nec to the screen.

File I/O

- Using the file name myData1.txt that is associated with the ifstream variable inFile at compile time, open inFile for input.
- Using the extraction operator, read the following sequence of characters from the input file: myData2.txt. Store the value read in s1.
- Using the value stored in s1 as the file name that is associated with the ofstream variable outFile at run time, open outFile for output.
- Using the extraction operator, read the following sequence of characters from the input file: 497 x y 9756.321 CS110-001. Store the values read in i1, c1, c2, d1, and, s1, respectively.
- Using the insertion operator, write the values in i1, c1, c2, d1, and, s1 to the output file, each value on a new line.
- Using the input file stream variable and the stream member function get, read the following sequence of characters from the input file: abc. Store the values read in c1, c2, and c3, respectively.
- Using the insertion operator, write the values in c1, c2, and c3 to the output file, all values on the same line, with adjacent values separated by spaces.
- Using the extraction operator, read the following sequence of values from the input file: CS 11 0 Science. Store the values read in s1, i1, i2, and s2, respectively.
- Using the insertion operator, write the values in s1, i1, i2, and s2 to the output file, all values on the same line, with no spaces between adjacent values.
- Using the string member function swap, exchange the values stored in s1 and s2.
- Using the insertion operator, write the values in s1 and s2 to the output file, each value on a new line.
- Using the input file stream variable and the getline function, read the following sequence of characters from the input file: CS 11 0 Science. Store the value read in s1.
- Using the insertion operator, write the value in s1 to the output file.
- Using the string member function substr, extract the characters from s1 starting in position 10 for a length of 3 characters and store the value extracted in s2.
- Using the insertion operator, write the value in s2 to the output file.
- Close the input and output files.
- Using the file name myData1.txt that is associated with the ifstream variable inFile at compile time, open inFile for input.
- Using the extraction operator, read the following sequence of characters from the input file: myData2.txt. Store the value read in s1.
- Close the input file.
- Using the value stored in s1 as the file name that is associated with the ifstream variable inFile at run time, open the file for input.
- Using the extraction operator, read the following sequence of characters from the keyboard: myData3.txt. Store the value read in s1.
- Using the value stored in s1 as the file name that is associated with the ofstream variable outFile at run time, open the file for output.
- Using the input file stream variable and the getline function, read the first line of the input file. Store it in s1.
- Using the insertion operator, write the value in s1 to the output file.
- Using the input file stream variable and the getline function, read the second line of the input file. Store it in s2.
- Using the insertion operator, write the value in s2 to the output file.
- Using the input file stream variable and the getline function, read the third line of the input file. Store it in s3.
- Using the insertion operator, write the value in s3 to the output file.
- Using the input file stream variable and the getline function, read the fourth line of the input file. Store it in s4.
- Using the insertion operator, write the value in s4 to the output file.
- Using the input file stream variable and the getline function, read the fifth line of the input file. Store it in s5.
- Using the insertion operator, write the value in s5 to the output file.
- Using the input file stream variable and the getline function, read the sixth line of the input file. Store it in s6.
- Using the insertion operator, write the value in s6 to the output file.
- Using the insertion operator, write the values in s6, s5, s4, s3, s2, and s1 to the output file, all values on the same line.
- Close the input and output files.

The file called myData1.txt is provided for you and can be found in the directory ~hilder/cs110Science/assignment3/datafiles. Submit your source code and a script that captures the following activities: compiling your program, executing your program, and printing the files myData2.txt and myData3.txt.

Explanation / Answer

#include<iostream>

#include<fstream>

#include<string>

using namespace std;

void main()

{

int i1,i2;

char c1,c2,c3;

double d1;

string s1,s2,s3,s4,s5,s6;

ifstream inFile;

ofstream outFile;

cout<<"i1:";

cin>>i1;

cout<<"c1:";

cin>>c1;

cout<<"c2:";

cin>>c2;

cout<<"d1:";

cin>>d1;

cout<<"S1:";

cin>>s1;

//using cin and stream member function

cin.get(c1);

cin.get(c2);

cin.get(c3);

cout<<c1<<" "<<c2<<" "<<c3<<endl;

cout<<"Reading:";

cin>>s1>>i1>>i2>>s2;

cout<<s1<<i1<<i2<<s2<<endl;

//i2 gets length of string

i2=s1.length();

i1=s1.size();

cout<<"Sum of s1 and s2"<<i1+i2<<endl;

// cin.getline(s1,10);

cout<<s1;

s3="enc";

int pos=s1.find(s3,0);

cout<<"Position of enc:"<<pos<<endl;

s3="nec";

pos=s1.find(s3,0);

cout<<"Position of nec:"<<pos<<endl;

system("pause");

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote