5) Same as question 3 but the user now enters \"two\" when asked for the first d
ID: 3808735 • Letter: 5
Question
5) 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;
6) 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;
7) 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;
8) 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.
Explanation / Answer
5. The program prints:
Enter a digit: two
Enter another digit: Sum of 0 + 0 is 0
I have executed the code and this is the output
As we enter string instead of digits, it simply assigns null(0) to both the variables
6. Program prints:
Enter a digit: two
Enter another digit: Sum of t + w is 235
This is the output after execution
In this case digit1 and digit2 are declared chracter type, so when user enters two, the first character 't' is stored in digit1 and the second character 'w' is stored in digit2
The value 235 is the sum of ASCII value of t(116) + ASCII value of w(119) .
7. It prints:
Enter name: John Smith
Hello John
When we enter 'John Smith' as an input, cin stops the input when it encounters space(" "). Thus after reading John, cin terminates the input, and name stores the string John.
8. C++ Input/Output are based on streams, which are sequence of bytes flowing in and out of the programs.
In input operations, data bytes flow from an input source(such as keyboard, file, network or another program) into the program.
In output operations, data bytes flow from the program to an output sink(such as console, file, network or another program).
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.