Challenge: user input

So far, the programs you have written do some work and then output text to the console. In this challenge, you will modify your CountDown solution to ask for input from the user. In particular, you will ask the user what number the countdown should start from.

To make this happen, you need to know about two new functions: readline() and atoi() (pronounced A to I).

The readline function is the opposite of printf(). Rather than printing text to the screen, it gets text that user has entered.

Before you can use readline(), you must first add the library that contains it to your program.

In the project navigator, click the top-level Coolness item. In the editor area, click Build Phases and then the disclosure triangle next to the line that says Link Binary With Libraries.

Figure 8.1  Link binary with libraries

Link binary with libraries

Click the + button. A sheet will a appear with a list of available code libraries. Use the search box to search for libreadline. When it appears in the list, select it and click Add.

Figure 8.2  Libraries

Libraries

Select main.c in the project navigator to get back to your code.

What were these steps for? Sometimes, you want to use a function that is not already provided for you. So you need to tell Xcode which code library contains the function you want to use.

Let’s look at an example that uses the readline() function. You started this chapter with code that printed Aaron is Cool. What if the user could enter the name of the person that is cool? Here is what the program would look like when run. (The user input is shown in bold)

W​h​o​ ​i​s​ ​c​o​o​l​?​ ​M​i​k​e​y​
M​i​k​e​y​ ​i​s​ ​c​o​o​l​!​

The code would look like this:

#​i​m​p​o​r​t​ ​<​r​e​a​d​l​i​n​e​/​r​e​a​d​l​i​n​e​.​h​>​
#​i​m​p​o​r​t​ ​<​s​t​d​i​o​.​h​>​
i​n​t​ ​m​a​i​n​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​W​h​o​ ​i​s​ ​c​o​o​l​?​ ​"​)​;​
 ​ ​ ​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​n​a​m​e​ ​=​ ​r​e​a​d​l​i​n​e​(​N​U​L​L​)​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​%​s​ ​i​s​ ​c​o​o​l​!​​n​​n​"​,​n​a​m​e​)​;​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

(Type this code into your Coolness project and run it, if you would like to see it in action.)

The first line of this main function is a variable declaration:

 ​ ​ ​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​n​a​m​e​;​

Remember that char * is a type you can use for strings.

In the third line, you call the readline function, and pass NULL as its argument. This line gets what the user typed in and stores it in the name variable.

Now let’s turn to the atoi function. This function takes a string and converts it into an integer. (The ‘i’ stands for integer, and the ‘a’ stands for ASCII.)

What good is atoi()? The following example code would cause an error because it attempts to store a string in a variable of type int.

i​n​t​ ​n​u​m​ ​=​ ​"​2​3​"​;​

You can use atoi() to convert that string into an integer with a value of 23, which you can happily store in a variable of type int:

i​n​t​ ​n​u​m​ ​=​ ​a​t​o​i​(​"​2​3​"​)​;​

(If the string passed into atoi() cannot be converted into an integer, then atoi() returns 0.)

With these two functions in mind, modify your code to prompt the user for input and then kick off the countdown from the desired spot. Your output should look something like this:

W​h​e​r​e​ ​s​h​o​u​l​d​ ​I​ ​s​t​a​r​t​ ​c​o​u​n​t​i​n​g​?​ ​4​2​
4​2​
3​9​
3​6​
3​3​
3​0​
F​o​u​n​d​ ​o​n​e​!​
2​7​
.​.​.​

Note that Xcode has an interesting behavior when using the readline function. It will duplicate text input as output:

Figure 8.3  readline() output

readline() output

This is expected behavior in Xcode.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset