CS151 -- Administration Notice
Assignment 1 due Tonight 12:00 midnight
Sign up for the class mailing list. Project 2 will
be assigned using that!
Project 1 is posted, and due next Friday, by midnight:
http://www.cs.unm.edu/~storm/C++/cs151_summer2001/Assignment1.html
CS151 -- Shawn Stoffer
Variable Types
Strings
CS151 -- Shawn Stoffer
C-String
-
Older
-
Used in C
-
char * string;
-
require usage of special functions, such as:
-
strcpy()
-
strcmp()
-
strchr()
-
sprintf()
-
basically functions are contained in the cstring library
CS151 -- Shawn Stoffer
String Class
-
Can use typical operators, =, +=, +, ==, etc...
-
string str;
-
The string library.
-
The more accepted fix for C-strings.
CS151 -- Shawn Stoffer
Input and Output
CS151 -- Shawn Stoffer
Input and Output
-
cout and cin are just variables
-
cout <<, cerr <<
-
cin >>
CS151 -- Shawn Stoffer
So, what can you do with variables?
CS151 -- Shawn Stoffer
What can you do with variables?
-
=
-
The assignment operator
-
gives a variable a value
-
often combined with arithmetic operators
-
x = y+3;
-
x += 3; == x = x+3;
CS151 -- Shawn Stoffer
What can you do with variables?
-
==
-
The equality operator
-
tests whether a variable has a value or not
CS151 -- Shawn Stoffer
What can you do with variables?
-
+, -
-
The operators add and subtract
-
performs the addition operation for two values or variables
-
performs the subtraction operation for two values or variables
CS151 -- Shawn Stoffer
What can you do with variables?
-
*, /
-
The operators multiply and divide
-
perform the multiplication operation for two variables or
values
-
perform the division operation for two variables or values
CS151 -- Shawn Stoffer
What can you do with variables?
-
more on the assignment operator (=)
-
it can be combined with any arithmetic operator
-
perform the operation on the variable on the left hand side
and the variable on the right hand side, and place the resulting value
into the left hand side variable.
-
x += 3; is the same as x = x+3;
CS151 -- Shawn Stoffer
C++ Basics
CS151 -- Shawn Stoffer
C++ Basics
<statements>
-
take the form of something ending in a semi-colon
-
special case (if, else, etc...)
CS151 -- Shawn Stoffer
C++ Basics
Statements
-
int y;
-
float x;
-
x = 3;
-
cout << "Hello World!" << endl;
CS151 -- Shawn Stoffer
C++ Basics
The puts operator: <<
-
Put something into an output stream (cout, cerr)
-
cout << "Hello";
-
string myName = "David";
cout << myName;
-
can output nearly anything
CS151 -- Shawn Stoffer
C++ Basics
The gets operator: >>
-
Get something from an input stream
-
can only "get" into a variable, and from an input stream
-
string userInput;
cin >> userInput;
-
with strings, only gets characters up to the first space,
or newline
-
with numbers, there must be a number there, otherwise, error.
CS151 -- Shawn Stoffer
C++ Basics
Flow of Control
-
if/else
-
string userInput;
cin >> userInput;
if (userInput == "David") {
return 1;
} else {
return 0;
}
CS151 -- Shawn Stoffer
C++ Basics
Planning your program
CS151 -- Shawn Stoffer