Functions You have probably noticed that if you try to read into an int variable using scanf but type some text instead, then the program will do unpredictable things and generally not work correctly....


Write code for the following


Functions<br>You have probably noticed that if you try to read into an int variable using scanf but type some text instead, then the program will do unpredictable things and generally not work correctly. In this exercise you will write a function to<br>safely read an int by first reading it as a string.<br>Write a function named safeReadInt that takes no parameters and returns an int. The function should read input into a large string using scanf, convert the string into an int, and return the int. Use the atoi function to<br>convert from a string to an int. Here is a reference on atoi if you want to learn more. (There is a safer way to do the conversion, strtoul, but we'll skip that for now). Write some code in main that calls and tests your function.<br>A disadvantage of using atoi is that it returns 0 if the string can't be converted to an integer, but there is no way to tell if the user intended to type 0 or you are getting 0 because of an error. Modify the function so that it loops<br>through the input string and forces the user to type new input if any of the characters typed in are not digits. This will require some type of loop. If all of the characters are digits then atoi can be invoked and returned. Recall<br>that '0' through '9' have numerical ASCII values that are contiguous, so you can check for a char < '0' and a char > '9' to see if it is not a digit.<br>

Extracted text: Functions You have probably noticed that if you try to read into an int variable using scanf but type some text instead, then the program will do unpredictable things and generally not work correctly. In this exercise you will write a function to safely read an int by first reading it as a string. Write a function named safeReadInt that takes no parameters and returns an int. The function should read input into a large string using scanf, convert the string into an int, and return the int. Use the atoi function to convert from a string to an int. Here is a reference on atoi if you want to learn more. (There is a safer way to do the conversion, strtoul, but we'll skip that for now). Write some code in main that calls and tests your function. A disadvantage of using atoi is that it returns 0 if the string can't be converted to an integer, but there is no way to tell if the user intended to type 0 or you are getting 0 because of an error. Modify the function so that it loops through the input string and forces the user to type new input if any of the characters typed in are not digits. This will require some type of loop. If all of the characters are digits then atoi can be invoked and returned. Recall that '0' through '9' have numerical ASCII values that are contiguous, so you can check for a char < '0'="" and="" a="" char=""> '9' to see if it is not a digit.

Jun 06, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here