Getting User Input - PowerPoint PPT Presentation

1 / 55
About This Presentation
Title:

Getting User Input

Description:

Getting User Input – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 56
Provided by: csInd
Category:
Tags: getting | input | piece | user

less

Transcript and Presenter's Notes

Title: Getting User Input


1
Getting User Input
Jan. 29, 2008
2
xkcd comic www.xkcd.com
3
News
  • Homepage mirror
  • http//www.cs.indiana.edu/mewhiteh/i210
  • First homework is assigned today
  • Due this Thursday before lecture
  • We'll look over submissions anonymously on
    Thursday
  • Lecture notes will be posted before lecture from
    now on
  • Password for week 3 sample code
  • turtle

4
QUIZ!
  • What's the difference between f and g?
  • What is an ASCII value?
  • What is a string? How is it different than
    other variable types we've seen before?
  • How would you declare a string of size 64 with
    the initial value of Go Giants! ?
  • char myString password How big is
    myString?
  • How would you change the string in 4 to No
    Giants! ?

5
Hacker's Tip of the Day
  • Alan Turing (1912-1954)?
  • Some Key Interests
  • Cryptography
  • Artificial Intelligence

6
Hacker's Tip of the Day
  • How do we know if a computer has true artificial
    intelligence?

7
Turing Test
8
Getting User Input
9
Warcraft Gold Conversion
int main( int argc, char argv )? float
costOneGold 0.069 int gold10Dollars
0 gold10Dollars 10.0 / costOneGold printf(
" 10 gets you 5d gold\n", gold10Dollars
) return( 0 )
10
Warcraft Gold Conversion
int main( int argc, char argv )? float
costOneGold 0.069 int gold10Dollars
0 gold10Dollars 10.0 / costOneGold printf(
" 10 gets you 5d gold\n", gold10Dollars
) return( 0 )
This was the conversion rate...
...and this was the dollar amount to be converted
11
Warcraft Gold Conversion
  • What if you gave the program to a friend that
    doesn't know about programming?
  • Would it be useful to her?
  • How would she convert different dollar amounts
    into gold counts?

12
Specific Vs. General Program
  • The Warcraft gold conversion program is highly
    specific in purpose
  • It's too specific...it only does 1 single
    conversion without recompiling
  • General Version of the Program
  • Allow different conversion rates to be used
  • Allow different dollar amounts to be converted

13
User Input
  • The program user (not the programmer!) should set
    the conversion rate and dollar amount to be
    converted
  • The program can then be reused for many different
    related conversion problems
  • This makes the program better!

14
Getting User Input
  • A special function is used to get user input
    scanf
  • scanf puts values from the user into variables
  • Scanned variable values can be used normally
    after scanning
  • scanf is from the stdio.h header file
  • scanf/printf for basic I/O
  • scanf for input
  • printf for output

15
scanf Example
int main( int argc, char argv )? int
userNumber 0 printf( "Please enter a number
from 1-10 " ) scanf( " d", userNumber
) printf( You entered the number d\n,
userNumber ) return( 0 )
16
scanf Example
First, declare a new variable to store the user
input
int main( int argc, char argv )? int
userNumber 0 printf( "Please enter a number
from 1-10 " ) scanf( " d", userNumber
) printf( You entered the number d\n,
userNumber ) return( 0 )
17
scanf Example
Its initial value doesn't really matter because
it will soon be replaced
int main( int argc, char argv )? int
userNumber 0 printf( "Please enter a number
from 1-10 " ) scanf( " d", userNumber
) printf( You entered the number d\n,
userNumber ) return( 0 )
18
scanf Example
int main( int argc, char argv )? int
userNumber 0 printf( "Please enter a number
from 1-10 " ) scanf( " d", userNumber
) printf( You entered the number d\n,
userNumber ) return( 0 )
A normal printf message prompts the user to enter
a value
19
scanf Example
int main( int argc, char argv )? int
userNumber 0 printf( "Please enter a number
from 1-10 " ) scanf( " d", userNumber
) printf( You entered the number d\n,
userNumber ) return( 0 )
scanf is the name of the new function that gets
user input
20
scanf Example
int main( int argc, char argv )? int
userNumber 0 printf( "Please enter a number
from 1-10 " ) scanf( " d", userNumber
) printf( You entered the number d\n,
userNumber ) return( 0 )
d is for the type of variable you are scanning
for. In this case, an integer. (NOTICE there
is a space before the d!!!)?
21
scanf Example
int main( int argc, char argv )? int
userNumber 0 printf( "Please enter a number
from 1-10 " ) scanf( " d", userNumber
) printf( You entered the number d\n,
userNumber ) return( 0 )
The variable to store the user input preceded by
an . It is very easy to forget the .
22
scanf Example
int main( int argc, char argv )? int
userNumber 0 printf( "Please enter a number
from 1-10 " ) scanf( " d", userNumber
) printf( You entered the number d\n,
userNumber ) return( 0 )
Now we can print out the scanned value with a
normal printf statement.
23
Steps to Using scanf
  • Declare a variable that will be used to store
    the entered value
  • Use printf to show a prompt that tells the user
    what kind of value to enter
  • Use the scanf function with the appropriate
    d,f,... marker and in front of the variable
    name
  • Use the variable with the scanned value normally

24
Warcraft Gold Conversion User Inputs
int main( int argc, char argv )? float
costOneGold 0.0 float dollarAmount
0.0 int numberGold 0 printf( Please enter
the cost for 1 piece of gold ) scanf( f,
costOneGold ) printf( Please enter the
dollar amount to convert ) scanf( f,
dollarAmount ) numberGold dollarAmount /
costOneGold printf( " .2f gets you d
gold\n", dollarAmount, numberGold ) return( 0
)
25
Warcraft Gold Conversion User Inputs
int main( int argc, char argv )? float
costOneGold 0.0 float dollarAmount
0.0 int numberGold 0 printf( Please enter
the cost for 1 piece of gold ) scanf( f,
costOneGold ) printf( Please enter the
dollar amount to convert ) scanf( f,
dollarAmount ) numberGold dollarAmount /
costOneGold printf( " .2f gets you d
gold\n", dollarAmount, numberGold ) return( 0
)
First, prompt for the conversion rate.
26
Warcraft Gold Conversion User Inputs
int main( int argc, char argv )? float
costOneGold 0.0 float dollarAmount
0.0 int numberGold 0 printf( Please enter
the cost for 1 piece of gold ) scanf( f,
costOneGold ) printf( Please enter the
dollar amount to convert ) scanf( f,
dollarAmount ) numberGold dollarAmount /
costOneGold printf( " .2f gets you d
gold\n", dollarAmount, numberGold ) return( 0
)
Next, use scanf to get the value from the user
27
Warcraft Gold Conversion User Inputs
int main( int argc, char argv )? float
costOneGold 0.0 float dollarAmount
0.0 int numberGold 0 printf( Please enter
the cost for 1 piece of gold ) scanf( f,
costOneGold ) printf( Please enter the
dollar amount to convert ) scanf( f,
dollarAmount ) numberGold dollarAmount /
costOneGold printf( " .2f gets you d
gold\n", dollarAmount, numberGold ) return( 0
)
Also, prompt and scanf for the dollar amount to
be converted.
28
Warcraft Gold Conversion User Inputs
int main( int argc, char argv )? float
costOneGold 0.0 float dollarAmount
0.0 int numberGold 0 printf( Please enter
the cost for 1 piece of gold ) scanf( f,
costOneGold ) printf( Please enter the
dollar amount to convert ) scanf( f,
dollarAmount ) numberGold dollarAmount /
costOneGold printf( " .2f gets you d
gold\n", dollarAmount, numberGold ) return( 0
)
Calculate the number of gold using the entered
values
29
Warcraft Gold Conversion User Inputs
int main( int argc, char argv )? float
costOneGold 0.0 float dollarAmount
0.0 int numberGold 0 printf( Please enter
the cost for 1 piece of gold ) scanf( f,
costOneGold ) printf( Please enter the
dollar amount to convert ) scanf( f,
dollarAmount ) numberGold dollarAmount /
costOneGold printf( " .2f gets you d
gold\n", dollarAmount, numberGold ) return( 0
)
Print out the dollar amount entered and the
calculated number of gold after conversion
30
scanf Ignoring Literal Values Used for
Formatting
31
Scanning Literal Values
  • Sometimes we want the user to enter a value in a
    certain format
  • Sometimes the user expects to enter a value in a
    certain format
  • We can use scanf to allow any kind of format for
    data entry
  • scanf can ignore some symbols and concentrate
    only on the specific values needed

32
Scanning Literal Values - Example
  • People like to enter dates like this
  • 01/29/2008 (today)?
  • 04/29/2008 (the course final exam)?
  • mm/dd/yyyy (general form)?
  • dd/mm/yyyy (European style)?
  • Numeric variables (integers and floats) would not
    be able to store these date values as entered

33
scanf with Literals
int main( int argc, char argv )? int month
0 int day 0 int year 0 printf( Please
enter your birthday like this mm/dd/yyyy
) scanf( d/ d/ d, month, day, year
) printf( " You were born on d-d-d\n",
month, day, year ) return( 0 )
34
scanf with Literals
int main( int argc, char argv )? int month
0 int day 0 int year 0 printf( Please
enter your birthday like this mm/dd/yyyy
) scanf( d/ d/ d, month, day, year
) printf( " You were born on d-d-d\n",
month, day, year ) return( 0 )
Declare different variables for each part of the
date
35
scanf with Literals
int main( int argc, char argv )? int month
0 int day 0 int year 0 printf( Please
enter your birthday like this mm/dd/yyyy
) scanf( d/ d/ d, month, day, year
) printf( " You were born on d-d-d\n",
month, day, year ) return( 0 )
Print a prompt that shows the exact format
expected by scanf
36
scanf with Literals
int main( int argc, char argv )? int month
0 int day 0 int year 0 printf( Please
enter your birthday like this mm/dd/yyyy
) scanf( d/ d/ d, month, day, year
) printf( " You were born on d-d-d\n",
month, day, year ) return( 0 )
Use literal forward slashes, /, in the scanf.
This means the user must enter in this format, or
the program will not work.
37
scanf with Literals
int main( int argc, char argv )? int month
0 int day 0 int year 0 printf( Please
enter your birthday like this mm/dd/yyyy
) scanf( d/ d/ d, month, day, year
) printf( " You were born on d-d-d\n",
month, day, year ) return( 0 )
The slashes are not stored in any variables.
They are just there for formatting purposes.
38
scanf with Literals
int main( int argc, char argv )? int month
0 int day 0 int year 0 printf( Please
enter your birthday like this mm/dd/yyyy
) scanf( d/ d/ d, month, day, year
) printf( " You were born on d-d-d\n",
month, day, year ) return( 0 )
Put a space in front of each d.
39
scanf with Literals
int main( int argc, char argv )? int month
0 int day 0 int year 0 printf( Please
enter your birthday like this mm/dd/yyyy
) scanf( d/ d/ d, month, day, year
) printf( " You were born on d-d-d\n",
month, day, year ) return( 0 )
This line scans the 3 parts of the date all at
once. We could have used a separate scanf for
each part.
40
scanf with Literals
int main( int argc, char argv )? int month
0 int day 0 int year 0 printf( Please
enter your birthday like this mm/dd/yyyy
) scanf( d/ d/ d, month, day, year
) printf( " You were born on d-d-d\n",
month, day, year ) return( 0 )
List the variables in order. Each is preceded by
an .
41
scanf with Literals
int main( int argc, char argv )? int month
0 int day 0 int year 0 printf( Please
enter your birthday like this mm/dd/yyyy
) scanf( d/ d/ d, month, day, year
) printf( " You were born on d-d-d\n",
month, day, year ) return( 0 )
Print out the date using a different format.
Remember, just the numeric values were stored, so
the output format can be different than the input
format.
42
scanf with Literals
int main( int argc, char argv )? int month
0 int day 0 int year 0 printf( Please
enter your birthday like this mm/dd/yyyy
) scanf( d/ d/ d, month, day, year
) printf( " You were born on d-d-d\n",
month, day, year ) printf( " European format
d-d-d\n", day, month, year ) return( 0 )
Remember, the order of the variables affects the
print out order
43
Formatting with Leading Zeros
int main( int argc, char argv )? int month
0 int day 0 int year 0 printf( Please
enter your birthday like this mm/dd/yyyy
) scanf( d/ d/ d, month, day, year
) printf( " You were born on 02d-02d-d\n",
month, day, year ) return( 0 )
Use 02d to print out a 0 before any single digit
day or month. What do you think 04d would do?
44
scanf with Strings
45
scanf with Strings
  • scanf works with any variable types
  • integers, floats, characters, strings
  • Scanning for strings works much the same way as
    for numeric types
  • There are a few extra considerations

46
scanf with Strings
int main( int argc, char argv )? char city
MaxStringLength char state
MaxStringLength printf( Please enter
your home city ) scanf( s, city
) printf( Please enter your home state
) scanf( s, state ) printf( " You hail
from s, s\n", city, state ) return( 0 )
47
scanf with Strings
int main( int argc, char argv )? char city
MaxStringLength char state
MaxStringLength printf( Please enter
your home city ) scanf( s, city
) printf( Please enter your home state
) scanf( s, state ) printf( " You hail
from s, s\n", city, state ) return( 0 )
Declare two string variables with size
MaxStringLength, which is defined as a constant
above (not shown)?
48
scanf with Strings
int main( int argc, char argv )? char city
MaxStringLength char state
MaxStringLength printf( Please enter
your home city ) scanf( s, city
) printf( Please enter your home state
) scanf( s, state ) printf( " You hail
from s, s\n", city, state ) return( 0 )
The initial values for these strings are empty.
scanf will give them values.
49
scanf with Strings
int main( int argc, char argv )? char city
MaxStringLength char state
MaxStringLength printf( Please enter
your home city ) scanf( s, city
) printf( Please enter your home state
) scanf( s, state ) printf( " You hail
from s, s\n", city, state ) return( 0 )
Print a prompt for the user to enter his home
city.
50
scanf with Strings
int main( int argc, char argv )? char city
MaxStringLength char state
MaxStringLength printf( Please enter
your home city ) scanf( s, city
) printf( Please enter your home state
) scanf( s, state ) printf( " You hail
from s, s\n", city, state ) return( 0 )
Use scanf with s to grab a string.
51
scanf with Strings
int main( int argc, char argv )? char city
MaxStringLength char state
MaxStringLength printf( Please enter
your home city ) scanf( s, city
) printf( Please enter your home state
) scanf( s, state ) printf( " You hail
from s, s\n", city, state ) return( 0 )
NOTICE a string variable does NOT have an in
front of it in a scanf!
52
scanf with Strings
int main( int argc, char argv )? char city
MaxStringLength char state
MaxStringLength printf( Please enter
your home city ) scanf( s, city
) printf( Please enter your home state
) scanf( s, state ) printf( " You hail
from s, s\n", city, state ) return( 0 )
Prompt and scan for the state value, too.
53
scanf with Strings
int main( int argc, char argv )? char city
MaxStringLength char state
MaxStringLength printf( Please enter
your home city ) scanf( s, city
) printf( Please enter your home state
) scanf( s, state ) printf( " You hail
from s, s\n", city, state ) return( 0 )
Print out the entered values for both city and
state. Separate them with a comma.
54
scanf with Strings
int main( int argc, char argv )? char city
MaxStringLength char state
MaxStringLength printf( Please enter
your home city ) scanf( s, city
) printf( Please enter your home state
) scanf( s, state ) printf( " You hail
from s, s\n", city, state ) return( 0 )
Try entering Los Angeles for your home city.
55
scanf with Strings
int main( int argc, char argv )? char city
MaxStringLength char state
MaxStringLength printf( Please enter
your home city ) scanf( s, city
) printf( Please enter your home state
) scanf( s, state ) printf( " You hail
from s, s\n", city, state ) return( 0 )
Try setting a very small MaxStringLength and
entering a longer city name.
Write a Comment
User Comments (0)
About PowerShow.com