Title: Getting User Input
1Getting User Input
Jan. 29, 2008
2xkcd comic www.xkcd.com
3News
- 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
4QUIZ!
- 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! ?
5Hacker's Tip of the Day
- Alan Turing (1912-1954)?
- Some Key Interests
- Cryptography
- Artificial Intelligence
6Hacker's Tip of the Day
- How do we know if a computer has true artificial
intelligence?
7Turing Test
8Getting User Input
9Warcraft 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 )
10Warcraft 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
11Warcraft 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?
12Specific 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
13User 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!
14Getting 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
15scanf 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 )
16scanf 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 )
17scanf 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 )
18scanf 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
19scanf 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
20scanf 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!!!)?
21scanf 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 .
22scanf 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.
23Steps 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
24Warcraft 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
)
25Warcraft 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.
26Warcraft 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
27Warcraft 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.
28Warcraft 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
29Warcraft 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
30scanf Ignoring Literal Values Used for
Formatting
31Scanning 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
32Scanning 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
33scanf 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 )
34scanf 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
35scanf 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
36scanf 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.
37scanf 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.
38scanf 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.
39scanf 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.
40scanf 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 .
41scanf 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.
42scanf 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
43Formatting 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?
44scanf with Strings
45scanf 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
46scanf 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 )
47scanf 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)?
48scanf 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.
49scanf 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.
50scanf 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.
51scanf 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!
52scanf 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.
53scanf 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.
54scanf 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.
55scanf 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.