n C++ I'm trying to modify the code I've written below to check the type of the array being called from main. If the array is int or double, user should enter a number instead of a string, and then the values get added together. Otherwise, if the array is a char it will default to the concatenate code I've already written. Here is the code I have: template T MyArray::increaseElementsBy() { cout << endl << "Welcome to increaseElementsBy()" << endl; string result; stringstream ss; string toConcat; cout << "Enter something to add to the element: "; cin >> toConcat; for (int i = 0; i < 10; i++) { ss << theArray[i] << toConcat << endl; } result = ss.str(); cout << result; return 0; }   If possible, it would be even better if what the user enters is also checked. If the user enters a string while the int array is being used, it should also concatenate the items together, but if they enter a string that is an int - convert the string to int and add it to the int array elements.

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

In C++ I'm trying to modify the code I've written below to check the type of the array being called from main. If the array is int or double, user should enter a number instead of a string, and then the values get added together. Otherwise, if the array is a char it will default to the concatenate code I've already written.

Here is the code I have:

template<typename T>
T MyArray<T>::increaseElementsBy()
{
cout << endl << "Welcome to increaseElementsBy()" << endl;
string result;
stringstream ss;
string toConcat;

cout << "Enter something to add to the element: ";
cin >> toConcat;

for (int i = 0; i < 10; i++)
{
ss << theArray[i] << toConcat << endl;
}

result = ss.str();
cout << result;

return 0;
}

 

If possible, it would be even better if what the user enters is also checked. If the user enters a string while the int array is being used, it should also concatenate the items together, but if they enter a string that is an int - convert the string to int and add it to the int array elements.

Expert Solution
steps

Step by step

Solved in 4 steps

Blurred answer
Knowledge Booster
Array
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.
Similar questions
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