Link to home
Start Free TrialLog in
Avatar of 890
890

asked on

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

String
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?
Avatar of thresher_shark
thresher_shark

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.
Avatar of 890

ASKER

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;

      all=&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 ";
cin>>value2;
all2=&value2;
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.

delete[]alla;


getch();
return 0;
}





Avatar of 890

ASKER

see above
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
Example:
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.
Avatar of 890

ASKER

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.
Avatar of 890

ASKER

Try is the right ans. Ans this Q I'll asign you the points.
ASKER CERTIFIED SOLUTION
Avatar of hongjun
hongjun
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial