Left Quote    I think there is a world market for maybe five computers.
- Thomas Watson, Chairman of IBM 1943    
Right Quote
[login] | [Register]

Beginning Visual C++ 6.0

by: bs0d
Page: 5 of 10
(View All)


Finally I will cover the assign function. With this spetacular function (i know you're excited), you can assign portionsof one string into another! Like always, the syntax is:

destinationString.assign(sourceString,startAssign, numOfCharsToAssign)

Now, lets see how she works:

In the sample above, you see we start with a string that has first and last. We then assign into the string, first, 6 characters. And then we assign the string, last, 8 characters starting with the 7th from the fullName string.

You can use this function in another way, like duplicating characters. Say you needed an exact amout of characters and might even need to edit the amount. Instead of just assigning them by hand, like : string str1 = "________________"; You can use the assign function to assign the variable a number of characters. See the code below:

The assign statement assigns 35 of character in the 2nd argument into the string, bar. You can easily change that 35 to 1000, or whatever. Thats an advantage to you.

If / Else

Moving on a bit, we'll talk about conditions. In C++ you'll be making some decisions in your programs, so If else and elseif come to the rescue! The syntax is pretty much standard, but again its all different; like opposed to Visual Basic. So follow these syntax guidelines:

if (condition) {
     do this
      and this

  } //end if
  else {
      do this
      and that

  } //end else

If you only have one statement, then the brackets are not required. The operators you will use are:

  •  < -less than
  •  <= -less than or equal to
  •  > - greater than
  •  >= - greater than or equal to
  •  == - equal
  •  != - Not equal.
  •  && - And (both conditions on each side of && must be true)
  •  || - OR (only one condition on either side has to be true)

*Note: When you use varX = something; thats different when you are comparing something in your condition and use '==', dont try to use '=' in your condition or you will get an error. Thats pretty common occurance, so be alert!!

Lets get a code sample started showing you the ropes using if/else and some operators:

You can look at that and make out whats going on. On the bottom condition with the letters, I defined the the characters as character literal constants (single quotes, [string liter consants are double]) so there is a difference between the case of the letters.

Nested Conditions

Thats about all there is to If/Else, unless you're unaware of working with nested conditions. This is where you will build multiple conditions to each er, condition. Like for example heres another code to look at (just the meat):

The inital condition is asking for gender, if you answer 'male' then you are also asked if you are over 21, if you say 'yes' it shows message, if you say no, it shows a different message. And if you answer anything other than 'male' to the first condition, it shows the last message.

Else-if and SWITCH

Technically, you can set more conditions upon conditions... Another method is to use else if. I will say right now that its not necessairly worth using if you have alot of conditions, i would use it on only a few conditions, because there is a better solution to multiple conditions. Before I get there, heres how you can use else if:

if(something == 'this' || something == 'THIS')
do this
else if(something == 'that' || something == 'THAT')
do that
else if(something == 'this-n-that' || something == 'THIS-N-THAT')
do this-n-that
do nothing

I think thats fine, a few conditions and else if never hurt anyone, but if you want to save your fingers and have quite a few conditions to deal with, I recommend to you switch. It will be easier to read and can deal with alot of conditions quite easily. All you need to know is the syntax, so here it is to follow:


case 'A':
case 'a': <code to do something here>
case 'B':
case 'b': <code that does something for these conditions>
case 'C':
case 'c': <something for these conditions>
default: <code that executes if VARIABLE meets none of the above>
} //ends switch

Let me explain a bit. You include in the parenthesis your variable you want to perform the check on. If the value of that variable meets the case of 'A' or 'a' it will execute the code stated on the last condition if a case is blank. Just like you see with each condition, the code of case 'a' would also be executed if the variable's value was 'A' - Notice at the bottom is default: This is the code that will execute if none of the cases match the variable. Now after the code you want executed, on the next line is a break statement. The break statement will break the switch, or drop out of it and continue on with the rest of your program.

1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  10  |  
Next »

No Comments for this page.

You Must be logged in or a member to comment.

Tutorial Stats

Tutorial Stats

1 Total Comments
4 Rating of 5 (2 Votes)


Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article



· C Tutorial
Your Article Here

"" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank