Thursday, December 2, 2010

Practice Solution for C1

Write a program that print Good morning, Good afternoon, Good night in 3 separate lines. Tips, to go to next line, use endl. The output of the program should look like this.
Good morning
Good afternoon
Good night
The solution of the practice problem is below. If you type the keywords correctly, you should get the program to compile and finish in about 10 to 15 mins. The idea is to test if you know what does endl do. Test it out, try to see what happen if you don't add endl like we did in the solution. You will get everything on the same line.
Good morningGood afternoonGoodnight
So what you want to do is to add a space at the end of the each word, like below.
cout << "Good morning " << "Good afternoon " << " Good night " << endl; 
// output will be like this: Good morning Good afternoon Good night
Finally, here is the solution to the question above. If you have any question, please feel free to email me or post question in the comment section below. Happy coding! :)
/*
 *  csci135p1.cpp
 *  Solution to the practice, just make sure to type endl for the next line
 *
 */

#include <iostream>
using namespace std;

int main()
{
   cout << "Good morning" << endl;
   cout << "Good afternoon" << endl;
   cout << "Good night" << endl;
   return 0;
}

No comments:

Post a Comment