Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Given the following code for the getNum() function (from Assignment #2), answer the following questions: What is stdin? Explain what the each of the three

Given the following code for the getNum() function (from Assignment #2), answer the following questions: What is stdin? Explain what the each of the three parameters to fgets() represent. What does the return value from sscanf() represent? What would likely happen if the & was left out from the sscanf() function call? Be specific and clear. atoi() and getNum() have the same limitation with respect to user input in that a particular value (0 in the case of atoi() and -1 in the case of getNum() are not properly handled. What would be a change to getNum() that would eliminate this limitation. Be specific and clear. Using the struct Part definition from the lecture, declare a variable called myPart of that data type. Initialize all field values appropriately (the actual values are your choice). Write a line of code that doubles the cost field of the myPart variable. Write two lines of code that get the cost field of the myPart variable from the user using fgets() and sscanf(). You do not have to prompt the user. Using the struct Part definition from the lecture, declare an array variable called myParts, containing 45 struct Parts. Write a line of code that doubles the cost field of element #5 of the myParts variable (that is, the element with 5 between the square brackets). Write two lines of code that get the cost field of element #5 of the myParts variable from the user using fgets() and sscanf(). You do not have to prompt the user. #pragma warning(disable: 4996) int getNum(void) { /* the array is 121 bytes in size; we'll see in a later lecture how we can improve this code */ char record[121] = { 0 }; /* record stores the string */ int number = 0; /* NOTE to student: indent and brace this function consistent with your others */ /* use fgets() to get a string from the keyboard */ fgets(record, 121, stdin); /* extract the number from the string; sscanf() returns a number * corresponding with the number of items it found in the string */ if (sscanf(record, "%d", &number) != 1) { /* if the user did not enter a number recognizable by * the system, set number to -1 */ number = -1; } return number; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions