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

PART 1 (continued from another question) Reposted these parts because they could

ID: 3806951 • Letter: P

Question

PART 1 (continued from another question)

Reposted these parts because they couldn't be answered from the previous question.

g) Given the code fragment below, what does it print when the user enters John Smith? Please briefly explain why this happens.
    string name;
    cout<<"Enter name: ";
    cin>> name;
    cout<<"Hello "<<name<<endl;

h) Please briefly explain (~3 lines) how reading from a file is similar to reading from cin and writing to a file is similar to writing to cout.

i) Give an example (~2 lines) of when you may want to read a file character by character as opposed to word by word. Give an example (~2 lines) when it would be more appropriate to read the file word by word.

j) Give a code fragment that attempts to open a file "myFile.txt" and terminates the program if the file was not opened successfully.

Explanation / Answer

g) Given the code fragment below, what does it print when the user enters John Smith? Please briefly explain why this happens.

Answer: Here in the given code the variable name is declared as string datatype, this string datatype always consider letters before space only then after it ignore enire part after the space.When the user enters John Smith it ignores the data after Space i.e " Smith".so now the String value in the variable name is John only.So it prints the message Hello John for the following program.

Program:

#include <iostream>

using namespace std;

int main() {
   string name;
cout<<"Enter name: ";
cin>> name;
cout<<"Hello "<<name<<endl;
   return 0;
}

Output:

Enter name: John Smith

Hello John

Suppose if we want to print entire part we should initializes the value of string variable name

Program:

#include <iostream>

using namespace std;

int main() {
   string name="John smith";
//cout<<"Enter name: ";
//cin>> name;
cout<<"Hello "<<name<<endl;
   return 0;
}

Output:

Hello John smith

h) Please briefly explain (~3 lines) how reading from a file is similar to reading from cin and writing to a file is similar to writing to cout.

Answer:

cin is the "standard input stream" in C++ programming

In most program environments, the standard input by default is the keyboard.

This cin is used together with the extraction operator which is written as >>

string name;
   cin>> name;

i) Give an example (~2 lines) of when you may want to read a file character by character as opposed to word by word. Give an example (~2 lines) when it would be more appropriate to read the file word by word.

Answer:

cout is the "standard output stream"

cout is used together with the insertion operator, which is written as <<

Example:

cout << "This is the sample output"; // prints This is the sample output on screen

j) Give a code fragment that attempts to open a file "myFile.txt" and terminates the program if the file was not opened successfully.

Answer:

#include <iostream>

#include <fstream>
using namespace std;

int main () {
open ("myFile.txt");
return 0;
}