CS 1400 - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

CS 1400

Description:

word = 'hello world'; // ERROR! if (word 'goodbye') // ERROR! word1 = word2; // ERROR! ... reads the next printable character (ignoring white spaces) cin.ignore ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 19
Provided by: scottc50
Category:
Tags: flags | of | printable | the | world

less

Transcript and Presenter's Notes

Title: CS 1400


1
CS 1400
  • Feb 2007
  • Pick ups from chapters 2 and 3

2
Example
  • A person 15 years of age may acquire a drivers
    lic. with permission from parents. A person over
    15 is eligible without parental permission. A
    person under 15 is not eligible.
  • Write a small program to input an age and output
    one of 3 messages permission required,
    eligible, or not eligible.

3
Boolean constants
  • A bool variable may only hold a bool value (true
    or false)
  • bool flag
  • flag true
  • flag false
  • What would this do?
  • while (true)
  • cout ltlt hello!

4
Named constants
  • Constants may be given symbolic names to
    represent them in a program
  • Form const type name value
  • Example
  • const float PI 3.14159
  • const int SIZE 24
  • Alternative Form define name value
  • define PI 3.14159
  • define SIZE 24
  • Defines should be place above main()

5
Character variables
  • A variable of type char can hold one character
  • char sex
  • A character constant is delimited by single
    quotes
  • sex f
  • if (sex m)
  • cout ltlt male

6
cin I/O rules
  • Only digit characters (and sign) can be entered
    for an int variable.
  • Only digit, decimal, and possibly E-notation
    characters (and sign) can be entered for a float
    variable.
  • Only printable characters can be entered for a
    char variable.

7
char vs. literals
  • A literal is different from a character!
  • char c
  • cout ltlt hello world
  • c hello world // ERROR!
  • c h // CORRECT
  • c h // ERROR!

8
Strings or char arrays
  • A message or string of characters can be stored
    in a special array variable
  • char word80 // holds lt80 characters
  • cin gtgt word
  • cout ltlt word ltlt endl
  • Only printable characters can be input using cin
    (no blanks, tabs, etc.)

9
Initially limit use of string arrays
  • For now, only input and output will be done on
    arrays
  • word hello world // ERROR!
  • if (word lt goodbye) // ERROR!
  • word1 word2 // ERROR!

10
Combining assignments
  • Multiple assignment statements may be combined
  • Example
  • a b c 25
  • Shorthand notation for combining arithmetic
    operations and assignment
  • Example
  • a a 2 same as a 2
  • b b 5 same as b - 5
  • c c / 7 same as c / 7

11
Formatting output
  • Requires include ltiomanipgt
  • cout manipulators
  • setw(n)
  • (set the minimum spaces for the next value
    output)
  • setprecision(n)
  • (set the precision or rounding for a float value)
  • fixed
  • (force output of float values in fixed-point
    notation)
  • showpoint
  • (show a decimal point with trailing zeros)

12
Controlling input
  • Requires include ltiomanipgt
  • cin manipulators
  • setw(n)
  • do not input more than n-1 characters for the
    next variable
  • textbook error, pg 128
  • char word5
  • cin gtgt setw(5) gtgt word // user enters Eureka
  • cout ltlt word // program outputs Eure

13
Special input examples
  • char sentence20, ch
  • cin.getline(sentence, 20)
  • reads all characters on a line (up to 19 in
    count) from the keyboard and stores them in array
    sentence
  • cin.get(ch)
  • reads the next input character (including white
    spaces)
  • cin gtgt ch
  • reads the next printable character (ignoring
    white spaces)
  • cin.ignore ()
  • ignore (discard) the next input buffer character

14
More math library functions
  • Requires include ltcmathgt
  • functions examples
  • abs y abs(x)
  • sqrt y sqrt(aa bb)
  • log y log(x)
  • sin y sin(xz)
  • cos y cos(x)
  • tan y tan(x)
  • etc

15
File Output
  • Requires include ltfstreamgt
  • ofstream fout
  • fout.open (report.txt)
  • fout ltlt hello from a file!\n
  • fout.close()
  • To write to report.txt on the a drive
  • fout.open (a\\report.txt)
  • fout.open (areport.txt)
  • fout.open (a\report.txt) // ERROR!

16
File Input
  • Requires include ltfstreamgt
  • int a, b
  • ifstream fin
  • fin.open (report.txt)
  • fin gtgt a gtgt b
  • cout ltlt sum is ltlt ab ltlt endl
  • fin.close()

17
Example
  • Write a program to generate a weekly report of
    concert hall business for a touring show.
  • assume reserved seat tickets 80 stadium seat
    tickets 50
  • standing tickets 35
  • production company gets 80

18
Input file
  • line 1 tour name
  • line 2 reserved stadium standing
  • Example
  • Rolling Stones Geriatric Tour
  • 7500 4110

atour.txt
Write a Comment
User Comments (0)
About PowerShow.com