The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 304 Weeks Ago, 1 Day Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
c++ programingÂ
Â
1) 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;
Â
2) 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;
Â
3) 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;
Â
4) 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?
Â
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.
Â
9) 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.
Â
10) Give a code fragment that attempts to open a file "myFile.txt" and terminates the program if the file was not opened successfully.
----------- Â ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly