|
C M S C 2 1 4 C o m p u t e r S c i e n c e I I F a l l 2 0 0 2 |
Yes.
The string length function returns an unsigned integer so you are
probably trying to assign an unsigned integer to a signed (the default
type) integer. You can do one of 2 things, have your value holding length
be unsigned:
No, we won't take off for info messages, but you should fix them anyway
as they mean you are violating the strongly typed conventions of C++.
Here are the following rule we used for defining palindromes.
Some students have asked "Doesn't it make sense to call the
empty string a palindrome?" and the answer is yes, it
does make sense, but in this project we DEFINE it NOT to be a
palindrome. Sometimes this happens. A definition may not meet your
intuition or perhaps even the official definition, but is the one
being used in a project. (For example, non-alphanumeric non-white
space isn't exactly a good definition for punctuation, but it is
convenient to define it that way).
There are two errors students have encountered. One has to do with
the fact that length() and size() return unsigned
int (for nearly every STL class that uses these methods--but in
this case, for strings), and the other is that toupper() and
tolower() return int rather than char.
If you use length() or size() methods on string
objects, in conjunction with looping variables of type int, you will
run into a problem. length() and size() return
unsigned int. Compilers complain that int and
unsigned int are not compatible types. To solve, use static
casting.
The other problem stems from toupper and tolower. These
functions return int, instead of char, so it complains that you are
losing some data by casting from a large type (an int, which contains
at least 4 bytes) to a smaller type (a char, which contains 1 byte).
Again, static cast to remove the error.
Yes. You want to be careful when using C++ strings. For example,
although it happens to be permissible to modify the characters of
a C++ string, you generally use concatentation (the + operator) to
increase the size of the string (there are other ways).
Here are two useful functions you can use:
Yes, it does. However, it is the standalone (non member function)
version. The prototype looks like:
(Note: this information comes courtesy of The C++ Standard Library:
A Tutorial and Reference by Nicolai Josuttis).
For the purposes of this project you should consider any non-space,
non-alphanumeric character punctuation.
It turns out that it is a directory that the compiler creates when
compiling your programs. You do *not* need to keep this directory
and you can remove it if you want - just delete all of the files
inside of it and then delete it. Note however that the compiler will
recreate it from time to time.
No, since you are using C++ string functions.
If you are logging in with a secure connection (like ssh - which you
should be) then you need to authenticate your session first by using
the dce_login command (enter your username and then password at
the first and second prompts) and then you should be able to change
your password using the passwd command (note: after doing this
you may need to enter the Unix exit command before you can
logout). If you continue to have problems you should go see
someone in office hours. If you forget/lose your password you need
to email your instructor - remember that per the guidelines on the
syllabus you must put your full name, login ID, section number and if
it is regarding your password you must include your student ID number
as well.
Yes! But be prepared to write the code yourself in testing situations.
Because some of the methods in Palindrome are const, your iterators
must be const. So instead of defining iterator, use const_iterator or
const_reverse_iterator.
Some of these concepts were covered today (1/30/02) in lab section
as well as handouts were provided (in lab section). Note that if
you do not show up to lab section you will not be able to get copies
of any handouts or other material provided in that lab section. If you
have a valid excuse (according to the syllabus' definition of what
constitutes a valid excuse) you will be able to get a copy of any
handouts for the day/lab in question from the head grading TA during
any of their posted office hours.
Class accounts can only be given out to students officially registered
in the course and can only be given out in person either during class
or lab (the one for which you are registered) or in the TA's (who teaches
your lab section) office hours (which start next week - see the class
webpage for further details regarding office hours).
Note that you can actually start practicing your Unix and programming
skills even if you do not have an official class account - WAM accounts
(which all students should have - if not go to www.wam.umd.edu to find
out how to get one) are very similar to the class accounts that we
provide in that they are Unix systems. The one main difference is that
they do not have the cxx compiler, but they do have the
g++ compiler which is very similar.
Accommodations will be made for students who do not get into the class
until late next week, but it is expected that students who are already
registered in the class received their class accounts today (1/30/02)
and should have plenty of time to complete project #0 (which is
considered comparable to a homework assignment - had there been homework
assignments).
A FAQ is a list of Frequently Asked Questions
and in particular the FAQ's for projects in this class will have
questions listed in reverse chronological order (meaning the most
recent question will appear at the top of the FAQ list and the
first question asked/answered will appear at the bottom of the
list).
Note that the FAQ's that are posted are usually ones that the
instructional staff have created in anticipation of potential
questions and ones that have been encountered frequently by the
instructional staff while assisting students in office hours.
Per the policies outlined on the syllabus you should see one of
the instructional staff during office hours regarding all questions
on projects - this includes project #0. This means that you will
have to wait until Monday morning prior to getting assistance on
this project. Note that any email inquiries concerning projects
may go unanswered (per the policies outlined on the class syllabus).
Also you should always read the corresponding FAQ prior to asking a
question on a project and you should generally ask no questions the
first day that a project is posted - rather you should thoroughly
read over the project description and think about it for a while.
Project #0 is due by 11pm on Thursday, September 12th, 2002. Note:
any project may be turned in late up to 2 days as specified on the
class syllabus. For details regarding the late policy and any
penalties associated with submitting late projects please see the
class syllabus.
string str;
unsigned int n = str.length();
or use the static cast described under the warnings question.
A palindrome must consist of at least one character (after
removing punctuation and white space).
Each line ending in a new line is processed by your program
(using getline() to read in the line). If the line
consists of only blanks or some combination of blanks and
punctuation, this will eventually become the empty string,
and is therefore not considered a palindrome.
// WRONG! size() returns unsigned int---looping variable is int! TYPE MISMATCH!
string str = "foo";
for ( int i = 0; i < str.size(); i++ )
cout << str[ i ] << endl;
// BETTER : static cast
for ( int i = 0; i < static_cast<int>( str.size() ); i++ )
cout << str[ i ] << endl;
The syntax for static cast is:
static_cast
where TYPE is replaced with the new type, and stuff
is replaced by the expression whose type you are casting. A decent
book on C++ should explain static casting. Read it!
char ch = 'a';
// toupper, unusually enough, returns an int. The static cast
// converts it back to a char.
char ch2 = static_cast<char>( toupper( ch ) );
Note: static casts are preferred over the old style casts use in C
(which placed the type in parentheses) since it's considered "safer".
So, even though it's more verbose, you should use the longer version.
char ch = 'A';
string str( 1, ch ); // constructor to convert character to string
This constructor takes two arguments. The first is an int, which
is the number of repetition. The second is a char, which is
what you want to convert to a string. By using 1 for the first argument,
you can convert characters to string. You can also use:
string( 1, ch ); // constructor to convert character to string without declaring variable
if you don't want to declare a variable, for example, you can use it
as:
string str; // empty string
char ch;
cin >> ch;
while ( ch != '?' )
str += string( 1, ch ); // Concatenate to str, one character at a time
The other method that's useful is substr. Here's
the prototype:
string substr( int start, int length );
This is a METHOD of string. You give it a start index (starting
at 0), and a length.
string str = "catalog";
string s2 = str.substr( 2, 3 );
cout << s2 << endl; // Prints "tal"
istream & getline( istream &is, string & str, char delimiter );
The function reads up to "max_size" characters (which should be
large enough to accomodate very large strings) from the input stream.
If the string is too large, the input stream goes into an error state.
The function reads up to the delimiter (e.g., '\n' or '\"'). The
delimiter is "extracted" (meaning removed from the input stream), but
not added to the end of str. Thus, if the delimiter is
a double quote, the double quote is removed, but does not appear
at the end of the string.
|
See the class syllabus for policies concerning email Last Modified: Tuesday September 3, 2002 |
|
|
|
|
|