string requires char type but how to add all entries and get total?

such as: strcpy, and strlen

They don't seem to be able to take other types like int and the datas have to be in int type or float in order to be able to sum them up.  when I use char, I can't do that?  Are there any way to make it work?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You want something like this?

char string[128];
memset (string, 0, 128);
strcpy (string, "This is the string.");

int i, total = 0;
for (i = 0; i < strlen (string); i++)
{ total += string[i]; }

// total = total of all values in the string.

If you have further questions, please feel free to ask.  I believe I will be able to answer any questions you have.
890Author Commented:
Not really but like the following
      #include <iostream.h>
      #include <conio.h>
      #include <cstring.h>

int main()
      float value;
      float value2;

      float* all;
      float* alla;

float* all2;

      int lenght;

      cout<<"enter "; cin>>value;

      lenght=strlen(all);  //strlen accept char only so it will not work

      alla=new char[lenght+4];
      strcpy(alla, all);

      cout<<"id "<< *all;

cout<<"enter value ";
cout<<"value "<< *all2<<endl;

float total = value + value2;     //like 3=1+2
//what I really want is to be able to caculate this and still able to use 'new' but char
// makes it impossible.
// I don't understand how to make it work.
//thanks your offer to help to clear my mine.


return 0;

890Author Commented:
see above
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

So what you want is just to find out how long a number like 123.45 is? (123.45 being 6 long)?   Is that right?
Use the sscanf() function.
For know lenght of float/double/long/int/... variables
you can use sizeof() function
float f;
int i = sizeof(f); //4
For copy  float/double/long/int/.. variable
best way simple use '=' . but can use
memcpy. example:
memcpy((void *)&f1,(void *)&f2,sizeof(float));// for copy float
You can do something like the following.  This code provides a generic strlen function that will work for any built in type:

#include <sstream>
#include <string.h>
#include <iostream>

using namespace std;

template <class T>
size_t stringlength(const T& value)
    ostringstream ss;
    ss << value;
    return ss.str().length();

typedef const char* PCC;
template <>
size_t stringlength<PCC>(const PCC& value)
    return strlen(value);

void main()
    int value = 5123;
    float f1 = 23.5;
    cout << stringlength(value) << endl;
    cout << stringlength(f1) << endl;
    cout << stringlength("abcsd") << endl;

The template specialization for char* is not actually required, but it will make things a bit more efficient.

You could do something similar for a string copy function.  I could provide this if you want.
890Author Commented:
Sorry right now I am not sure which is the right answer.  I'll need some time to study the codes, then get back to you to reward the points. Maybe I shouldn't use string in the first place but so far It's the only way I know how to create a new memory location without using array.
I think there is some confusion over exactly what you want.  Could you please clarify?
I think what "890" is saying, is that string functions like strlen and strcpy use arguments that are of type char, which doesn't do the same thing if he were to use arguments of type int, or float.  Since his data is of type int and float, he would like that these string functions handle the int and float types arguments (similarly to the way they do for char) so he can perform arithmetic and mathematical exercises on his data.

"890", if I am correct in understanding what it is you're faced with, and trying to accomplish, you will NOT be able to do what you want with these functions.  The simple reason being that strcpy and strlen (plus a few, similar to them) are functions specifically created to handle char and string types; NOT int, float, double (etc.) types.

strcpy, for example, copies one string to another.  You don't have to copy a variable of type int or float (etc.) the way one does with strings.  To copy a variable of type int or float from one variable to another variable of the same type, you simply assign one to the other.  For example:

int a = 100;
int b;
b = a;

In a sense, that's how you copy one variable of type int to another.  In other instances, if you were to cross assign variables of int with float, most compilers will do internal conversion for you, but in the process (depending on which is the rvalue) you may lose a decimal place, extra digits and have rounding off done for you.

There are some functions in the character handling library , <ctype.h>, that will perform test on characters to determine if a certain character is a digit or an alpha (etc.), e.g. "int isdigit(int i)" returns TRUE if "i" is a digit, and FALSE if it is not, and "int isalpha(int i)" returns TRUE if "i" is a letter, and FALSE if it is not.

Then there are some string conversion functions in the general utilities library <stdlib.h> that converts strings of digits to integer (i.e. numbers that are in string form; but are of type char or string).  These functions will convert such strings from char to integer and floating-point values.  For example:

double atof(const char *p)

will convert the string (of numbers which is of type char) that 'p' is pointing to, to double.  After doing this, you can add, subtract, divide, multiply (etc.) the result any way you choose.  Here's an example:

long nbr = atol("100");

the argument to the atol() function is a string comprising all numbers, but after the function executes, the result assigned to 'nbr' is of type long and can be mathematically used.

int atoi(const char *p)

will do similarly as the above example, except that the output will be of type int.


Here is an example of another function which is really neat!

double strtod(const char *p, char **pRestOfString)

This function will convert a sequence of characters (both numbers and letters, etc.) to type double, but separating the numbers from the rest of the string.  Here's an example:

double nbr;
char *p = "99.9% is OK.", *pRestOfString;

nbr = strtod(p, &pRestOfString);
cout<< nbr << " is all I need. The rest, \"" << pRestOfString << "\"" << endl;

would print:

99.9 is all I need. The rest, "% is OK."

The variable 'nbr' would contain the value 99.9, which you could then use to do whatever mathematical exercise you want.
890Author Commented:
Try is the right ans. Ans this Q I'll asign you the points.
/* Hope the below is what you want */
/* atoi converts string to integer */

#include <stdio.h>
#include <stdlib.h>

    char str1[10]="1";
    char str2[10]="2";

    printf("Result of string1 + string2 = %d\n",atoi(str1)+atoi(str2));

    return EXIT_SUCCESS;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.