Solved

Posted on 2003-03-19
Medium Priority
207 Views
I need to know how to take two strings ex("18" and "12") and be able to add them together to get 30.
0
Question by:PoKe_SmOt
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 4

Expert Comment

ID: 8170546
0

Accepted Solution

Cuperous earned 80 total points
ID: 8171438
This explains the overloaded function and the atoi function
In a Source file, you can write:
#include <stdio.h> //this class is for the gets() function
#include "col.h" //the file containing your class
#include <iostream.h> //Input Output Stream

void main()
{
int x; // declare an integer variable
char str1[10], str2[10]; // declare two strings
gets(str1); //use gets() function to get the first string
gets(str2);//use gets() function to get the second string
Coverload y(str1); // declare and send the first string
cout<<x;           // print the result on the screen
}

In a header file, you can write:

#include <stdlib.h> //this class is for the atoi() function
{
protected:
char str[10]; //declare string
public:
{
for(int i=0;i<10;i++)  // Copy the string sent
str[i]=string[i];
}
};

int operator+( Coverload obj, char string2[])
{
int total_value=0; // declare the total to equal 0
total_value += atoi(string2); //converts string2
//to an integer and
total_value += atoi(obj.str); //takes the value of
//the string in obj2
//and converts it into
//it to the total
}

I hope this is useful, please comment, if you have anymore problems. I'll be checking your problem again. And please grade it after testing it.
0

LVL 2

Expert Comment

ID: 8177708
A solution may be the atoi function (array-to-integer), which is a library function declared in the stdlib.h file :
cout << (atoi("18") + atoi("12")) ;
The desired conversion effect may be obtained with functions like :

int ArrayToInteger (char *s) { int i, n, sign ;
for (i = 0 ; s[i] == ' '|| s[i] == '\n'|| s[i] == '\t' ; i++) ;
sign = 1 ;
if (s [i] = '+' || s[i] == "-') sign (s[i++] == '+')?1:-1 ;
for (n = 0 ; s[i] >= '0' && s[i] <= '9' ; i++)
n = 10 * n + s[i] - '\0' ;
return sign * n ; }
This code is from books for C programming subjects.

int operator+ (char*, char*) ;
(of course, this prototype is for a global, non-class function).

Good luck !
0

Expert Comment

ID: 8621756
I'm not totally sure what you mean, Poke_Smot, but if you're trying to make it do simple addition in math, then a program similar to this may help:

---------------------------------------------------------------------------------------------
// This program will add two numbers inputted by the user!
#include <iostream.h>
#include <math.h>

int Blah(int x, int y);
int x, y, z;

int main()
{
cout << "Enter a number please." <<endl;
cin >> x;
cout << "Enter one more number." <<endl;
cin >> y;
cout << "The sum is " << Blah(x, y) <<endl;
return 0;
}

int Blah (int x, int y)
{
z = x + y;
return z;
}
---------------------------------------------------------------------------------------------

Or you could just skip the simple function 'Blah', resembling the following....

---------------------------------------------------------------------------------------------

#include <iostream.h>
#include <math.h>

int Blah(int x, int y);
int x, y, z;

int main()
{
cout << "Enter a number please." <<endl;
cin >> x;
cout << "Enter one more number." <<endl;
cin >> y;
z = x + y;
cout << "The sum is " << z <<endl;
return 0;
}
---------------------------------------------------------------------------------------------

Both do the same thing. Hope this helps.
0

LVL 9

Expert Comment

ID: 9510641

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Tinchos
EE Cleanup Volunteer
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
###### Suggested Courses
Course of the Month10 days, 21 hours left to enroll