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

Hello all can you please help so I double check the answer PART 1 a) Given the c

ID: 3806080 • Letter: H

Question

Hello all can you please help so I double check the answer

PART 1

a) Given the code fragment below, what does it print when the user enters 2 and then 3?
Could you explain why the result is not 5?
    // 0 to 9
    char digit1, digit2;
    cout<<"Enter a digit: ";
    cin>> digit1;
    cout<<"Enter another digit: ";
    cin>> digit2;
    cout<<"Sum of "<<digit1<<" + "<<digit2<<" is "<<(digit1 + digit2)<<endl;

b) Same as the question above but the code has been modified slightly. What is the output now when the user enters 2 and then 3?
    // 0 to 9
    string digit1, digit2;
    cout<<"Enter a digit: ";
    cin>> digit1;
    cout<<"Enter another digit: ";
    cin>> digit2;
    cout<<"Sum of "<<digit1<<" + "<<digit2<<" is "<<(digit1 + digit2)<<endl;

c) Same as the question above but the code has been modified slightly. What is the output now when the user enters 2 and then 3?
    // 0 to 9
    int digit1, digit2;
    cout<<"Enter a digit: ";
    cin>> digit1;
    cout<<"Enter another digit: ";
    cin>> digit2;
    cout<<"Sum of "<<digit1<<" + "<<digit2<<" is "<<(digit1 + digit2)<<endl;

d) From the examples above, when a user types a value like 2 or 3 as input, is that value a number, a character, or a string?
What determines what the program will treat the value as?

e) Same as question 3 but the user now enters "two" when asked for the first digit. What does the program print?
    // 0 to 9
    int digit1, digit2;
    cout<<"Enter a digit: ";
    cin>> digit1;
    cout<<"Enter another digit: ";
    cin>> digit2;
    cout<<"Sum of "<<digit1<<" + "<<digit2<<" is "<<(digit1 + digit2)<<endl;

f) Same as question 1 but the user now enters "two" when asked for the first digit. What does the program print?
    // 0 to 9
    char digit1, digit2;
    cout<<"Enter a digit: ";
    cin>> digit1;
    cout<<"Enter another digit: ";
    cin>> digit2;
    cout<<"Sum of "<<digit1<<" + "<<digit2<<" is "<<(digit1 + digit2)<<endl;

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

HI, I have answered first 4 part of questions.

Please repost others in separate post.

Please let me know in case of any issue in answered part.

a) Given the code fragment below, what does it print when the user enters 2 and then 3?
Could you explain why the result is not 5?
// 0 to 9
char digit1, digit2;
cout<<"Enter a digit: ";
cin>> digit1;
cout<<"Enter another digit: ";
cin>> digit2;
cout<<"Sum of "<<digit1<<" + "<<digit2<<" is "<<(digit1 + digit2)<<endl;

Output: Sum of 2 + 3 is 5

b) Same as the question above but the code has been modified slightly. What is the output now when the user enters 2 and then 3?
// 0 to 9
string digit1, digit2;
cout<<"Enter a digit: ";
cin>> digit1;
cout<<"Enter another digit: ";
cin>> digit2;
cout<<"Sum of "<<digit1<<" + "<<digit2<<" is "<<(digit1 + digit2)<<endl;

Output: Sum of 2 + 3 is 2 + 3

c) Same as the question above but the code has been modified slightly. What is the output now when the user enters 2 and then 3?
// 0 to 9
char digit1, digit2;
cout<<"Enter a digit: ";
cin>> digit1;
cout<<"Enter another digit: ";
cin>> digit2;
cout<<"Sum of "<<digit1<<" + "<<digit2<<" is "<<(digit1 + digit2)<<endl;

integer value of character 2 = (48+2) = 50 // 48 is for '0'
integer value of character 3 = (48+3) = 53 // 48 is for '0'
Output: Sum of 2 + 3 is 103

d) From the examples above, when a user types a value like 2 or 3 as input, is that value a number, a character, or a string?
What determines what the program will treat the value as?

   input is based on data type of variable.

   in part (a), the data type of digit1 and digit2 is integer(number)
   in part (b), the data type of digit1 and digit2 is string
   in part (c), the data type of digit1 and digit2 is character

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