Lecture06-w03-LIN-09162019-scanf_Conditional.key.pdf - Computing I COMP.1010 Fall 2019 Lecture 6 scanf Decision Making Structure By Sirong Lin PhD

Lecture06-w03-LIN-09162019-scanf_Conditional.key.pdf -...

This preview shows page 1 - 11 out of 37 pages.

Computing I By: Sirong Lin, PhD University of Massachusetts Lowell COMP.1010, Fall 2019 Lecture 6: scanf & Decision Making Structure
Image of page 1
Work Ready Life Ready World Ready Computing I, F2019, By Dr. Sirong Lin Upcoming Due Dates 2 Due Date Daily 04 09-16-2019 (M), 9:00pm Week 03 Reading 1, 2, 3 MWF, 9:00am Week 03 Practice 09-22-2019 (Su), 9:00pm Daily 05 09-18-2019 (W), 9:00pm Daily 06 09-20-2019 (F), 9:00pm
Image of page 2
Work Ready Life Ready World Ready Computing I, F2019, By Dr. Sirong Lin Outline Read data from standard input (keyboard) using scanf (Cont’d) Decision Making Structure 3
Image of page 3
Work Ready Life Ready World Ready Computing I, F2019, By Dr. Sirong Lin Read Data from Keyboard — scanf with %d (Cont’d) read an integer from user’s input and place it to the memory location of variable userNum %d indicates to read an integer & (ampersand): address-of operator in C &score : the address of int variable userNum 4 int score; scanf("%d", &score); //in stdio.h
Image of page 4
Work Ready Life Ready World Ready Computing I, F2019, By Dr. Sirong Lin Read Data from Keyboard — scanf with %d (Cont’d) when scanf function executes, the program pauses until the required data are entered and the <return>/<enter> key is pressed through the keyboard 5 scanf("%d", &score); //in stdio.h
Image of page 5
Work Ready Life Ready World Ready Computing I, F2019, By Dr. Sirong Lin Read integer from Keyboard — scanf with %d (Cont’d) Run the code below, what do we see? Blank screen — the user might not know what to do 6 int score; scanf("%d", &score); printf("You entered: %d\n", score);
Image of page 6
Work Ready Life Ready World Ready Computing I, F2019, By Dr. Sirong Lin 7 scanf function — a better way to write code Fix : add a printf before scanf: display a prompting message and tell the user what data to enter (otherwise, the user will just see a blank screen) int score; printf("Please input a score:"); scanf("%d", &score); printf("You entered: %d\n", score);
Image of page 7
Work Ready Life Ready World Ready Computing I, F2019, By Dr. Sirong Lin Read Data from Keyboard — scanf with %d (Cont’d) How is an integer read? (Basic facts) program first skips any white spaces (including spaces, \n, \t, etc) scans digits until it reaches a character that cannot be part of the number then all the scanned digits will be converted to an integer value and stored in the variable in memory, e.g., score 8
Image of page 8
Work Ready Life Ready World Ready Computing I, F2019, By Dr. Sirong Lin Read Data from Keyboard — scanf with %d (Cont’d) User enters 123 123 is scanned, converted to an integer and placed into variable score 9
Image of page 9
Work Ready Life Ready World Ready Computing I, F2019, By Dr. Sirong Lin Read Data from Keyboard — scanf with %d (Cont’d) User enters 123
Image of page 10
Image of page 11

You've reached the end of your free preview.

Want to read all 37 pages?

  • Fall '15

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors