-------------------------------------------------
Introducing scanf to designate selective information into variables from the keyboard
The program be looked at in the previous session was fine to use when working at bottom the C++ IDE (Integrated Development Environment). exactly what if we wanted to give the program to somebody else without the C compiler, so that they could change the years set in the program without having to change the original source canon?
You need some way of letting the person (or user) scotch selective information into the program - the obvious choice being the keyboard.
The scanf officiate will wait for the user to case in information at the keyboard, and when they press the Enter key, will take the data they have typed, and place it into a variable.
For example, if we wanted to read somebodys eld, and compile out the approximate number of long time and weeks they have lived, these lead lines of code would do the trick (note that this isnt a complete program, so will not work on its own if you type it in): -
float age,days,weeks;
printf(Enter age in years: ); scanf(%f,&age);
days = age*365.25; weeks = age*52.0;
printf( You have lived for %5f days and %4f weeks, days, weeks );
This assumes the average number of days in the year is 365.
25 and in that respect are 52 weeksÂ
in the year.
The scanf function is a musical composition like the opposite of the printf function - printf writesÂ
data out to the console, scanf reads data in from the console.
Notice that scanf also uses the %f code to represent that we want to read data into a floating-point variable. If we wanted to read into an integer variable, we would use %d instead.
The &age shows that the %f that is read in from the keyboard will be put into the ageÂ
variable. Why do we need the & in drive of the variable? We will cover this in aÂ
posterior session, when we deal with passing variables to functions by reference. DontÂ
worry about this for the moment.
But be VERY...If you want to get a full essay, mold it on our website: Ordercustompaper.com
If you want to get a full essay, wisit our page: write my paper
No comments:
Post a Comment