Home > OS >  Homework Beginner C : Unable to perform Type Casting during addition of integers
Homework Beginner C : Unable to perform Type Casting during addition of integers

Time:01-16

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.

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;
  
}

CodePudding user response:

The problem with the code is that it is performing string concatenation when it needs to perform arithmetic addition instead. So you need to get the user's input into numeric variables, not strings. The assignment even alludes to this.

However:

Check out the Type Casting page for some idea

That is bad advice for this task, as you can't solve the problem with type casting.

You need to either:

  • Change the code to use int variables instead of string variables. This is the preferred solution, eg:
#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, if you want to continue using string variables, you need to convert (not type cast!) their values into int values at runtime, and then convert back afterwards, eg:
#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;
  
}

CodePudding user response:

if you typecast a character or string it get's converted into its equivalent ASCII value , either you need to use stoi or subtract from '0' for every digit position(a bit repeatitive work) go with stoi

  •  Tags:  
  • Related