Project 4 FAQ

Project 4 FAQ - Spring 2009 CS 31 Project 4 FAQ I get...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Spring 2009 CS 31 Project 4 FAQ I get apparently random results when I try to run some little tests of the comparison operators on strings. For example, a test like "abcde" < "abcxyz" sometimes returns false. What's going on? o To get meaningful results, make sure that at least one of the two objects you are comparing are declared to be of the string type, like s , t , a[0] , and a[1] below: string s = "hello"; string t = "help"; string a[2] = { "helping", "hello" }; s The following are all true: s < t // The h's, e's, and first l's match, but s has // an 'l' where t has a 'p', and 'l' < 'p'. s < "help" // True for the same reason. t < a[0] // The first four characters match, then t runs // out while a[0] has more. "help" < a[0] // True for the same reason. s == a[1] // All 5 characters match, and both strings end at // the same time. s == "hello" // True for the same reason What you cannot do predictably is compare two character string literals: "hello" < "help" may be true or false. A test like this
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 3

Project 4 FAQ - Spring 2009 CS 31 Project 4 FAQ I get...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online