For lab 12, you need to request a date from the user, in the format "mm-dd-yyyy". This is a string but you will need to parse it into its component integers. This is how you go about doing that.
Showing posts with label tutorials. Show all posts
Showing posts with label tutorials. Show all posts
Monday, November 7, 2011
File access and user input
Usually you don't want to hard-code filenames into your projects: you want to let the user decide what the filenames should be. Here is an easy way to do just that.
Sunday, October 23, 2011
Whitespace... it's free!
I was reviewing today's batch of source code submissions and I noticed that an awful lot of people were not making good use of their whitespace. Please observe the following rules in your code:Friday, October 14, 2011
Newlines
It isn't necessary to write multiple cout statements when you want a line break. This code
cout << endl;
cout << "Your random # is " << num << "." << endl;
cout << endl;
could be rewritten using the newline character, \n
cout << "\nYour random # is " << num << ".\n"
<< endl;
These two code snippets generate the same output:
cout << endl;
cout << "Your random # is " << num << "." << endl;
cout << endl;
could be rewritten using the newline character, \n
cout << "\nYour random # is " << num << ".\n"
<< endl;
These two code snippets generate the same output:
Monday, October 10, 2011
Enhanced telephone digits
Example 5-5 in the textbook (pp 241-242) asks the user to enter an uppercase character and returns the corresponding number from the telephone keypad. If you would prefer to make the code case-insensitive, you could add the following to the program in the book.
rand() and srand()
Sometimes you need to generate a random number. In C++, the easiest way to do this is to invoke the rand() function from the cmath library. This method returns a value from [0 - 32767] but you can use a little mathematical
Here are some examples and what they return:
Here are some examples and what they return:
Wednesday, October 5, 2011
Just say no to "magic numbers"
It is unacceptable to use "magic numbers" in your source code. When dealing with trivial programs, using a naked number doesn't look like such a big deal (the code is short and, if well commented, the intent of the programmer is clear) but it is still a very bad habit to develop. The problem is both clearer to see and greatly magnified once the size of the code in question is larger.
Subscribe to:
Posts (Atom)