bg287

CSC 201 2010 09 01 Notes

Posted in csc201 by bnmng on 2010 09/01

Homework: Read chapter 6 to page 262.
Homework sheet was handed out due Monday.

Today:
Chapter 4: File I/O
Chapter 5:
Logical Experssions
If
Nested, if
Logical

>> This is the extraction operator
<< This is the insertion operator.
cin.get(ch) This is a function that works like cin << but will read white space.
usually, by convention, if we use cin.get then we output with cin.put.

In class exercise of input streams.
float x; int n; char ch;
Data typed is (next two lines):
123 3,145 XYZ
35 Z
(end of data in)

a) cin >> n >> x; cin >> ch;
n=123
x=3.0
ch = ‘,’

b) cin >> n; cin.get(ch);
n=123
ch=’ ‘

c) cin >> x; cin.get(ch); cin >>n;
x=123.0
ch=’ ‘
n=3

d) cin >> ch >> n >>x; cin >> ch;
ch =’,’
m=23
n=3.0

e) cin >> ch >> ch >> ch >> x >> n;
ch=’3′
x=3.0
n=bad number

Using Disk Files:
First, Create a data file. Use a text file editor.
Steps
1) include fstream
2) filename (internal name). Often called a handle. This is not the name of the file on the disk, it’s a name used by the program to refer to the file.
3) open file (use external or diskfile name). Test file.
4) Use the name of the input file (intername) instead of cin, and the name of the output file (internal name) instead of cout.
example
5) avoid silent program
6) close your files.

Decisions:
(example program was handed out)
C++ has a boolean variable, who’s values are defined as true or false.
This won’t work:
bool p;
p=1;
while (p<=5)
p++;
}
It won't work because bool never is greater than 1, which is 'true'. It will be an endless loop.

The following are not assignments, they’re comparitors.
==, !=, <, , >=

The code below will yield ‘false’:
int i;
i=7;
if (i==5)
false;

if (i=5) may not be what you expect. It will assign i=5 and return true.

7<5 // yields false
7.0<5 // works. It will change the 5 to a float. It would be better to cast.
7<'5' // works, but it's using the ascii value of '5', which is 53.

string mystate=”Texas”;
mystate < "texas"; // is true in ascii.

“Maryland” < "Texas" // unpredictable results. Compares the addresses.

string m1 = "475", m2="93";
m1<m2 // True

syntax of a simple if.. else:

if(x < y)
x++;
else
y++;

you can have an if without an else, but no else without an if.

Same statement with curly’s:
if(x < y)
{
x++;
}
else
}
y++;
}
Very important to keep in mind where your semicolons are.

int main ()
{
int a, b, c;
bool error;

cout <> a >> b ;
if (b !=0)
{
c= a/b;
error=false;
}
else
{
c=999;
error=true;
}
if ( ! error ) // This is appropriate sysntax. Means if not error=false;
cout << c << "\n";
else
cout << "Zero Divide \n";
system ("pause");
return 0;
}

The expression if (! error) tests for the variable named ‘error’ being false .
An alternate if ( isok ) would test for the variable ‘isok’ being true.

This control will fail at 100 and 170.
if(weight<110)
cout << "Too Thin\n";
if((110<weight) and (weight < 170))
cout <170)
cout << "Too Fat\n";

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: