Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Prease write code by using C++ File: parse_int.cpp In this problem you will write a function to parse an integer from a string that is

Prease write code by using C++

File: parse_int.cpp

In this problem you will write a function to parse an integer from a string that is robust (including overflow) to invalid integers. You cannot use any functions provided by the standard library for parsing an integer from a string, including std::cin >> to an integer variable. Your function will receive the string as a const std::string & and write the parsed integer to an integer out parameter. Youll need to use an out parameter because the function will return a bool (true or false) to signal if parsing was successful or not. The signature of the function youre writing should be: bool parse_int(const std::string &str, int &val); Your parser should be able to handle positive and negative numbers along with slightly odd styles, e.g. +40 should parse as positive 40. You should read the string from std::cin into a std::string. In the case that you encounter something that is not a digit (recall the functions in the cctype header) you should stop parsing and return false to indicate parsing failed. In main youll then see if parse_int returned true or false, if it returns true simply print out the number, if it returns false print out Parsing failed. How can we determine the value of an integer from a char? Recall that we saw that ASCII characters were just numbers, so we can subtract them from each other, for example: '2' - '0' = 2.

image text in transcribed

image text in transcribed

2. Parsing Integers: 30 Points File: parse_int.cpp In this problem you will write a function to parse an integer from a string that is robust (including overflow) to invalid integers. You cannot use any functions provided by the standard library for parsing an integer from a string, including std: cin > to an integer variable. Your function will receive the string as a const std: :string & and write the parsed integer to an integer out parameter You'l need to use an out parameter because the function will return a bool (true or false) to signal if parsing was successful or not. The signature of the function you're writing should be: bool parse int (const std: :string &str, int kval) Your parser should be able to handle positive and negative numbers along with slightly odd styles, e.g. +40 should parse as postve 40. You should read the string from std::cin into a std::string. In the case that you encounter something that is not a digit (recall the functions in the cctype header) you should stop parsing and return false to indicate parsing failed. In main you'l then see if parse int returned true or false, if it returns true simply print out the number, if it returns false print out 'Parsing failed How can we determine the value of an integer from a char? Recall that we saw that ASCII characters were just numbers, so we can subtract them from eachh other, for example: '2' - '0' 2. Example Input 1542 9852 +92 10 hello! 10oops 123 123456789012354444444444444444 Expected Output: Case 0: 1542 Case 1 -9852 Case 2: 92 Case 3 10 Case 4

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_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions

Question

1. How will you, as city manager, handle these requests?

Answered: 1 week ago

Question

1. Identify the sources for this conflict.

Answered: 1 week ago