Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Homework Beginner C++: Unable to perform Type Casting during addition of integers

In order to help us understand type casting in C++, we are required to perform addition of two int‘s as shown below. If we provide two int‘s as 4 and 5 respectively, the output should be 4 + 5 = 9.

I tried to follow this type casting tutorial without any success. Could someone please provide me a hint or something?

Quoting the assignment verbatim.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Your friend wrote a program called an adder. The adder is supposed to take two numbers inputted by a user and then find the sum of those numbers, but it’s behaving oddly.

Your first task is to figure out what is wrong with the adder. Your second task is to fix it.

Hint(s) to identify the problem
Try entering 1 and 1. You expect the output to be 2 but you get 11 instead. Similarly, if you enter 3 and 4, you expect the output to be 7 but you get 34. Remember, string concatenation also uses the + operator.

Hint(s) to identify the solution
The + operator functions differently based on the type of data that comes before and after it. What data types will cause the + operator to calculate a mathematical sum? What data type is present in the program now? How do you convert from one data type to another? Check out the Type Casting page for some idea

#include <iostream>
using namespace std;

int main() {
  
  string num1;
  string num2;
  cout << "Type the first whole number and then press Enter or Return: ";
  cin >> num1;
  cout << "Type the second whole number and then press Enter or Return: ";
  cin >> num2;
  

  string sum = num1 + num2;
  cout << ( num1 + " + " + num2 + " = " + sum )  << endl;

  
  return 0;
  
}

Edit:
Your first task is to figure out what is wrong with the adder. Your second task is to fix it.

>Solution :

You can’t solve this task with type casting. You need to either:

  • Change your code to use int variables instead of string variables:
#include <iostream>
using namespace std;

int main() {
  
  int num1;
  int num2;
  cout << "Type the first whole number and then press Enter or Return: ";
  cin >> num1;
  cout << "Type the second whole number and then press Enter or Return: ";
  cin >> num2;

  int sum = num1 + num2;
  cout << num1 << " + " << num2 << " = " + sum << endl;

  return 0;
  
}
  • Otherwise, convert (not cast) your string values into int values at runtime, and back:
#include <iostream>
#include <string>
using namespace std;

int main() {
  
  string num1;
  string num2;
  cout << "Type the first whole number and then press Enter or Return: ";
  cin >> num1;
  cout << "Type the second whole number and then press Enter or Return: ";
  cin >> num2;

  int sum = stoi(num1) + stoi(num2);
  cout << ( num1 + " + " + num2 + " = " + to_string(sum) )  << endl;

  return 0;
  
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading