The Loop Operation Let's spend a few moments writing a plan as a pseudocode. sum <- 0 number - 0 Grab the current character -> ch If ch is a digit then ● digit <-ascii-to-decimal(ch) number<- number * 10 number<- number + digit Else sum <- sum + number number <- 0 Here are some notes on implementing this plan in C++. Use str.at () to grab the current character; store it in a variable Use isdigit() to see if the character is between '0' and '9' inclusive There is no ascii-to-decimal function. Instead, subtract the character '0' from ch and store the result in the variable digit. Note that the ch has an underlying ASCII code and the codes are sequential. If ch is '0' then subtracting '0' will store the binary number in digit. If the character is '1', then subtracting '0' will leave the binary number 1. Multiply the current value of number by 10, and then add the digit. For instance, if number has the value 2 and digit has the value 5, then number * 10 -> 20 and adding 5 leaves number with 25, which is correct. When you encounter something that isn't a digit, then add the current value of number to sum. Then, set number back to 0 for the next iteration.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Need some help turning this c++ pseudocode into working code

The Loop Operation
Let's spend a few moments writing a plan as a pseudocode.
sum <- 0
number - 0
Grab the current character -> ch
If ch is a digit then
digit ‹- ascii-to-decimal (ch)
number<- number * 10
number<- number + digit
●
Else
sum <- sum + number
number <- 0
Here are some notes on implementing this plan in C++.
Use str.at() to grab the current character; store it in a variable
Use isdigit() to see if the character is between '0' and '9' inclusive
There is no ascii-to-decimal function. Instead, subtract the character '0'
from ch and store the result in the variable digit. Note that the ch has an
underlying ASCII code and the codes are sequential. If ch is '0' then
subtracting '0' will store the binary number in digit. If the character is
'1', then subtracting '0' will leave the binary number 1.
Multiply the current value of number by 10, and then add the digit. For
instance, if number has the value 2 and digit has the value 5, then number
* 10 -> 20 and adding 5 leaves number with 25, which is correct.
When you encounter something that isn't a digit, then add the current value
of number to sum. Then, set number back to 0 for the next iteration.
Transcribed Image Text:The Loop Operation Let's spend a few moments writing a plan as a pseudocode. sum <- 0 number - 0 Grab the current character -> ch If ch is a digit then digit ‹- ascii-to-decimal (ch) number<- number * 10 number<- number + digit ● Else sum <- sum + number number <- 0 Here are some notes on implementing this plan in C++. Use str.at() to grab the current character; store it in a variable Use isdigit() to see if the character is between '0' and '9' inclusive There is no ascii-to-decimal function. Instead, subtract the character '0' from ch and store the result in the variable digit. Note that the ch has an underlying ASCII code and the codes are sequential. If ch is '0' then subtracting '0' will store the binary number in digit. If the character is '1', then subtracting '0' will leave the binary number 1. Multiply the current value of number by 10, and then add the digit. For instance, if number has the value 2 and digit has the value 5, then number * 10 -> 20 and adding 5 leaves number with 25, which is correct. When you encounter something that isn't a digit, then add the current value of number to sum. Then, set number back to 0 for the next iteration.
string sumNums (const string& str)
{
}
string result;
int sum{0};
int num{0};
for(size_t i{0}, len{str.size()}; i < len; ++i)
{
}
Grab the current character -> ch
If ch is a digit then
digit <- ascii-to-decimal(ch)
number <- number * 10
number <- number + digit
Else
sum <- sum + number
number - 0
return result;
Transcribed Image Text:string sumNums (const string& str) { } string result; int sum{0}; int num{0}; for(size_t i{0}, len{str.size()}; i < len; ++i) { } Grab the current character -> ch If ch is a digit then digit <- ascii-to-decimal(ch) number <- number * 10 number <- number + digit Else sum <- sum + number number - 0 return result;
Expert Solution
Step 1

We need to write a C++ code for the given scenario.

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Basics of loop
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education