Question: Program 1 Write a program that prompts the user to enter two positive whole numbers as strings! Create a function that validates the input and
Program 1
Write a program that prompts the user to enter two positive whole numbers as strings!
- Create a function that validates the input and returns true if all chars in the string are digits. Call the function from the main and prompt the user to re-enter as long as invalid input was entered.
- Create a second function that converts the string to an int and returns it. Use a loop to iterate over each char in the string. You can convert a char c to an int by subtracting the char 0 from it: int num = (int)ch - '0'; Add the digit to a result variable and multiply the result with each iteration by 10, for example: 4127 can be split up as 4 * 1000 + 1 * 100 + 2 * 10 + 7 * 1
The program then converts both strings to integers and computes and prints out the sum.
Hints: You may use the isdigit(x) function provided by
WARNING: If you're program does not execute due to syntax errors, you will lose at least 50% of total points.
We use C program by the way. Thank you.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
