Question
C++ language only. Thank you
C++ language (cout <). NO atoi function. And please pay attention to the rules at the bottom. Extra good rating to whoever can help me with this. TIA. In certain programming situations, such as getting information from web forms, via text boxes, the data coming in may need to be numerical (we want to perform arithmetic on it), but it is stored in the form of a list of characters (the numerical value stored is based upon the table above). Therefore, as the programmer, we must convert the character to an integer digit or the string containing digits as characters to its decimal equivalent. YOUR JOB: For this project, you will simulate this conversion. Your program will ask the user to enter a 4 digit numerical value. As the programmer, you will input the value as a string. You will devise an algorithm to convert the string of characters (string of digits) to a single integer, stored in a single integer value. At the end of this algorithm, you will print the addition (using the operator) of the input string to itself, and the addition of the converted integer to itself. For example, if the user were to input 9275, the outputs would be 18550 and 92759275 !!!Your program should generate appropriate error messages if the user ent less than 4 digits, (2) more than 4 digits, or (3) any non-numeric digits. !!!CONSTANTS should be used in place of literals where appropriate !!!!!Meaningful variable and constant names should match the intent or the purpose of the data item-use words, not abbreviations. Try to avoid loops if possible.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below with detailed inline comments.

CODE

=========================

#include <iostream>
#include <cstring>
#define SIZE 4 // CONSTANT to represent the size of the number`

using namespace std;

int main() {
string number_str;
cout << "Enter a 4-digit number: ";
getline(cin, number_str);
int length = number_str.length(); // stores the length of the input string
  
// the length of the input string is less than 4
if(length < SIZE) {
cout << "Error.... the number has less than 4 digits!!" << endl;
}
// the length of the input string is greater than 4
else if(length > SIZE) {
cout << "Error.... the number has more than 4 digits!!" << endl;
}
  
int number = 0; // represent the numerical value of the input string
  
// loop through all the characters of the input string
for(int i=0; i<number_str.length(); i++) {
int digit = number_str[i] - '0';  
// the character is not a digit, print error message
if(digit < 0 || digit > 9) {
cout << "Error.... the given string is not a valid number!!" << endl;
return 0;
}
  
// update the number value
number = number * 10 + digit;
}
  
  
// print the output
cout << "The output is: " << endl;
cout << number + number << ", " << number_str + number_str;
return 0;
}

OUTPUT

=========================

Enter a 4-digit number: 9275 The output is: 18550, 92759275Enter a 4-digit number: 12r4 Error.... the given string is not a valid number! I

Add a comment
Know the answer?
Add Answer to:
C++ language only. Thank you C++ language (cout <). NO atoi function. And please pay attention...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN C language Write a C program that prompts the user to enter a line of...

    IN C language Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr andcreadLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate...

  • c++ pleased Assignment 6a (15 points] - Character and String related processing... Listed below are two...

    c++ pleased Assignment 6a (15 points] - Character and String related processing... Listed below are two interesting programming challenges that will require a bit of character and/or string related processing with functions. (Carefully read the specifications for each programming challenge and select ONE.) (1) Sum of Digits in a String Write a program that asks the user to enter a series of single-digit numbers with nothing separating them. Read the input as a C-string or a string object. The program...

  • Banks issue credit cards with 16 digit numbers. If you've never thought about it before you...

    Banks issue credit cards with 16 digit numbers. If you've never thought about it before you may not realize it, but there are specific rules for what those numbers can be. For example, the first few digits of the number tell you what kind of card it is - all Visa cards start with 4, MasterCard numbers start with 51 through 55, American Express starts with 34 or 37, etc. Automated systems can use this number to tell which company...

  • Using C++ language please not matlab a. Write a function, called SumOfDigits that is able to...

    Using C++ language please not matlab a. Write a function, called SumOfDigits that is able to calculate the summation of a five-digit number. This function receives one integer parameter 'a', then returns the sum of 5 digits of the number. [2.5 marks] b. Write a function, called Armstrong that prints all Armstrong numbers in the range of 0 and 500. An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is...

  • In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and...

    In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character....

  • Please I need help with this c++ code. please show all steps , write comments and...

    Please I need help with this c++ code. please show all steps , write comments and show sample runs. Thank you. 1. The Federal Bureau of Investigation (FBI) has recently changed its Universal Control Numbers (UCN) for identifying individuals who are in the FBI's fingerprint database to an eight-digit base 27 value with a ninth check digit. The digits used are: 0123456789ACDE FHJKLMNPRTVWX Some letters are not used because of possible confusion with other digits: B->8, G->C, I- >1, 0->0,...

  • This is programming project 1 chapter 9(Book ISBN:1292222824 (1-292-22282-4) Walter Savitch Problem Solving with C++: Global...

    This is programming project 1 chapter 9(Book ISBN:1292222824 (1-292-22282-4) Walter Savitch Problem Solving with C++: Global Edition, 10/E) Question Do Programming Project 7 in Chapter 7 using a dynamic array. In this version of the problem, use dynamic arrays to store the ditits in each large integer. Allow an arbitrary number of digits instead of capping the number of digits at 20. TER 7/ Arrays time. For example digit at a the integer 1234 could be stored in the array...

  • C language please 2. You are to write a subroutine/function which will expect a string as...

    C language please 2. You are to write a subroutine/function which will expect a string as input. The function should locate any instances of multiple space characters together. The function should modify the string to replace all mulktiple-space blocks with single spaces. No non-space characters should be altered Example: The string "Bob has twelve has twelve apples." apples." should be altered to become "Bob

  • In the first task, you will write a Java program that accepts a string of the...

    In the first task, you will write a Java program that accepts a string of the format specified next and calculate the answer based on the user input. The input string should be of the format dddxxdddxx*, where d represents a digit and x represents any character and asterisk at the end represents that the string can have any number of characters at the end. 1. Prompt the user to enter a string with the specific format (dddxxdddxx*) 2. Read...

  • In Chapter 4, we developed an algorithm for converting from binary to decimal.

    1.In Chapter 4, we developed an algorithm for converting from binary to decimal. You can generalize this algorithm to work for a representation in any base. Instead of using a power of 2, this time you use a power of the base. Also, you use digits greater than 9, such as A . . . F, when they occur.In convert.py, define a function named repToDecimal that expects two arguments, a string, and an integer. The second argument should be the base....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT